【问题标题】:Theme dependent WP plugins主题相关的 WP 插件
【发布时间】:2016-03-31 23:05:29
【问题描述】:

我正在创建一个 WP 插件,它需要两个主题中的任何一个,以便它可以使用从任何一个插件继承的主题功能。我写了这个条件检查。

register_activation_hook(__FILE__, 'plugin_activation_check');

add_action ('admin_init', 'plugin_activation_check');
function plugin_activation_check()
{
 if (get_template() != 'Theme-One' || get_template() !='Theme-Two'){
        deactivate_plugins('theme-folder/my-plugin.php');

  /* message to user when they try to activate the plugin */
    wp_die('This plugin needs either Theme-One or Theme-two themes to be active.');

}
}

我希望只有在两个推荐主题中的任何一个处于活动状态时才能激活该插件。否则,请停用该插件并返回错误消息。

我可以通过像这样编写 if 条件来轻松实现一个主题

if (get_template() != 'Theme-One'){

但不确定如何使用两个主题来实现这一点。

谢谢

【问题讨论】:

    标签: wordpress plugins


    【解决方案1】:

    您遇到的问题是您的条件将始终评估为真。

    'Theme-One' 和'Theme-Two' 不能同时激活,因此逻辑上存在缺陷。例如,如果我激活了“Theme-One”,那么get_template() !='Theme-Two' 为真,它满足您的第二个条件并触发deactivate_plugins('theme-folder/my-plugin.php');

    改变这个:

    if (get_template() != 'Theme-One' || get_template() !='Theme-Two'){
    

    到这里:

    if ( get_template() != 'Theme-One' && get_template() !='Theme-Two' ) {
    

    在更新的版本中,我们确保没有一个主题处于活动状态。

    如果主题的数量可能会增加,那么我建议改用数组。

    例子:

    $themes = array(
        'Theme-One',
        'Theme-Two',
        'Theme-Three',
    );
    
    if ( ! in_array( get_template(), $themes ) ) {
    

    【讨论】:

    • 非常感谢。我犯了这个愚蠢的错误真的很愚蠢。我确实需要一些咖啡。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多