abstract class WPForms_Template {
* Full name of the template, eg "Contact Form".
* Slug of the template, eg "contact-form" - no spaces.
* Source of the template.
* Short description the template.
public $description = '';
* Short description of the fields included with the template.
* URL of the icon to display in the admin area.
* Form template preview URL.
* Form template thumbnail url.
* Array of data that is assigned to the post_content on form creation.
* Priority to show in the list of available templates.
* Core or additional template.
* Modal message to display when the template is applied.
* Primary class constructor.
public function __construct() {
$type = $this->core ? '_core' : '';
add_filter( "wpforms_form_templates{$type}", [ $this, 'template_details' ], $this->priority );
add_filter( 'wpforms_create_form_args', [ $this, 'template_data' ], 10, 2 );
add_filter( 'wpforms_save_form_args', [ $this, 'template_replace' ], 10, 3 );
add_filter( 'wpforms_builder_template_active', [ $this, 'template_active' ], 10, 2 );
public function init() {}
* Add basic template details to the Add New Form admin screen.
* @param array $templates Templates array.
public function template_details( $templates ) {
'source' => $this->source,
'categories' => $this->categories,
'description' => $this->description,
'includes' => $this->includes,
'url' => ! empty( $this->url ) ? $this->url : '',
'plugin_dir' => $this->get_plugin_dir(),
'thumbnail' => ! empty( $this->thumbnail ) ? $this->thumbnail : '',
* Get the directory name of the plugin in which current template resides.
private function get_plugin_dir(): string {
$reflection = new ReflectionClass( $this );
$template_file_path = wp_normalize_path( $reflection->getFileName() );
// Cutting out the WP_PLUGIN_DIR from the beginning of the template file path.
$template_file_path = preg_replace( '{^' . wp_slash( wp_normalize_path( WP_PLUGIN_DIR ) ) . '}', '', $template_file_path );
$template_file_chunks = explode( '/', $template_file_path );
return $template_file_chunks[1];
* Add template data when form is created.
* @param array $args Create form arguments.
* @param array $data Template data.
public function template_data( $args, $data ): array {
if ( empty( $data['template'] ) || $data['template'] !== $this->slug ) {
// Enable Notifications by default.
$this->data['settings']['notification_enable'] = $this->data['settings']['notification_enable'] ?? '1';
* Allow modifying form data when a template is applied to the new form.
* @param array $form_data New form data.
$this->data = (array) apply_filters( 'wpforms_templates_class_base_template_modify_data', $this->data );
$args['post_content'] = wpforms_encode( $this->data );
* Replace template on post update if triggered.
* @param array $form Form post data.
* @param array $data Form data.
* @param array $args Update form arguments.
public function template_replace( $form, $data, $args ): array {
// We should proceed only if the template slug passed via $args['template'] is equal to the current template slug.
// This will work only for offline templates: Blank Form, all the Addons Templates, and all the custom templates.
// All the online (modern) templates use the hash as the identifier,
// and they are handled by `\WPForms\Admin\Builder\Templates::apply_to_existing_form()`.
if ( empty( $args['template'] ) || $args['template'] !== $this->slug ) {
$form_data = wpforms_decode( wp_unslash( $form['post_content'] ) );
// Something is wrong with the form data.
if ( empty( $form_data ) ) {
// Compile the new form data preserving needed data from the existing form.
$new['id'] = $form_data['id'] ?? 0;
$new['settings'] = $form_data['settings'] ?? [];
$new['payments'] = $form_data['payments'] ?? [];
$new['meta'] = $form_data['meta'] ?? [];
$template_id = $this->data['meta']['template'] ?? '';
// Preserve template ID `wpforms-user-template-{$form_id}` when overwriting it with core template.
if ( wpforms_is_form_template( $form['ID'] ) ) {
$template_id = $form_data['meta']['template'] ?? '';
$new['meta']['template'] = $template_id;
* Allow modifying form data when a template is replaced.
* @param array $new Updated form data.
* @param array $form_data Current form data.
* @param array $template Template data.
$new = (array) apply_filters( 'wpforms_templates_class_base_template_replace_modify_data', $new, $form_data, $this );
// Update the form with new data.
$form['post_content'] = wpforms_encode( $new );
* Pass information about the active template back to the builder.
* @param array $details Details.
* @param object $form Form data.
public function template_active( $details, $form ) {
$form_data = wpforms_decode( $form->post_content );
if ( empty( $this->modal ) || empty( $form_data['meta']['template'] ) || $this->slug !== $form_data['meta']['template'] ) {
$display = $this->template_modal_conditional( $form_data );
'description' => $this->description,
'includes' => $this->includes,
'modal_display' => $display,
* Conditional to determine if the template informational modal screens
* @param array $form_data Form data and settings.
public function template_modal_conditional( $form_data ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found