Make WordPress Core

Opened 7 weeks ago

Closed 6 weeks ago

#66161 closed defect (bug) (fixed)

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 (last modified by afercia)

The function settings_errors() is responsible for building and displaying 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 the means of wp_admin_notice().

I'd like to propose to refactor settings_errors() so that 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 ); and wp_get_admin_notice() fires the filter wp_admin_notice_markup.

That would help the ongoing effort in #50486 to collect all the admin notices. It would allow to retrieve the message and all the args used by the admin notice, which is a pre-requisite for #50486.

Change History (8)

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


7 weeks 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:


7 weeks ago
#2

Cc @pattonwebz

#3 @afercia
7 weeks ago

  • Owner set to afercia
  • Status new → assigned

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
7 weeks 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.

​@afercia commented on ​PR #13669:


7 weeks ago
#5

Currently, this PR introduces a change in the behavior for _potential_ custom notice types.
Before, a passed type like for example calltoaction worked and was printed out without prefixing notice-.
After, it gets prefixed and becomes notice-calltoaction.

This is inherently the behavior of wp_admin_notice(() which always adds the notice- prefix.
A decision shoudl be made on whether to allow custom types without the notice- prefix.

​@afercia commented on ​PR #13669:


7 weeks ago
#6

I addressed the backward compatibility issue in a hopefully better way and added more test cases. It covers cases when the $type passed to add_settings_error() may be like:

'error'
'success'
'warning'
'info'
'updated'
'notice-error'
'custom'
'notice-custom'
'error my-own-css-class hello world'
'error my-notice-is-awesome hello world'

This should cover the previous behavior.
Note that custom types may produce CSS classes that are rendered in a different order, but the classes are all there.

#7 @afercia
6 weeks ago

  • Description modified (diff)

#8 @afercia
6 weeks ago

  • Resolution → fixed
  • Status assigned → closed

In 63943:

Improve the Settings admin notices implementation.

The amdin notices rendered in the Settings pages use their own function to echo the notices, which is a little legacy. Today, Core provides a standardized way to render the admin notices by the means of wp_admin_notice().

  • Refactors settings_errors() to use wp_admin_notice() internally.
  • Expands the related tests.

Added bonus: The Settings pages notices will now fire the some hooks and filters fired by wp_admin_notice(), e.g. wp_admin_notice and wp_admin_notice_markup, which is a necessary preparation for #50486.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13669

Props afercia, westonruter, joedolson.
See #50486.
Fixes #66161.

Note: See TracTickets for help on using tickets.