Reputation: 525
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.
Upvotes: 13
Views: 18249
Reputation: 503
Check the example below:
$plugin_path = 'wordpress-seo/wp-seo.php';
if (isset(get_plugins()[$plugin_path])) {
if (is_plugin_active($plugin_path)) {
echo 'Plugin is installed and active!';
} else {
echo 'Plugin is installed but not active.';
}
} else {
echo 'Plugin is not installed.';
}
Upvotes: 0
Reputation: 35
Since Wordpress 6.5 there's an easier way to make another plugin as required.
Add below lines in main plugin file:
/**
* Plugin Name: Your plugin name
* Requires Plugins: woocommerce
*/
Change plugin name to your required plugin URI
See detailed explanation at link: Introducing Plugin Dependencies in WordPress 6.5
Upvotes: 0
Reputation: 41
$all_plugins = get_plugins();
//print_r( $all_plugins );
if( array_key_exists( 'woolementor/woolementor.php', $all_plugins ) ){
//do something
}
Upvotes: 4
Reputation: 29
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' ) );
Upvotes: 0
Reputation: 4372
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; ?>
Upvotes: 2
Reputation: 1480
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
Upvotes: 18
Reputation: 125
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;
}
Upvotes: 3
Reputation: 5261
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!
Upvotes: 3
Reputation: 21
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
}
Upvotes: 0