Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress: Check if Plugin is installed

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.

like image 564
J. Doe Avatar asked Jul 26 '16 16:07

J. Doe


People also ask

How do I know if WordPress plugin is installed or not?

Open up the page source for the website and press Ctrl + F to search the code quickly. Type in wp-content/plugins/ and start the search. You will find all the plugins used on the site.

Where are plugins located in WordPress?

All WordPress plugins you download and install on your site are stored in /wp-content/plugins/ folder.


3 Answers

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!

like image 61
Mr. Jo Avatar answered Sep 25 '22 04:09

Mr. Jo


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

like image 42
Jonas Lundman Avatar answered Sep 21 '22 04:09

Jonas Lundman


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; ?>
like image 29
Dmitry Avatar answered Sep 24 '22 04:09

Dmitry