Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Yoast SEO plugin

I want to detect Yoast SEO. I already use this function:

function active( $plugin ) {
    $network_active = false;
    if ( is_multisite() ) {
        $plugins = get_site_option( 'active_sitewide_plugins' );
        if ( isset( $plugins[$plugin] ) ) {
            $network_active = true;
        }
    }
    return in_array( $plugin, get_option( 'active_plugins' ) ) || $network_active;
}

if ( active( 'wordpress-seo/wp-seo.php' ) {

And that works fine. But if Yoast ever thinks of renaming wordpress-seo/wp-seo.php, this function becomes useless. So we need to add an backup, something which is hard to change, like the WPSEO_VERSION constant:

if ( active( 'wordpress-seo/wp-seo.php' ) {
    // activate
} elseif( defined( 'WPSEO_VERSION' )) {
    // activate
} else {
    // deactivate
}

This line if( defined( 'WPSEO_VERSION' )) { for some reason does not detect Yoast.. how is that possible?

Thanks everyone.

like image 906
Hacked Files Avatar asked Dec 08 '22 21:12

Hacked Files


1 Answers

The simplest way is:

if ( 
    is_plugin_active( 'wordpress-seo/wp-seo.php' ) 
    || is_plugin_active( 'wordpress-seo-premium/wp-seo-premium.php' )
) {
   /* Let's do cool things */
}

Function Reference/is plugin active

like image 92
Ivijan Stefan Stipić Avatar answered Jan 14 '23 17:01

Ivijan Stefan Stipić