4

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
    $valid = ( 'correct_value' == $new_value );

    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
      $valid = ( 'correct_value' == $_POST['my_var'] );
      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, I do see the notification now. 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.

3

After digging in the setting API I found the answer. Method 1 was correct, but the notification should not be done by hooking to admin_notices, rather by the function add_settings_error

add_action ('pre_update_option_my_var', function( $new_value, $old_value) {
    //Do my validation
    $valid = ( 'correct_value' == $new_value );

    if ( !$valid ) {
         //This fixes the issue
         add_settings_error( 'my_var', $error_code, $message );
    }
    return ($valid)? $new_value : $old_value;
});

In order to display them, this also needs to be added at the beginning of the settings page:

<h2>The heading of my settings page</h2>
<?php settings_errors(); ?>
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
}
  • How does this apply to my case? I don't see any option being save or the $_POST array used? – DavidTonarini Jan 13 '18 at 13:34
  • I just showed the general idea how to organize admin notices. – KAGG Design Jan 13 '18 at 13:41
  • From your question it is impossible to understand what do you expect in $_POST values, how do they relate to saved options and where do you try to read these values. – KAGG Design Jan 13 '18 at 13:43
  • I updated the code. I need the notice to be displayed in case a wrong value is passed in the settings. I don't know if when a setting page is updated the values are passed through $_POST. Apparently not. But I have no idea how to access them (basically I need to be able to know when the settings are updated and how to access the equivalent of $new_value and $old_value from the first example) – DavidTonarini Jan 13 '18 at 14:24
  • WordPress never sends values via $_POST. It just saves them in options. – KAGG Design Jan 13 '18 at 14:25
1

I run into this same challenge a couple of months ago. I needed to show admin notices based on certain validations I had with some custom fields in a custom post type.

Because my validation was in a pre_save hook, the problem is that "the browser refreshes after that hook fires."

So I found a class by John Oleksowicz that basically stores the admin notices on the options table and clears it when not needed.

It's called: Easy WordPress Admin Notifications

The class is pretty straight forward and you can just use the plugin. In my case, I used the class and adapted it by changing the way to call the admin messages functions.

So now, any time I need to trigger an admin notice I just do:

planner_notices_add_error( $message );
0

We wrote small library for Wordpress admin notices: https://gitlab.com/wpdesk/wp-notice

Now displaying admin notice is as simple as creating new object:

$notice = new \WPDesk\Notice\Notice( 'info', 'Notice text goes here' );

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.