admin_notices not displaying in plugin

I know I’m probably doing something dumb here but I just can’t get this to run. I’m trying to set up a little API for my plugin and to create a class to display admin notices a little easier. Here’s what I have:

// Send data to class to get HTML for admin notice
$efpd=Efpdd::getInstance();
$plugin_update = $efpd->efpd_admin_notice(
    $notice_info = array(
        'type' => 'update',
        'message' => 'The plugin has just been updated.',
        'button' => 'Click for details'
    )
);
//wp_die(var_dump($plugin_update)); // Testing output of admin notice HTML code
add_action('admin_notices',function(){echo $plugin_update;});

And in my class, there is this function:

Read More
public function efpd_admin_notice($data=array()){
    extract($data); // Extracts $message, $type, and $button from $data array
    if(empty($message) && !empty($type)){ // If no message was passed through the $data array, create one based on the type of notice passed, also begin the HTML output here
        switch($type){
            case 'error':
                $message = 'There's been an error!';
                $return = "<div id="message" class="error">n";
            break;
            case 'update':
                $message = 'There's been an update!';
                $return = "<div id="message" class="updated">n";
            break;
            default:
                $message = 'There's something wrong with your code...';
                $return = "<div id="message" class="error">n";
            break;
        }
    }
    if(empty($button)) $button = 'Click Here';
    $return .= "    <p style="float: left;">{$message}</p>n";
    $return .= "    <p style="float: left;"><a href="{$settings_url}&amp;clear_cache=y">{$button}</a></p>n";
    $return .= "</div>n";
    return $return;
}

So I guess I’m asking, what am I doing wrong to get this admin notice to not show? Is there a workaround to get this to work? Thanks.

Related posts

Leave a Reply

2 comments

  1. Try this. I cleaned up your arg array and placed everything in a function. Also, why are you using the getInstance method when your efpd_admin_notice is public? See the code below for accessing this method properly.

    function plugin_update(){
        $plugin_update = Efpdd::efpd_admin_notice(array(
            'type' => 'update',
            'message' => 'The plugin has just been updated.',
            'button' => 'Click for details'
        );
        echo $plugin_update;
    );
    add_action('admin_notices', 'plugin_update');
    
  2. When using Multi-Site, you should do both:

    add_action('admin_notices',         'your_function');
    add_action('network_admin_notices', 'your_function');
    

    btw, $plugin_update variable u used inside function, will have NULL value, because it was defined outside function.