[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
Wed Feb 21 22:02:58 UTC 2024


#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      |  Resolution:
 Keywords:  has-patch    |     Focuses:  template
-------------------------+------------------------------

Comment (by judyeland):

 It seems like you've already identified the filter hooks in the WordPress
 core that you need to modify to add a custom template via a plugin.
 However, modifying core files directly is generally discouraged in
 WordPress because it can lead to compatibility issues and difficulties
 during updates.

 Instead of modifying core files, a recommended approach is to use action
 and filter hooks provided by WordPress to achieve your goal. Based on the
 information you provided, it looks like you are trying to make the custom
 template file visible from your plugin.

 Here's a suggestion on how you might achieve this:

 1. **Create a Function to Register Your Template:**

    In your plugin file or a dedicated file, create a function to register
 your custom template. This function will be hooked into the
 `block_editor_templates_files` filter.
 {{{
    ```php
    function register_custom_template_files($themes) {
        // Add your plugin's template directory
        $themes['your-plugin-name'] = plugin_dir_path(__FILE__) .
 'templates';

        return $themes;
    }

    add_filter('block_editor_templates_files',
 'register_custom_template_files');
 }}}


 This assumes that your template file is located in the 'templates'
 directory inside your plugin.

 2. **Place the Template File in Your Plugin Directory:**

    Create a directory named 'templates' in your plugin directory, and
 inside it, place your template file, e.g., 'help-us.html'.

 3. **Use the Template in the Site Editor:**

    With the above changes, your custom template file should now be
 recognized by the block editor, and you can use it for your pages.

 Remember to replace 'your-plugin-name' with the actual name of your
 plugin.

 This approach avoids modifying core files directly and provides a more
 sustainable and update-friendly solution. Always ensure you thoroughly
 test your plugin to make sure it works as expected.

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


More information about the wp-trac mailing list