如何从类插件显示ADMIN_NOTICES挂钩中的字符串

时间:2014-01-17 作者:danceoften

我试图在插件激活时在admin\\u notices hook中显示一条消息,在我的类中使用全局变量或静态变量($association\\u error)。我将变量值设置到register\\u activation\\u hook函数中:这一切都不起作用。

我对wordpress开发和php非常陌生,如果这是一个简单的问题,请原谅。此外,我发现通知只包含设置到installMenu()中的字符串,而不包含设置到installDb()中的字符串。最后,我的通知未正确定位到通知区域中,但即使使用div的update或error类,它也位于通知区域上方。

下面是我的类插件代码:

    <?php
    /*
    Plugin Name: danceoften-associati
    Plugin URI: http://danceoften.com
    Description: gestione degli associati
    Version: 1.0
    Author: daniele ziccardi
    Author URI: http://danceoften.com
    */
    class danceoften_associati {

        private $table_name;
        private static $associati_error;
        private static $associati_message;
        private static $instance;

        private function __construct()  
        {
            global $wpdb;
            $this->table_name = $wpdb->prefix . \'associati\';    
            add_action(\'admin_notices\', array($this, \'notifyMsg\') );        
            add_action(\'admin_menu\', array($this, \'installMenu\') ); 
            add_shortcode(\'associato\', array($this, \'shortCodeAssociato\'));  
            add_shortcode(\'associato_search\', array($this, \'shortCodeSearch\'));  
        }  

        public static function getinstance()
        {
            // Get an instance of the
            if( null == self::$instance ) {
                self::$instance = new self;
            } // end if
            return self::$instance;
        }

        public function register()
        {       
            $this->installDb();     
        }
        public function shortCodeAssociato($atts)  
        {  
        echo \'shortCodeAssociato\';
        }   

        public function shortCodeSearch($atts)  
        {  
        echo \'shortCodeSearch\';
        }   

        private function installDb() {
            try{
                global $wpdb;
                // check table NOT exists
                $sql  = "show tables like $this->table_name";
                if(!$wpdb->query($sql)){
                    //Create table
                    $sql  = "
        CREATE TABLE $this->table_name (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `status` enum(\'active\',\'draft\',\'passive\') NOT NULL DEFAULT \'draft\'      
        ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
        ";              
                    $res = $wpdb->query($sql);
                    //echo ( $wpdb->show_errors());
                    self::$associati_message .= \'Creata tabella\'. $this->table_name;
                }
                else{
                self::$associati_message .= \'Trovata tabella \'.$this->table_name.\': verificare contenuto\';
                }
            }       
            catch (Exception $e){
                self::$associati_error = \'exception \'.$e->getMessage();
            }
        }

        public function installMenu() {
            //this is the main item for the menu
            add_menu_page(\'associati\', //page title
            \'Associati\', //menu title
            \'manage_options\', //capabilities
            \'list_associati\', //menu slug
            array($this,\'listAll\') //function
            );

            //this is a submenu
            add_submenu_page(\'list_associati\', //parent slug
            \'Nuovo associato\', //page title
            \'Nuovo associato\', //menu title
            \'manage_options\', //capability
            \'new_associato\', //menu slug
            array($this,\'newAssociato\') //function
            );

            //this submenu is HIDDEN, however, we need to add it anyways
            add_submenu_page(null, //parent slug
            \'Modifica associato\', //page title
            \'Modifica associato\', //menu title
            \'manage_options\', //capability
            \'update_associato\', //menu slug
            array($this,\'updateAssociato\') //function
            );
            self::$associati_message .=\'Menu aggiunto<br />\';
        }

        public function notifyMsg() {
            self::$associati_message .= \' test \';
            if( isset(self::$associati_error)) {
                echo \'<div class="error">Attenzione: \',self::$associati_error,\'</div>\';
                //unset (self::$associati_error);
            }
            //else {echo \'<div class="error">noerror</div>\';}
            if( isset(self::$associati_message)) {
                echo \'<div class="update">\',self::$associati_message,\'</div>\';
                //unset (self::$associati_message);
            }
            //else{echo \'<div class="update">nomsg</div>\';}
        }

        function listAll () {
        // code here
        }   

        function updateAssociato () {
        //code here
        }

        function newAssociato () {
            //code here
         }
    }
    static $danceoften_associati_instance;
    $danceoften_associati_instance = danceoften_associati::getinstance();
    define(\'danceoften_associati_dir\', plugin_dir_path(__FILE__));
    register_activation_hook(__FILE__,array($danceoften_associati_instance,\'register\'));
    ?>
最后一个疑问是,我没有用静态工厂方法getInstance()正确处理实例化,但除了通知之外,现在一切都正常了。thanx提前通知任何想要帮助的人

2 个回复
SO网友:s_ha_dum

看看激活插件时会发生什么。也就是说,用HttpFox或Wireshark之类的工具观看活动。激活后,页面将重定向回自身$_GET 附加的参数。例如:

wp-admin/plugins.php?activate=true&plugin_status=all&paged=1&s=
注意,activate=true?. 我敢肯定,您的代码不起作用,因为涉及到两个请求。例如,您需要使用瞬态或cookie来补偿该重定向。您可以添加自己的$_GET 参数,但我没有时间研究和编造这些参数。

SO网友:Sepster

由于@s\\u ha\\u dum已正确识别,您在激活期间填充的变量不会持续存在,因为激活会导致新的页面请求。ie在调用admin\\u notices处理程序时,您的代码已经有效地重新启动。

因此,您需要一种机制来保持$associati_error 跨页面加载。

最简单的方法是使用内置的wordpressupdate_option()get_option() 来自的函数Wordpress Options API, 如下所示。这将在数据库中保留您的消息。

当完成写入此变量时(例如在register() 功能),使用:

  update_option(\'danceoften_associati_error\') = self::$associati_error;
然后在notifyMsg(), 使用:

 self::$associati_message = get_option(\'danceoften_associati_error\');
也许这可以消除$associati_message 变量完全是静态的。

结束

相关推荐