Opened 14 months ago
Last modified 6 months ago
#58799 new enhancement
Need to add filter to register a template in [NEW] WordPress site editor through a plugin
Reported by: | saadiqbal | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | blocker | Version: | 6.2.2 |
Component: | Editor | Keywords: | has-patch |
Focuses: | template | Cc: |
Description
I am trying to add a new template in the WordPress site editor like this like this
but unable to do it with my custom plugin.
I have found a filter to register a custom template
add_filter( 'default_template_types', 'register_template_type' ); function register_template_type( $templates ) { $templates['help-us'] = array( 'title' => _x( 'Page: Help Us', 'Template Name' ), 'description' => __( 'Displays Help Us template' ), ); return $templates; }
after adding this code I have figured out that by adding a file in the theme's template directory help-us.html I was able to register and display my custom template.
What if I want to add a custom template via plugin?
I was not able to find a way to add a custom template via the plugin. Previously I was using customizer in my plugin to customize the layout of a page with some custom functionality, now I am looking for a way to customize this page with the help of NEW site editor.
After going through, I figured out that we just need to make the template html file visible from the plugin to the template loop in order to display it. So I went over and added filter hooks in two places which just worked for me.
We need to add filters in following two lines;
1- wp-includes\block-template-utils.php:263
$themes = array( get_stylesheet() => get_stylesheet_directory(), get_template() => get_template_directory(), ); // NEW Filter hook $themes = apply_filters( 'block_editor_templates_files', $themes, $template_type ); $template_files = array();
2- wp-includes\block-template-utils.php:306
$themes = array( get_stylesheet() => get_stylesheet_directory(), get_template() => get_template_directory(), ); // NEW Filter hook $themes = apply_filters( 'block_editor_templates_files', $themes );
Looking forward to your feedback and consideration.
Thanks