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.
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.
- 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!) – TheDeadMedic Commented Apr 22, 2016 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 Commented Apr 22, 2016 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. – Pieter Goosen Commented Apr 22, 2016 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 – Mark Kaplun Commented Apr 22, 2016 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 Commented Apr 22, 2016 at 13:44
2 Answers
Reset to default 20I 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." );
}
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/reference/functions/add_settings_error/