Make WordPress Core

Opened 4 hours ago

Last modified 2 hours ago

#66161 assigned defect (bug)

Improve the way the 'settings_errors' admin notices are built and printed out

Reported by: afercia Owned by: afercia
Priority: normal Milestone: 7.2
Component: Administration Version:
Severity: normal Keywords: has-patch
Cc: Focuses: accessibility

Description

The function settings_errors() is responsible for building and display the settings errors registered by add_settings_error(). That is: the admin notices in the Settings pages. The name of the function may be misleding, as the admin notices can be of various types e.g. error, success, warning, info.

This function loops over the registered settings errors, builds the HTML and then echoes it.

While that works OK, it's a little legacy. Today, the WP admin has a more standardized way to render the admin notices by teh means of wp_admin_notice().

I'd like to propose to refactor settings_errors() so taht it uses internally wp_admin_notice(). This would not just be code modernization. It would also help using the existing hooks related to the admin notices more consistently.

In fact, wp_admin_notice() fires the action hook:

do_action( 'wp_admin_notice', $message, $args );

That would help the ongoing effort in #50486 to collect all the admin notices. The hook allows to retrieve the message and all the args used by the admin notice, which is a rpe-requisites for #50486.

Change History (4)

This ticket was mentioned in PR #13669 on WordPress/wordpress-develop by @afercia.


4 hours ago
#1

  • Keywords has-patch added

Trac ticket: https://core.trac.wordpress.org/ticket/66161

Use of AI Tools

None

@afercia commented on PR #13669:


4 hours ago
#2

Cc @pattonwebz

#3 @afercia
4 hours ago

  • Owner set to afercia
  • Status newassigned

To test https://github.com/WordPress/wordpress-develop/pull/13669:

  • Go to a Settings page and save.
  • After page reload, observe the success notices is printed at the top of the page.
  • Observe the notice markup is equivalent to the one from the current implementation in trunk.
  • Test other type of notices, for example in the General Settings page, remove the @ from the 'Administration Email Address' field and save. An error notice should be printed at the top of the page.
  • Temporarily add more notices for testing purposes by manually adding them on a settings page or for example here.
  • Test rendering multiple notices at the same time.

Example of how to add a settings errors admin notice:

add_settings_error( 'general', 'settings_testnotice', __( 'Test notice.' ), 'warning' );

#4 @afercia
2 hours ago

Note that the PR had to address some backward compatibility for edge cases e.g. a passed notice type that may contain spaces like error my-own-css-class hello world.

Those should be considered a doing it wraong in fact, wp_get_admin_notice() expects a string with no spaces.

See ticket #44941.

Note: See TracTickets for help on using tickets.