Support » Developing with WordPress » Page template added via plugin not saved in Gutenberg

  • I have added page templates via plugin. It works absolutely fine with classic editor but while using Gutenberg, the page can not be published when page template is assigned. However, page templates added via theme works fine and page is saved as well. But page templates added via plugins creates issues as page cannot be published with those page templates (added via plugin) assigned.

    This is how I have added page templates via plugin:

    function wpte_filter_admin_page_templates( $templates ) {
         $templates['templates/template-destination.php'] = __( 'Custom Template','');
         return $templates;
        }
        
    /**
      * Destination template added.
      */
    add_action( 'wp_loaded', array( $this, 'wpte_add_destination_templates' ) );    	
    function wpte_add_destination_templates() {
       if( is_admin() ) {
          add_filter( 'theme_page_templates', array($this, 'wpte_filter_admin_page_templates' ) );
        	    }
        	}

    This is the error seen on console when ‘Publish’ button is pressed after selecting plugin’s page templates:

    {"code":"rest_invalid_param","message":"Invalid parameter(s): template","data":{"status":400,"params":{"template":"template is not one of templates\/about.php, templates\/contact.php, templates\/team.php, templates\/testimonial.php."}}}

    All of these templates are of theme. Page templates added via plugin is not listed there. Though, they are available in the templates drop-down.

    Any help would be more than appreciable.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I’ve done something similar without issue. It looks like your theme or something is doing extra validity checking. You would need to disable or enhance this validity checking.

    Apparently your plugin has added its template files to the theme’s templates? Or is there other code sorting out the path to your template? Your plugin shouldn’t add files to theme folders. I’ve stored plugin templates in my plugin without problems. I also hook “template_include” and when the page’s template is one of my plugin’s, I correct the path to the file before returning the result. Just sayin’. This is unrelated to your current issue.

    Hello @bcworkz,

    Many thanks for replying.

    The function was not pasted properly in my question above. So, below is my full correct code. I am adding page templates via plugin so that it can be assigned to a page. I am not adding files to themes. I am just providing base path to the plugin’s template file (template-destination.php)that I am adding. If someone adds a page with page template Destination Template then that page will use template-destination.php.

    Can you please elaborate on this: It looks like your theme or something is doing extra validity checking? Can you please point me what I am doing wrong in the code?

    Below is my full correct code:

       /**
         * Destination template.
        */
        function wpte_get_destination_template( $template ) {
            $post = get_post();
            $page_template = get_post_meta( $post->ID, '_wp_page_template', true );
            if( $page_template == 'templates/template-destination.php' ){
                $template_path = locate_template( array( 'template-destination.php' ) );
                if(!$template_path)
                {
                    $template_path = BASE_PATH . '/includes/templates/template-destination.php';
                }
                return $template_path;
            }
            return $template;
        }
    
    	/**
         * Destination template returned.
        */
    	function wpte_filter_admin_page_templates( $templates ) {
    	    $templates['templates/template-destination.php'] = __( 'Destination Template','' );
    	    return $templates;
    	}
    
    	/**
         * Destination template added.
        */
        add_action( 'wp_loaded', array( $this, 'wpte_add_destination_templates' ) );
    	function wpte_add_destination_templates() {
    	    if( is_admin() ) {
    	        add_filter( 'theme_page_templates', array( $this, 'wpte_filter_admin_page_templates' ) );
    	    }
    	    else {
    	        add_filter( 'page_template', array( $this, 'wpte_get_destination_template' ) );
    	    }
    	}
    Moderator bcworkz

    (@bcworkz)

    I don’t know if there is anything wrong with your code. I use the theme_page_templates filter as well and have never seen the console errors you posted. The error message implies some sort of check was run on chosen templates and yours was not on a list of theme specific template files.

    Where is “page_template” filter applied? I couldn’t find it. I use “template_include” filter for the same situation. My callback checks the post meta value keyed “_wp_page_template” and if it’s one of my templates, returns the proper path to the file in my plugin folder. You might try something similar to see if it gets around that validity check.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Page template added via plugin not saved in Gutenberg’ is closed to new replies.