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:
/**
* 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' ) );
}
}
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.
add_action( 'wp_loaded', $plugin_admin, 'wpte_add_destination_templates' );
is not valid. What is$plugin_admin
supposed to be?