SparrwHawk
SparrwHawk

Reputation: 14153

Detect whether WordPress plugin is present

Is there a way to detect whether a plugin is present in WordPress and output conditional code?

Here is why I want to do that...

I'm using the WordPress SEO Yoast plugin, and need to change the default title tag to get it to work properly e.g.

<title>
    <?php
        wp_title( '|', true, 'right' );
    ?>
</title>

Needs to be changed to ...

<title><?php wp_title(''); ?></title>

That's fine, but what if I uninstall SEO Yoast at some point and forget to switch the code back? To be safe I'd like to have some conditional code that says..

If the SEO Yoast plugin is present, change the title code, if not, output the default title code.

Is there a simple way to do this?

Thanks

Upvotes: 1

Views: 760

Answers (1)

The Alpha
The Alpha

Reputation: 146191

You can try is_plugin_active function (In the front end, in a theme, etc)

<?php 
    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    If (is_plugin_active('plugin-directory/plugin-file.php')) {

        //plugin is activated
    }
    else
    {
        //plugin is not activated
    }
?>

More here.

Update:

<?php
    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    if (is_plugin_active('wordpress-seo/wp-seo.php')) { 
        ?><title><?php wp_title(''); ?></title><?php
    } else {

        ?><title><?php wp_title( '|', true, 'right' ); ?></title><?php
    }
 ?>

Upvotes: 2

Related Questions