[wp-trac] [WordPress Trac] #58799: Need to add filter to register a template in [NEW] WordPress site editor through a plugin

WordPress Trac noreply at wordpress.org
Thu Jul 13 11:55:00 UTC 2023


#58799: Need to add filter to register a template in [NEW] WordPress site editor
through a plugin
-------------------------+-----------------------------
 Reporter:  saadiqbal    |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Editor       |    Version:  6.2.2
 Severity:  blocker      |   Keywords:  has-patch
  Focuses:  template     |
-------------------------+-----------------------------
 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.

 **REF**: https://wordpress.stackexchange.com/questions/417325/how-to-
 register-a-template-in-new-wordpress-site-editor-through-a-plugin


 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

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58799>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list