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 :
<?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'));
?>
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