1

I want to add new custom templates in my plugin (custom-page.php, custom-single.php). When activated, I want to dynamically use my plugin's custom templates to override the templates (page.php, single.php) of the theme.

Is there a function that can do this?

1
  • hi! I found your question very useful, thank you. I have a question: how can you integrate your new templates from your plugin into any Wordpress theme? I mean, any WP theme is different...how can you preserve the aspect of the current theme? thankyou in advance! M.
    – Mauro
    Aug 19, 2021 at 11:10

1 Answer 1

4

One way you can do that is by adding a filter to the hook {$type}_template.

Sample code:

function my_plugin_custom_template( $template, $type ) {
    switch ( $type ) {
        case 'page':
            $template = '/path/to/custom-page-template';
            break;

        case 'single':
            $template = '/path/to/custom-single-template';
            break;
    }
    return $template;
}
add_filter( 'page_template', 'my_plugin_custom_template', 10, 2 );   // Filter for page.php
add_filter( 'single_template', 'my_plugin_custom_template', 10, 2 ); // Filter for single.php

Or you can also add a filter to the hook template_include.

Sample code:

function my_plugin_custom_template2( $template ) {
    if ( is_page() ) {
        $template = '/path/to/custom-page-template';
    }
    elseif ( is_single() ) {
        $template = '/path/to/custom-single-template';
    }
    return $template;
}
add_filter( 'template_include', 'my_plugin_custom_template2' );

It's up to you to decide which hook to filter; however, the template_include filter is called after the {$type}_template hook. And if you look at the my_plugin_custom_template() code, you basically don't need to check whether the current page (i.e. queried object) is a Page, Post, etc.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.