如何检查WordPress插件是否安装并激活?

问题描述 投票:0回答:8

我想检查 WordPress 中是否安装了 Yoast SEO。我已经在我的测试环境中激活了 Yoast SEO,但它不起作用。

在Yoast的wp-seo-main.php中,第16行有这样一行:

define( 'WPSEO_VERSION', '3.4' );

所以我想,这是检查 Yoast 是否已安装并运行的好方法,所以我这样做了:

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

但它给了我“不,未定义”。多么奇怪,因为它应该被定义。

有人有主意吗?我完全没有主意了。

php wordpress plugins
8个回答
15
投票

或者没有额外的内含物、前端或后端:

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
}

适用于任何插件,只需查看您的插件文件夹即可找到您的目标:

plugin-folder/plugin-index-name.php
,后者应将插件详细信息驻留在文件顶部。

请检查文档。 参考


4
投票
$all_plugins = get_plugins();

//print_r( $all_plugins );

if( array_key_exists( 'woolementor/woolementor.php', $all_plugins ) ){

     //do something
}

3
投票

受到 Jonas Lundman 回答的启发,我写这篇文章也是为了在 Yoast 溢价生效时进行处理。

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;
}

2
投票

感谢所有其他人迄今为止所做的出色工作!但问题也是关于检查是否安装了插件,并且您所有的答案都只是关于检查插件是否处于活动状态。

所以我去了我的游乐场并开发了一个正确的检查,我想通过使用 WP 已经提供的功能向您展示。

// 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;
}

如您所见,我编写了两个单独的函数来检查

Yoast SEO
是否已安装并处于活动状态,因此我只需要调用它并检查返回参数:

$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!';
}

但是如果你想跳过这两个额外的函数,你可以直接调用这两个检查函数:

$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!';
}

我希望这可以帮助您进行良好的安装和主动检查!


2
投票

使用以下代码:

 <?php include_once ABSPATH . 'wp-admin/includes/plugin.php'; ?>
 <?php if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) ) :

      //do staff

 <?php endif; ?>

0
投票

这对我有用。

if(count(
        preg_grep(
            '/^wordpress-seo.*\/wp-seo.php$/',
            apply_filters('active_plugins', get_option('active_plugins'))
        )
    ) != 0
){
    // Plugin activated
}

0
投票

选中此项,以获取所有活动插件名称;

$callback = function($key){
    $key = explode('/', $key);
    return $key[0];                                        
}

$active_plugins = array_map($callback, get_option( 'active_plugins' ) );

0
投票

自 Wordpress 6.5 起,有一种更简单的方法可以根据需要制作另一个插件。

在主插件文件中添加以下行:

/**
 * Plugin Name: Express Payment Gateway Checkout for Shop
 * Requires Plugins: shop, payment-gateway
 */

请参阅以下链接: 在 WordPress 6.5 中引入插件依赖关系

© www.soinside.com 2019 - 2024. All rights reserved.