I've created my custom posts. Data from the same is displayed in let's say single-custom-post.php.

Now to clean that file and to remove 100 lines of code, I want to make a new file (let's call it custom-header.php) and then just echo or call that content.

Since I've built custom post as a plugin, ideally would be nice to call it if plugin is active, however I don't know how to finish this because one statement is if myplugin is active but then I would somehow add another statement - function, and here I am lost (below).

I could call it by if function exist, but then i am not sure how do I create that function.

What I did and it does work is this:

  • inside custom-header.php I've added

    if ('custom_header_display') { ?>   
        //my code
    } ?>

and then in single-custom-post.php I replaced that 100 lines of code with

include (TEMPLATEPATH . '/inc/custom-header.php' );?>

So my problem is, I don't know is this a good way, and if it's not, what is it?


You've got an issue with your code. This code will always return true because you're passing a string to it.

if ('custom_header_display') { ?>   
    //my code
} ?>

You can instead use is_plugin_active() to check if the plugin is activated.

Quoting the example from the Codex page, your code should be like:


include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( 'plugin-directory/plugin-file.php' ) ) {
   // include your file.
| improve this answer | |
  • Hey RRikesh, yea i know that's why I'm confused. Now thing is, i did find the same snippet as you, but I'm not sure what to add here // include your file . Do i add here content or the content inside another if, for an example if ('custom_header_display') { ?> ...that's what i am not sure. Check if plugin exist, show content, or check if plugin exist ..check something else, show content.. – Max Mar 9 '16 at 19:01
  • No, your if('customheader_display') is flawed and should be remove. Add the include (TEMPLATEPATH . '/inc/custom-header.php' ); part in there – RRikesh Mar 10 '16 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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