Why won't my global variables properly resolve?

谁说胖子不能爱 提交于 2019-12-25 02:45:03

问题


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."


回答1:


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



来源:https://stackoverflow.com/questions/2397766/why-wont-my-global-variables-properly-resolve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!