0

I'm trying to build a plugin that can be used within every theme. It uses a custom post type and i used the action hook to change the content, so i could use extra meta fields and such. Also explained here: How to make my plugin theme-independent?.

Problem:
It's working but the problem is it uses the default post template from the active theme. They often have a lot of meta stuff and i don't want that. Creating a custom template in the plugin folder wouldn't help because every theme is different.

Possible solution:
Using the default page template of the theme. But how can I accomplish this?

1
  • Could you elaborate a bit on what you want your plugin to add/modify in the template? Right now the question is kind of broad.
    – cjbj
    Jun 8, 2016 at 15:33

1 Answer 1

2

Using default page template has the same issues than using single post template. It is true that usually page tempaltes has not post information like author, date and so on, but it is totally up to the theme developer. Even there are themes without page templates.

But if you want to force the use of the default page template for your custom post, you can do it (only if page.php exists in the active theme, of course):

add_filter( 'template_include', 'cyb_set_template' );
function cyb_set_template( $template ) {

    if ( is_singular( 'yourcpt' )  ) {
        $page_template = locate_template( array( 'page.php' ) );
        if ( '' != $page_template ) {
            return $page_template;
        }
    }

    return $template;
}

Note that this break WordPress template hierarchy. No theme could use single-yourcpt.php to build custom templates for your custom post type. Also, the only template required by a theme is index.php and there could be themes only with index.php template file.

The next snippet could solve the issue by looking for single-yourcpt.php file first, theme page.php, single.php and finally index.php (code not tested):

add_filter( 'template_include', 'cyb_set_template' );
function cyb_set_template( $template ) {

    if ( is_singular( 'yourcpt' )  ) {
        // Try to locate single-youprcpt.php first,
        // then page.ph
        $page_template = locate_template( array( 'single-yourcpt.php', 'page.php', 'single.php', 'index.php' ) );
        if ( '' != $page_template ) {
            return $page_template;
        }
    }

    return $template;
}
2
  • wished there was a better solution, all the blog based themes have to much meta information if you ask me. Making easy to implement plugins is hard
    – Gijs
    Jun 9, 2016 at 13:08
  • 1
    Answer upadated with fallback to single.php and index.php. This approach is similar to the method used in bbPress. Other plugins let the user choose which page he/she wants to use by inserting shortcodes or by implementing a cofigurable option. I couldn't say which approach is better but surely (99.9999%) you can not force a theme to hide some template parts.
    – cybmeta
    Jun 9, 2016 at 14:20

Your Answer

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

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