skip to Main Content

I want to check if Yoast SEO is installed in WordPress. I have activated Yoast SEO in my Test Environment, but it’s not working out.

In the wp-seo-main.php of Yoast, there’s this line on line 16:

define( 'WPSEO_VERSION', '3.4' );

So I thought, that’s a good line to check if Yoast is installed and running, so I did:

if ( defined( 'WPSEO_VERSION' ) ) {
    echo '<script>alert("Yes, defined");</script>';
} else {
    echo '<script>alert("No, undefined");</script>';
}

But it gives me “No, undefined”. How weird, because it should be defined.

Anyone got an idea? I’m totally out of ideas.

7

Answers


  1. Or without extra inclusions, front end or back end:

    if ( in_array( 'wordpress-seo/wp-seo.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
        // do stuff only if Yoast is installed and active
    }
    

    Works with any plugin, just look into your plugins folder for your target: plugin-folder/plugin-index-name.php where the latter shuld resident the Plugin details at the top inside the file.

    Please Check Doc. reference

    Login or Signup to reply.
  2. Inspired by Jonas Lundman’s answer I wrote this to handle also when Yoast premium is active.

    function is_yoast_active() {
        $active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
    
        foreach ( $active_plugins as $plugin ) {
            if ( strpos( $plugin, 'wp-seo' ) ) {
                return true;
            }
        }
    
        return false;
    }
    
    Login or Signup to reply.
  3. Use below code:

     <?php include_once ABSPATH . 'wp-admin/includes/plugin.php'; ?>
     <?php if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) ) :
    
          //do staff
    
     <?php endif; ?>
    
    Login or Signup to reply.
  4. This is what worked for me.

    if(count(
            preg_grep(
                '/^wordpress-seo.*/wp-seo.php$/',
                apply_filters('active_plugins', get_option('active_plugins'))
            )
        ) != 0
    ){
        // Plugin activated
    }
    
    Login or Signup to reply.
  5. Thanks to all the others for doing a good job so far! But the question is also about a check if a plugin is installed and all your answers are only about checking if a plugin is active.

    So I went to my playground and developed a correct check I want to show you by using functions WP already provided.

    // Check if needed functions exists - if not, require them
    if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'is_plugin_active' ) ) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
    }
    
    /**
     * Checks if Yoast SEO is installed
     *
     * @return bool
     */
    function is_wp_seo_installed(): bool {
        if ( check_plugin_installed( 'wordpress-seo/wp-seo.php' ) ) {
            return true;
        }
    
        return false;
    }
    
    /**
     * Check if Yoast SEO is activated
     *
     * @return bool
     */
    function is_wp_seo_active(): bool {
        if ( check_plugin_active( 'wordpress-seo/wp-seo.php' ) ) {
            return true;
        }
    
        return false;
    }
    
    /**
     * Check if plugin is installed by getting all plugins from the plugins dir
     *
     * @param $plugin_slug
     *
     * @return bool
     */
    function check_plugin_installed( $plugin_slug ): bool {
        $installed_plugins = get_plugins();
    
        return array_key_exists( $plugin_slug, $installed_plugins ) || in_array( $plugin_slug, $installed_plugins, true );
    }
    
    /**
     * Check if plugin is installed
     *
     * @param string $plugin_slug
     *
     * @return bool
     */
    function check_plugin_active( $plugin_slug ): bool {
        if ( is_plugin_active( $plugin_slug ) ) {
            return true;
        }
    
        return false;
    }
    

    As you can see I’ve wrote two separate functions to check if Yoast SEO is installed and active so I just need to call it and check the return param:

    $installed = is_wp_seo_installed();
    $active    = is_wp_seo_active();
    
    if ( $installed && $active ) {
        echo 'WP SEO is installed and active!';
    } else {
        echo 'WP SEO is not installed or active!';
    }
    

    But if you want to skip this two extra functions, you can call the two check functions directly:

    $installed = check_plugin_installed( 'wordpress-seo/wp-seo.php' );
    $active    = check_plugin_active( 'wordpress-seo/wp-seo.php' );
    
    if ( $installed && $active ) {
        echo 'Yoast SEO is installed and active!';
    } else {
        echo 'Yoast SEO is not installed or active!';
    }
    

    I hope this helps you to do a good installed & active check!

    Login or Signup to reply.
  6. Check this, to get all active plugin names;

    $callback = function($key){
        $key = explode('/', $key);
        return $key[0];                                        
    }
    
    $active_plugins = array_map($callback, get_option( 'active_plugins' ) );
    
    Login or Signup to reply.
  7. $all_plugins = get_plugins();
    
    //print_r( $all_plugins );
    
    if( array_key_exists( 'woolementor/woolementor.php', $all_plugins ) ){
    
         //do something
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search