i'm trying to display a message in admin_notices hook upon plugin activation, using both a global variable or a static variable in my class ( $associati_error). i set the variable value into the register_activation_hook function: none of that is working.

i'm very new to wordpress development and php, so excuse me if this is a simple question. also i found that the notification contains only the string set into installMenu() and not the string set into installDb(). finally my notification is'nt properly positioned into the notification area but is above it even using the update or error class for the div.

here is my class plugin code :

    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()
        public function shortCodeAssociato($atts)  
        echo 'shortCodeAssociato';

        public function shortCodeSearch($atts)  
        echo 'shortCodeSearch';

        private function installDb() {
                global $wpdb;
                // check table NOT exists
                $sql  = "show tables like $this->table_name";
                    //Create table
                    $sql  = "
        CREATE TABLE $this->table_name (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `status` enum('active','draft','passive') NOT NULL DEFAULT 'draft'      
                    $res = $wpdb->query($sql);
                    //echo ( $wpdb->show_errors());
                    self::$associati_message .= 'Creata tabella'. $this->table_name;
                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__));

the last doubt is that i'm not handling instantiation correctly with the static factory method getnstance() but except for notification everything seems working by now. thanx in advance to anyone wishing to help

  • What have you done to debug or to diagnose the issue? Which callbacks/functions work as expected, and which ones don't? Where in the process does the failure occur? If you move the hook out of the class does it work as intended/expected? Jan 17, 2014 at 13:48
  • all functions work as expected, except the value set into installDB() is'nt seen on the screen during notifyMsg() do you think it's correct to set a static variable into the activation_hook execution scope ?
    – danceoften
    Jan 17, 2014 at 14:15

2 Answers 2


Take a look at what happens when you activate a plugin. That is, watch the event with something like HttpFox or Wireshark. After activation the page is redirected back to itself with $_GET parameters appended. For example:


Notice, activate=true?. Your code isn't working, I am pretty sure, because there are two requests involved. You will need to compensate for that redirect with, for example, a transient or cookie. You may be able to add your own $_GET parameter but I do not have the time to research and cook up any of that.

  • unfortunately your answer says nothing to me.
    – danceoften
    Jan 17, 2014 at 16:24

As @s_ha_dum has correctly identified, your variables populated during activation don't persist because activation causes a new page request. ie your code has effectively re-started by the time your admin_notices handler is called.

So you need a mechanism to persist your $associati_error across page loads.

The easiest is to use wordpress built-in update_option() and get_option() functions from the Wordpress Options API, as follows. This will persist your messages in the database.

When finished writing to this variable (eg at end of your register() function), use:

  update_option('danceoften_associati_error') = self::$associati_error;

And then at the start of notifyMsg(), use:

 self::$associati_message = get_option('danceoften_associati_error');

And perhaps this may remove the need for the $associati_message variable to be static at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.