1

In WordPress's backend, you can add notifications and errors using HTML that looks like this

<div class="updated error">
    <p><?php esc_html_e( 'A bad thing happened!', 'your-text-domain' );?></p>
</div>    

You can add these notices using the admin_notices hook.

add_action( 'admin_notices', function(){
    ?>
        <div class="updated error">
            <p><?php esc_html_e( 'A bad thing happened!', 'your-text-domain' );?></p>
        </div>         
    <?php
} );

Does WordPress have a mechanism, or is there a "generally considered good" third party practice, that would allow you to set a "one time" notice? The scenario I'm thinking of is

  1. User posts a form
  2. For handling code does stuff, notifies user
  3. New page loads with notice
  4. If users reloads or re-navigates to page (back button), message does not display again

Other application frameworks I've used have a session abstraction to handle things like this. I'm curious if WordPress has something similar, or if there's a generally accepted way to do this, or if WordPress plugins Just Don't Do This™.

1
  • Here's a logic I am following for my plugins. I assign an option for all notices update_option(), now when I need to add an admin notice after a specific operation with code, I update that option, when the WP admin loads, I call admin_notices which pulls everything currently in the option, and at shutdown just clear everything. let me know if that was not clear I can answer with a code sample. Commented Sep 27, 2016 at 18:54

1 Answer 1

3

The idea is that you need to save_errors or update the option that has the errors/notices whenever you want. As soon as its output once by admin_notices it will be cleared.

/**
 * Sample_Notice_Handling
 */
class Sample_Notice_Handling {

    public static $_notices  = array();

    /**
     * Constructor
     */
    public function __construct() {

        add_action( 'admin_notices', array( $this, 'output_errors' ) );
        add_action( 'shutdown', array( $this, 'save_errors' ) );
    }

    /**
     * Add an error message
     */
    public static function add_error( $text ) {
        self::$_notices[] = $text;
    }

    /**
     * Save errors to an option
     */
    public function save_errors() {
        update_option( 'custom_notices', self::$_notices );
    }

    /**
     * Show any stored error messages
     */
    public function output_errors() {
        $errors = maybe_unserialize( get_option( 'custom_notices' ) );

        if ( ! empty( $errors ) ) {

            echo '<div id="mc_errors" class="error notice is-dismissible">';

            foreach ( $errors as $error ) {
                echo '<p>' . wp_kses_post( $error ) . '</p>';
            }

            echo '</div>';

            // Clear
            delete_option( 'custom_notices' );
        }
    }

}
1
  • +1 That's an interesting technique -- thank you for sharing. Commented Sep 27, 2016 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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