1

我已经安装了 SMS Validator,所以注册到我网站的每个人都必须输入那里的电话号码才能注册

现在我创建了一个新功能(链接),如果用户通过访问此链接注册,则添加不同类型的用户角色: http ://example.com/wp-login.php?action=register&role=vip_member

我只想为此 URL 链接关闭 SMS Validator。

有可能以某种方式做到吗?


到目前为止使用 mathielo 代码的成功:

// Activate SMSGlobal 
function activate_plugin_conditional() {
if ( !is_plugin_active('sms-validator-SMSG/sms-validator.php') ) {
    activate_plugins('sms-validator-SMSG/sms-validator.php');
    }
}

// Deactivate SMSGlobal
function deactivate_plugin_conditional() {
if ( is_plugin_active('sms-validator-SMSG/sms-validator.php') ) {
    deactivate_plugins('sms-validator-SMSG/sms-validator.php');
    }
}

// Now you in fact deactivate the plugin if the current URL matches your desired URL
if(strpos($_SERVER["REQUEST_URI"], '/wp-login.php?action=register&role=vip_member') !== FALSE){
// Calls the disable function at WP's init
    add_action( 'init', 'deactivate_plugin_conditional' );
}

if(strpos($_SERVER["REQUEST_URI"], '/wp-login.php?action=register&role=seller') !== FALSE){
// Calls the enable function at WP's init
    add_action( 'init', 'activate_plugin_conditional' );
}

if(strpos($_SERVER["REQUEST_URI"], '/wp-login.php?action=register&role=provider') !== FALSE){
// Calls the enable function at WP's init
    add_action( 'init', 'activate_plugin_conditional' );
}

到目前为止,此代码可帮助我在这 3 个选定的 URL 中激活和停用此插件。但如果在此链接中停用此插件,我希望激活此插件:/wp-login.php/wp-login.php?action=register

但是,如果我将其设置为在 URL:/wp-login.php 上激活,那么它不会在 URL:/wp-login.php?action=register&role=vip_member 上被停用,我需要将其停用。

4

1 回答 1

2

您可以使用以下方式匹配当前 URL $_SERVER["REQUEST_URI"],然后禁用插件functions.php

// Just creating the function that will deactivate the plugin
function deactivate_plugin_conditional() {
    if ( is_plugin_active('plugin-folder/plugin-name.php') ) {
        deactivate_plugins('plugin-folder/plugin-name.php');    
    }
}

// Now you in fact deactivate the plugin if the current URL matches your desired URL
if(strpos($_SERVER["REQUEST_URI"], 'my-disable-url') !== FALSE){
    // Calls the disable function at WP's init
    add_action( 'init', 'deactivate_plugin_conditional' );
}

注意:请注意,如果 needle 在位置零与给定字符串匹配,则phpstrpos()可能会返回,因此需要条件。0!==

从这里获得了我的参考资料并实施了 URL 检查。查看$_SERVER["REQUEST_URI"]所需 URL 处的当前值以获得完美匹配。


回答你问题的第二部分:

您可以改进您的代码,删除最后 2if秒并用 补充第一个else,如下所示:

// Now you in fact deactivate the plugin if the current URL matches your desired URL
if(strpos($_SERVER["REQUEST_URI"], '/wp-login.php?action=register&role=vip_member') !== FALSE){
    // Calls the disable function at WP's init
    add_action( 'init', 'deactivate_plugin_conditional' );
}
// Otherwise, for any other URLs the plugin is activated if it's currently disabled.
else{
    add_action( 'init', 'activate_plugin_conditional' );
}

现在,对于不是您希望停用插件的每个URL,如果该插件当前处于非活动状态,您的代码将启用该插件。

于 2013-09-15T06:50:01.583 回答