为什么activate_plugin在register_activation_hook中不起作用


10

我正在尝试在激活第一个插件时自动激活第二个插件。

register_activation_hook(__FILE__, 'example_activation' );
function example_activation() {
        include_once(ABSPATH .'/wp-admin/includes/plugin.php');
        activate_plugin('hello.php');
}

它不能在register_activation_hook内部工作。如果我直接使用它,则它的工作方式如下:

include_once(ABSPATH .'/wp-admin/includes/plugin.php');
activate_plugin('hello.php');

我该如何解决?感谢帮助

解:

我现在为自己使用它:

// When this plugin activate, activate another plugin too.
register_activation_hook(__FILE__, function(){
    $dependent = 'hello.php';
    if( is_plugin_inactive($dependent) ){
        add_action('update_option_active_plugins', function($dependent){
            /* for some reason, 
            activate_plugin($dependent); 
               is not working */
            activate_plugin('hello.php');
        });
    }
}); 

// When this plugin deactivate, deactivate another plugin too.
register_deactivation_hook(__FILE__, function(){
    $dependent = 'hello.php';
    if( is_plugin_active($dependent) ){
        add_action('update_option_active_plugins', function($dependent){
            deactivate_plugins('hello.php');
        });
    }
}); 

我原本误读了您的帖子。我必须说,它的格式正确。这个功能在您的插件主程序中吗?
克里斯(Chris)

等一下。需要编辑我的帖子。
克里斯(Chris)

是它在试图激活第二插件的第一个插件..第二插件是hello.php为例
Ünsal科尔克马兹

Answers:


8

有关正在发生的事情的完整说明,请参阅这篇文章(这是用于停用插件,但问题是相同的)。

简要说明:插件实际上是通过将插件添加到数据库中存储的活动插件阵列中来激活的。当您激活第一个插件时,WordPress检索所有当前活动插件的数组,将插件添加到其中(但尚未更新数据库),然后运行安装回调。

此安装回调将运行您的代码。

之后,WordPress使用上面的数组更新数据库,该数组包含第一个但包含第二个插件。因此,您的第二个插件似乎没有被激活。

解决方案:在上面的链接中已经提到解决方案是这样的(未经测试):

//This goes inside Plugin A.
//When A is activated. activate B.
register_activation_hook(__FILE__,'my_plugin_A_activate'); 
function my_plugin_A_activate(){
    $dependent = 'B/B.php';
    if( is_plugin_inactive($dependent) ){
         add_action('update_option_active_plugins', 'my_activate_dependent_B');
    }
}

function my_activate_dependent_B(){
    $dependent = 'B/B.php';
    activate_plugin($dependent);
}

如果您有时间,我可以编辑问题以写下我现在正在使用的内容。您可以看到activate_plugin($ dependent); 激活插件时不起作用。任何想法如何解决?
Ünsal科尔克马兹

将您的代码复制到测试插件中(并创建另一个测试hello.php测试插件-对我来说很好)。请记住,如果插件位于子目录中,则需要提供文件夹:例如hello/hello.php
史蒂芬·哈里斯

我的代码将工作正常..问题开始当你改变activate_plugin('hello.php');符合activate_plugin($dependent);
Ünsal科尔克马兹

这是因为提供给您的回调的变量不是hello.php而是当前活动插件的数组。您不能将自己的变量传递给这样的回调。除非您使用类/全局变量,否则将需要使用activate_plugin('hello.php');。但这不应该是一个问题...?
史蒂芬·哈里斯

好吧,我不喜欢在很多地方写相同的变量。但没什么大不了的。THX很多
Ünsal科尔克马兹

2

这是一个解释为什么对他不起作用的人,以及他如何必须复制activate_plugin方法并创建自己的自定义方法而不会出现问题:https : //stackoverflow.com/questions/1415958/how-to-在内部激活wordpress-plugins

这是activate_plugin代码的原始来源,可用于创建自己的功能并查看不适用于您的地方:http : //hitchhackerguide.com/2011/02/11/activate_plugin/

这是一个可能的解决方案,其中包含其他人可能会从该源代码中窃取的另一种插件激活方法:如何通过代码激活插件?


该解决方案还没有在register_activation_hook工作
Ünsal科尔克马兹

幸运的是,既然您拥有自己的激活插件的方法,那么您应该能够对其进行调试,并弄清楚它出了故障的地方吧?:)您是否更改了register_activation_hook以使用自定义的activate_plugin方法而不是默认方法?如果是这样,则抛出一些trigger_error语句并找出失败的地方。例如,一个好的开始是找出方法失败时返回的内容。
mltsy 2012年

我尽力调试,但找不到原因。也许active_plugins选项在执行register_activation_hook之后得到更新。
Ünsal科尔克马兹

抱歉,我刚刚确定了交换的两个链接的顺序(最后两个)。您是否正在使用标题为“如何通过代码激活插件”的链接中的代码?另一个有用的提示是默认activate_plugin方法的返回值。您可以使用trigger_error查找返回值,并将其发布在此处。
mltsy
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.