为什么我的全局变量不能正确解析? [英] Why won't my global variables properly resolve?

查看:160
本文介绍了为什么我的全局变量不能正确解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的插件激活码

  $ classified_category_name ='classified'; 
$ credit_table_name ='credits';
$ credit_table_version = 0.1;

register_activation_hook(__ FILE__,'LBH_Classifieds_Activate');

函数LBH_Classifieds_Activate()
{
global $ wpdb;
global $ classified_category_name;
global $ credit_table_name;
全球$ credit_table_version;
$ table_name = $ wpdb->前缀。 $ credit_table_name;
if($ wpdb-> get_var(SHOW TABLES LIKE'$ table_name')!= $ table_name){
$ sql =CREATE TABLE。 $ table_name。 (
time bigint(11)DEFAULT 0 NOT NULL,
amount tinyint(3)DEFAULT 0 NOT NULL,
username varchar(50)NOT NULL,
UNIQUE KEY username用户名)
);;
require_once(ABSPATH。'wp-admin / includes / upgrade.php');
dbDelta($ sql);
}
add_option('lbh_db_version',$ credit_table_version);
}

但全局变量为空。



另外,有没有办法打印插件中的任何信息?我试着返回一个WP_Error,抛出一个WP_Error,而我所能得到的只是一个大的黄色框,大部分都是空的,插件无法被激活,因为它触发了一个致命错误。



当激活发生时,您的插件被包含在另一个函数中,然后您的myplugin_activate()将从该函数中调用(具体而言,在activate_plugin()函数中)插入点被激活的位置。因此,主体变量在activate_plugin()函数的作用域内,并且不是全局的,除非您明确声明了它们的全局作用域

关于可变范围,请参阅本说明的其余部分: http://codex.wordpress.org/Function_Reference/ register_activation_hook#A_Note_on_Variable_Scope


here is my plugin activation code

$classified_category_name = 'classified';
$credit_table_name = 'credits';
$credit_table_version = 0.1;

register_activation_hook(__FILE__, 'LBH_Classifieds_Activate');

function LBH_Classifieds_Activate()
{
    global $wpdb;
    global $classified_category_name;
    global $credit_table_name;
    global $credit_table_version;
    $table_name = $wpdb->prefix . $credit_table_name;
    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
            $sql = "CREATE TABLE " . $table_name . " (
                    time bigint(11) DEFAULT 0 NOT NULL,
                    amount tinyint(3) DEFAULT 0 NOT NULL,
                    username varchar(50) NOT NULL,
                    UNIQUE KEY username (username)
            );";
            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
            dbDelta($sql);
    }
    add_option('lbh_db_version', $credit_table_version);
}

but the global variables are empty.

Also, is there any way to print any information from within a plugin? I've tried returning a WP_Error, throwing a WP_Error, and all I can ever get is a big yellow box, mostly empty, with "Plugin could not be activated because it triggered a fatal error."

解决方案

When activation occurs, your plugin is included from another function and then your myplugin_activate() is called from within that function (specifically, within the activate_plugin() function) at the point where your plugin is activated. The main body variables are therefore in the scope of the activate_plugin() function and are not global, unless you explicitly declare their global scope

See the rest of this note on variable scope: http://codex.wordpress.org/Function_Reference/register_activation_hook#A_Note_on_Variable_Scope

这篇关于为什么我的全局变量不能正确解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆