我试图在插件激活时在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提前通知任何想要帮助的人