12

So there is the following case.

I need to display a name inside of admin_notices.

class MyPlugin_Admin {
    public static function render_admin_notice() {
        echo $name . ' has been upgraded.';
    }
}

add_action( 'admin_notices', array( 'MyPlugin_Admin', 'render_admin_notice' ) );

How to populate $name?

I thougth of following solutions:

No. 1:

class MyPlugin_Admin {

    public static $name;

    public static function render_admin_notice() {
        echo self::$name . ' has been upgraded.';
    }
}

MyPlugin_Admin::$name = 'John Doe';

add_action( 'admin_notices', array( 'MyPlugin_Admin', 'render_admin_notice' ) );

No. 2:

$name = 'John Doe';

add_action('admin_notices', function() use ($name){ 
    echo $name . ' has been upgraded.'; 
});

I don't like both somehow, since No. 1 requires $name to be populate class wide and therefor might lead to confusion and No. 2 requires at least PHP 5.3.

5
  • Could you elaborate on why you need to pass a variable? Perhaps explain what you are ultimately trying to do (it will help us determine the best solution - it might even be the case that you're looking at this from the wrong angle!) Apr 22 '16 at 10:48
  • I need to display a success message on top of the page. The message needs to be personalized e.g. John Doe has been promoted. How do I pass the name into the function which is registered on the admin_notices hook.
    – Aley
    Apr 22 '16 at 11:03
  • Downvoted, PHP 5.5 will finally be EOL'ed in July, so I cannot see what your problem actually is here. Approach 2 solves the issue, is valid, stick with it and go with it. Apr 22 '16 at 12:00
  • As someone that released by mistake an update to a relatively popular plugin that broke on 5.2, I am willing to testify that the uproar was much smaller then might have been expected and we ended with changing the FAQ instead of changing the code. My conclusion is that either the people on 5.2 just don't upgrade at all, or upgrade the php is so easy they don't bother to complain. just my 2 cents Apr 22 '16 at 12:16
  • 2
    @PieterGoosen Your argumentation is not really worth a downvote. I agree with you that not using No. 2 because of the backwards compatibility issues it might produce is not always a good point, but still sometimes people care.
    – Aley
    Apr 22 '16 at 13:44
15

I think a better implementation would be a "message" class e.g.:

class WPSE_224485_Message {
    private $_message;

    function __construct( $message ) {
        $this->_message = $message;

        add_action( 'admin_notices', array( $this, 'render' ) );
    }

    function render() {
        printf( '<div class="updated">%s</div>', $this->_message );
    }
}

This allows you to instantiate the message at any time prior to rendering:

if ( $done ) {
    new WPSE_224485_Message( "$name has been upgraded." );
}
4
0

Can be done much more straight forward:

$message = $name . ' has been upgraded.';

add_settings_error( 'my_admin_notice', 'my_admin_notice', $message, 'updated' );

Read more on https://developer.wordpress.org/reference/functions/add_settings_error/

2
  • The problem with this that this function is not for this purpose. It's definition says: "Part of the Settings API. Use this to show messages to users about settings validation problems, missing settings or anything else." So this is related to Settings API. Generally a good habit to avoid using functions/hooks, etc. for out-of-their-scope issues, for other than they been designed for. (for example this function's name can be misleading for other developers.) Cheers Jul 9 '20 at 12:58
  • Hi Viktor thanks for the feedback :) Who said the Settings API is not for plugins? It is in fact the recommended way of handling plugin settings since a long time. I agree add_settings_error is a confusing name but the last parameter $type clearly shows that it is also intended to handle 'success', 'warning' and 'info' messages, not only 'error'...
    – RavanH
    Jul 11 '20 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.