• I am developing a plugin for Wordprss. When I activate the plugin, I want to add a page template. When creating a page with dropdown, I should select the page template and create the page.

    I found out that I need to use the following code, but the page template is not enabled even though the page template name appears in the dropdown.

    add_filter( 'theme_page_templates', 'filter_inject_page_templates' );
    
    function filter_inject_page_templates( $templates ) {
        $path               = plugin_dir_path(__FILE__).'templates/template-landing.php';
        $templates[ $path ] = 'Template Landing' . $path;
        return $templates;
    }

    I need help urgently. Thank you very much, everyone.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    That filter only defines the dropdown list. Whatever you place there, WP will by default only search theme folders for the selected files. Doesn’t matter what path you supply, WP only looks in theme folders.

    You need to help WP find templates in plugin folders. You can use the “template_include” filter to get WP to use your template file when it’s the one that was selected. The passed $template arg would be the one you added to the dropdown, but it’s full path will be wrong. Return the correct full path and WP will use it.

Viewing 1 replies (of 1 total)
  • The topic ‘theme_page_templates Not working.’ is closed to new replies.