• Hi there,

    I’m creating a plugin that make use of a custom post type, i need it to be theme-independent so i am overriding the template with my own which is in my plugin’s folder, this is how i do it:

    define( 'PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
    
    //Template fallback
    add_filter( "template_include", 'ps_theme_redirect' );
    
    function ps_theme_redirect( $template ) {
        //cpt is the slug of the custom post type registered
        if ( get_post_type() == 'cpt' ) {
            if ( is_single() ) {
                //looks in the current theme directory for the template if so it is returned otherwise the template from the plugin directory is returned.
                if ( file_exists( get_stylesheet_directory() . '/single-cpt.php' ) )
                    return get_stylesheet_directory() . '/single-cpt.php';
                return PLUGIN_PATH . '/templates/single-cpt.php';
            } else {
                if ( file_exists( get_stylesheet_directory() . '/archive-cpt.php' ) )
                    return get_stylesheet_directory() . '/archive-cpt.php';
                return PLUGIN_PATH . '/templates/archive-cpt.php';
            }
        }
        return $template;
    }

    Now i need also to do this with the header because i don’t want to show menus and stuff that a theme might have in header.

    Can anyone please advice on how to do this? Basically i need to override theme’s header.php with the one i have in my plugins folder.

    Thanks in advance,
    Stelios

Viewing 10 replies - 1 through 10 (of 10 total)
  • I’m not sure if this is going ot help that much, but there’s some information about this sort of issue over on Stack Overflow. The biggest thing in there is that there’s some good ideas about what filters or actions you’ll need to set the template to the one that you want to use. If you use the ideas in there as a base I’m sure that you’ll be able to make it happen with a bit of hacking.

    Thread Starter steliodj

    (@steliodj)

    I’m not sure if this is going ot help that much, but there’s some information about this sort of issue over on Stack Overflow. The biggest thing in there is that there’s some good ideas about what filters or actions you’ll need to set the template to the one that you want to use. If you use the ideas in there as a base I’m sure that you’ll be able to make it happen with a bit of hacking.

    Hi catacaustic, unfortunately i couldn’t find anything there, it might be something but i don’t get…i’m not really good at PHP, but willing to learn.

    Any more help ? Would be much appreciated…or any known plugins that have a similar functionality so i can investigate the code.

    Cheers,
    Stelios

    If you can’t understand the code that’s on there, there’s not much else that we can do. It’s a little bit advanced, but that’s how it’s done. think of this as a learning opportunity where you’ll need to push yourself out of your comfort zone to learn something new. I can’t suggest a plugin that does what you’re asking because most plugins don’t tamper with the template code, they leave that to the theme. However if you really want to look into it (warning: it will be hard to find) I’d suggest looking at plugins like Woocommerce and Buddypress as these both set up different folders for some templates, so there’s got to be something in them that will help with what you’re after.

    Thread Starter steliodj

    (@steliodj)

    I’m creating a plugin that creates landing pages and i need them to be totally different than any other page so it has to be theme-independent…this is the reason i’m messing with the template.

    Woocommerce & buddypress have custom templates for sure but do they have a custom header as well ? I’ll have to give them a better look.

    Thanks again.

    Any more tips are more that welcome.

    Thanks,
    Stelios

    Hmm… Is this a plugin that you’re going to be releasing, or one that you’re using only for yourself? If you’re going to release it them yeah you’ll need to find something. If it’s only for your own use, then forget doing this and just create a new template for each landing page.

    Thread Starter steliodj

    (@steliodj)

    Exactly…this is a plugin that i will be releasing and this is why i need it to work out of the box.

    So i’m trying to do it with proper wordpress standards and code because most possibly i will be submitting this to WordPress repo.

    [bumps will get moderated – think of posting more information if possible]

    Thread Starter steliodj

    (@steliodj)

    Someone suggested that i could replace the get_header with
    inlcude( PLUGIN_PATH . '/templates/header.php' );

    and get_footer with
    inlcude( PLUGIN_PATH . '/templates/footer.php' );

    So i can use my own files instead of the theme’s

    Is this bad practice?

    Any more suggestions?

    Moderator bcworkz

    (@bcworkz)

    That is exactly what came to my mind as I scanned through your previous posts. As long as you call wp_head() so other plugins can do whatever, I don’t see any issues. I don’t see how this particular move would be considered bad practice, but I’m not up to date with these sort of things.

    FWIW, I do sort of question ignoring the theme, but I don’t know what you’re up to, so I assume you’ve thought this through and have your good reasons. So be it. Like I said, I’m not up to date on good or bad practice.

    Thread Starter steliodj

    (@steliodj)

    Hi bcworkz, thanks for replying.

    It’s a plugin for creating landing pages and the template in question(including header and footer) will be used for only a specific custom post type(landing pages).
    So i think it will not be an issue ignoring the theme, actually i think that it is better to ignore the theme for this specific purpose.

    What do you think? Bad move from me 🙂 ?

    Moderator bcworkz

    (@bcworkz)

    Hmmm. Landing pages. I can see valid arguments for both approaches. Since your initial instinct was to ignore, I’m inclined to say stay with that.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Override header.php with one in plugin's folder’ is closed to new replies.