How to display content If function exist/condition true?

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?

Topic functions conditional-content Wordpress

Category Web


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:

<?php

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( 'plugin-directory/plugin-file.php' ) ) {
   // include your file.
} 

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.