Within a plugin class I have:

    private $notices = array();

Various functions within the class add notices to this array as array($classname,$message). Then, I’m expecting the following action to display them:

    function __construct(){
        // add notice
        add_action('admin_notices',array($this,'output_notices'),999);
    }

    public function output_notices(){
        if(!empty($this->notices)){
            foreach($this->notices as $notice){
                echo '<div class="notice is-dismissable notice-'.esc_attr($notice[0]).'"><p>'.esc_html($notice[1]).'</p></div>';
            }
        }
    }

But the array is always empty (yes, I’ve checked that manually filling it works). I’m guessing that either the admin_notices hook is called before there are any notices to display, or the $notices variable is being discarded before the hook is called at all. Is there a way to do this correctly?

Read more here: How can I generate admin notices from within a plugin?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: