Wordpress Setting Flashdata Error

2019-05-22 19:57发布

I've done a bunch of work with codeigniter, cakephp, zend, magento and they all have methods for setting what I've heard called flashdata errors.

see:

This is basically a value set in session that every page looks for and if found displays then clears from the session so that it is only displayed once.

Typically these are displayed in the form of a popup or small short lived box that is color coded depending on the level.

Ex: message : blue success : green warning : yellow error : red

Is there something like this within wordpress where you can for instance do some business logic through a post to say "wp_ajax" or "wp_ajax_nonpriv" and if something breaks redirect to the previous page after adding a flash data error?

1条回答
beautiful°
2楼-- · 2019-05-22 20:24

If you want to display flash messages within the wp-admin area, you can add an action to admin_notices hook. It would work like this (taken from here and here):

// Adds the action to the hook
add_action( 'admin_notices', 'your_custom_function' );

function your_custom_function() {
    ?>
    <div class="updated">
        <p><?php _e( 'Updated!', 'my-text-domain' ); ?></p>
    </div>
    <?php
}

As for the CSS classes, looks like there are few options:

The class "updated" will display the message with a white background and a green left border (used to be a yellow background before WordPress 3.8.).

The class "error" will display the message with a white background and a red left border (used to be a red background before WordPress 3.8.).

The class "update-nag" will display the message with a white background and a yellow left border. In addition, the message will be moved above the page title (<h2>).

But that shouldn't keep you from creating your own CSS classes and using them.


If you want to display the messages outside wp-admin, you might need to develop your own solution or get user generated solutions. Unfortunately WP doesn't have a built-in functionality for that as it may vary from theme to theme.

查看更多
登录 后发表回答