Reputation: 1581
I created a custom plugin. What I try to do is to put inside a class "Hooks" create methods for add_actions with a callback parameter.
The reason I am to that is to call easy a WP hook and to store all WP hooks in one file and not repeat add_actions in a code.
How can I do this correctly? Now the admin_notice don't display.
namespace Classes;
if( ! defined ('ABSPATH')){ die;} // when user try to direct access the file
class Hooks
{
private $_notice;
private $_wploaded;
private $_init;
private $_Class;
//admin_notices Prints admin screen notices.
public function admin_notices($Class,$call){
$this->_notice = $call;
$this->_Class = $Class;
add_action( 'admin_notices', array( $this->_Class, $this->_notice ) );
}
//wp_loaded After WordPress is fully loaded
protected function wp_loaded($call){
$this->_wploaded = $call;
add_action( 'wp_loaded', array( $this, $this->_wploaded ) );
}
//init
protected function init($call){
$this->_init = $call;
add_action( 'init', array( $this, $this->_init ) );
}
}
Then another class
namespace Classes;
if( ! defined ('ABSPATH')){ die;} // when user try to direct access the file
class Core
{
public $TypeNotice;
public $message;
// get Message
public function set_Message( $message ){
$this->message = $message;
}
// show admin notice based on get Message method
public function show_Message($TypeNotice){
$this->TypeNotice = $TypeNotice;
$Hooks = new Classes\Hooks();
switch ($this->TypeNotice) {
case 'Error':
return $Hooks->admin_notices("Classes\Core","show_Message") . '<div class="error notice is-dismissable"><p>'.$this->message.'</p></div>';
break;
default:
return $Hooks->admin_notices("Classes\Core","show_Message") . '<div class="notice notice-success is-dismissible"><p>'.$this->message.'</p></div>';
break;
}
}
}
$plugin = new Classes\Core();
$plugin->set_Message("Hi");
$plugin->show_Message("Error");
Upvotes: 0
Views: 4666
Reputation: 1581
I found a solution to do better!
class Notices
{
private $_admin_notice = "admin_notices"; // name of action
private $_message;
// 1. get Message
public function set_Message( $message ){
$this->_message = $message;
}
// 2. define add_action admin notices for method error
public function show_Error(){
add_action( $this->_admin_notice, array( $this, 'set_error_Message') );
}
// 2a. define add_action admin notices for method notice
public function show_Notice(){
add_action( $this->_admin_notice, array( $this, 'set_notice_Message') );
}
// 2b. define add_action admin notices for method notice
public function show_Warning(){
add_action( $this->_admin_notice, array( $this, 'set_warning_Message') );
}
// 3. show admin error based on get Message method
public function set_error_Message(){
echo '<div class="notice notice-error is-dismissable"><p>' . $this->_message . '</p> </div>';
}
// 3a. show admin notice based on get Message method
public function set_notice_Message(){
echo '<div class="notice notice-success is-dismissible"><p>' . $this->_message . '</p></div>';
}
// 3b. show admin warning based on get Message method
public function set_warning_Message(){
echo '<div class="notice notice-warning is-dismissible"><p>' . $this->_message . '</p></div>';
}
}
Then initialization the class
$notice = new Notices();
$notice->set_Message("Hi");
$notice->show_Notice();
Upvotes: 1