How to display an admin notice after updating plugin settings?

by DavidTonarini   Last Updated January 13, 2018 13:08 PM

I have a plugin with a setting page, and there are some action that have to be done when a certain option is saved. I am using the pre_update_option_ hook to do this. So far so good. If something goes wrong, however, I'd also need to notify that to the user. I tried these things:

1) Add hook to admin_notices before updating the code

add_action ('pre_update_option_my_var', function( $new_value, $old_value) {
    //Do my validation
    if ( !$valid ) add_action('admin_notices', 'my_notification_function' )
    return ($valid)? $new_value : $old_value;
});

The validation works, but the notification is not displayed because, I assume, by then the functions hooked to admin_notices have already been executed?

2) Validation in the function hooked to admin_notices To solve the problem above, I had this idea.

add_action('admin_notices', function () {
      //Do my validation
      if (!$valid) { /* Display error message */ }
      //Store the value of $valid
});

add_action ('pre_update_option_my_var', function( $new_value, $old_value) {
    //fetch the value of $valid which I stored 
    return ($valid)? $new_value : $old_value;
});

Now, this would seem to work well, the problem is that for some strange reason I don't see the posted values. I tried to print $_POST and it always empty! Probably WordPress is passing the values in some other way? If so, how?

Which one is the right way to go, and how can I fix the issue? Of course, if any other method is better than these two and solves the issue, it's welcome.



Answers 1


No, you are doing wrong. add_action must be in __construct() of your plugin object. Here is the excerpt from my small plugin WOOF by Category, you can donload the whole code from wordpress.org:

public function __construct() {
    //...
    add_action( 'admin_notices', array( $this, 'wbc_plugin_not_found' ) );
    //...
}

In my code I check a condition (validation - in your case) and return result via wbc_requirements_met(). If result is false, I add notification message with wbc_admin_message

public function wbc_plugin_not_found() {
    if ( $this->wbc_requirements_met() ) {
        return;
    }

    $message = __( 'WOOF by Category plugin requires the following plugins installed and activated: ', 'woof-by-category' );
    //...
    $this->wbc_admin_message( $message, 'error notice is-dismissible' );
    }

private function wbc_admin_message( $message, $class ) {
    ?>
    <div class="<?php echo esc_attr( $class ); ?>">
        <p>
            <span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">
            <?php echo wp_kses( $message, wp_kses_allowed_html( 'post' ) ); ?>
            </span>
        </p>
    </div>
    <?php
}
KAGG Design
KAGG Design
January 13, 2018 12:50 PM

Related Questions


add_settings_error on validating plugin options API

Updated August 17, 2017 13:08 PM




Plugin Options Won't Get Saved

Updated March 10, 2018 20:08 PM