如何在检查变量的真实性后启用WordPress插件激活



只有在变量的条件为true后,我才能激活我开发的插件。变量数据是从数据库中提取的。

$results = get_option( 'stp_api_settings');
$test_result = $results['stp_api_text_field_0'];

现在,如果$test_result返回一个非零值,则true插件被激活;如果没有值,则显示一条false错误消息。

我正在尝试使用以下代码:

add_action( 'admin_init', 'check_for_register_key' );
function check_for_register_key()
{
$results = get_option( 'stp_api_settings');
$test_result = $results['stp_api_text_field_0'];
if ($test_result === true ) {
add_action( 'admin_notices', 'error_notice' );
deactivate_plugins( plugin_basename( __FILE__ ) ); 
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
function register_plugin_notice(){
?><div class="error"><p>The plugin is not registered, go to the options page and enter the activation 
key.</p></div><?php
}

但它不起作用:无论变量的值如何,插件在任何情况下都是注册的。请帮忙解决这个问题。

请尝试此解决方案。在插件激活之前,您需要使用register_activation_hook回调函数来进行一些逻辑表示

最新更新