Make WordPress Core

Opened 7 years ago

Closed 5 years ago

Last modified 19 months ago

#40173 closed feature request (wontfix)

New function wp_admin_notice

Reported by: sebastianpisula's profile sebastian.pisula Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Administration Keywords:
Focuses: Cc:

Description

I suggest add this function to core WP:

<?php
/**
 * @param string|array $messages
 * @param string $status Status of message. Options:  info, warning, success, error
 * @param bool $is_dismissible
 * @param bool $display
 *
 * @return string
 */
function wp_admin_notice( $messages, $status = 'error', $is_dismissible = false, $display = false ) {
        if ( ! in_array( $status, array( 'info', 'warning', 'success', 'error' ) ) ) {
                $status = 'error';
        }
        $classes = array( 'notice', 'notice-' . $status );
        if ( $is_dismissible ) {
                $classes[] = 'is-dismissible';
        }
        if ( is_array( $messages ) ) {
                $messages = implode( "<br/>\n", $messages );
        }
        $html = '<div class="' . implode( ' ', $classes ) . '"><p>' . $messages . '</p></div>';
        if ( $display ) {
                echo $html;
        }
        return $html;
}

For example we have this line: wp-admin/plugin-editor.php:182:

<div id="message" class="updated notice is-dismissible"><p><?php _e('File edited successfully.') ?></p></div>

We can use this function:

<?php
wp_admin_notice( __('File edited successfully.'), 'success', true, true);

Authors of plugin will be can use this function in plugins.

Change History (3)

#1 in reply to: ↑ description @obenland
7 years ago

The existing API for admin notices shoulders all of that for you already:

<?php
add_action( 'admin_init', function() {
    add_settings_error( 'unique_key', 'warning', __( 'Message' ), 'updated'        ); // Or 'notice-warning'
    add_settings_error( 'unique_key', 'error',   __( 'Message' ), 'error'          ); // Or 'notice-error'
    add_settings_error( 'unique_key', 'info',    __( 'Message' ), 'notice-info'    );
    add_settings_error( 'unique_key', 'success', __( 'Message' ), 'notice-success' );
} );

add_action( 'all_admin_notices', function() {
     settings_errors( 'unique_key' );
} );

We could definitely do a better job documenting that behavior however and surfacing these possibilities.
What do you think?

Last edited 7 years ago by obenland (previous) (diff)

#2 @andraganescu
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

For lack of response and because this is handled already by the API for admin notices, will close this.

Note: See TracTickets for help on using tickets.