【问题标题】:WordPress: Check if Plugin is installedWordPress:检查插件是否已安装
【发布时间】:2016-11-30 09:45:32
【问题描述】:

我想检查一下 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>';
}

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

有人有想法吗?我完全没有想法。

【问题讨论】:

  • 请记住 wordpress.stackexchange.com,您的问题可能更适合那里
  • @YakovL 是的,但我认为这更像是一个与 PHP 相关的东西,但事实证明它有 wordpress 函数。
  • 请查看我的功能。它包含很多使用 wp 函数进行良好且稳定的检查的内容!
  • 这里的相关问题是Yoast是安装并运行还是只是“安装”?后者我想知道我们什么时候需要知道插件文件夹中是否有一个非活动插件?所以我想在问题“......并且正在运行”中写的是我们应该给出的答案.

标签: php wordpress plugins


【解决方案1】:

或者没有额外的包含,前端或后端:

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
}

适用于任何插件,只需查看您的 plugins 文件夹 以找到您的目标:plugin-folder/plugin-index-name.php 后者应该在文件顶部驻留插件详细信息。

请检查文档。 reference

【讨论】:

  • 不正确!!!!这将检查插件是否处于活动状态。如果它处于非活动状态但已安装,则不会出现在此处。
  • @Mr.Jo 问题不仅仅是“stackoverflow 标题”。由于主题上下文搜索解决方案是“检查 Yoast 是否已安装并运行的好方法......”因此答案是正确的。
  • 是的,检查它是否处于活动状态是正确的,但不能检查它是否已安装。我将在下面发布正确答案以检查正确的方法...
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    所以我去了我的游乐场并开发了一个正确的检查,我想通过使用 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!';
    }
    

    我希望这可以帮助您做好安装和主动检查!

    【讨论】:

      【解决方案4】:

      使用下面的代码:

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

      【讨论】:

      • 这只是已接受答案的副本,这是 2 年后添加的。
      【解决方案5】:

      这对我有用。

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

      【讨论】:

        猜你喜欢
        • 2017-03-28
        • 2013-03-24
        • 2014-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多