Skip to content

Commit

Permalink
Taxonomy: Show error message for terms without a name.
Browse files Browse the repository at this point in the history
Display an error message to users if they attempt to create a term without a name via the admin-ajax `add-tag` action. This improves the accessibility of the screen by avoiding the use of color alone to indicate an error.

Props conner_bw, birgire, afercia.
Fixes #47018.



git-svn-id: https://develop.svn.wordpress.org/trunk@53088 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Apr 7, 2022
1 parent 342cd8c commit 7f94f99
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/js/_enqueues/admin/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ jQuery( function($) {
$('#submit').on( 'click', function(){
var form = $(this).parents('form');

if ( ! validateForm( form ) )
return false;

if ( addingTerm ) {
// If we're adding a term, noop the button to avoid duplicate requests.
return false;
Expand All @@ -127,8 +124,14 @@ jQuery( function($) {

$('#ajax-response').empty();
res = wpAjax.parseAjaxResponse( r, 'ajax-response' );
if ( ! res || res.errors )

if ( res.errors && res.responses[0].errors[0].code === 'empty_term_name' ) {
validateForm( form );
}

if ( ! res || res.errors ) {
return;
}

parent = form.find( 'select#parent' ).val();

Expand Down
9 changes: 7 additions & 2 deletions src/wp-admin/includes/ajax-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1071,16 +1071,21 @@ function wp_ajax_add_tag() {
}

if ( ! $tag || is_wp_error( $tag ) ) {
$message = __( 'An error has occurred. Please reload the page and try again.' );
$message = __( 'An error has occurred. Please reload the page and try again.' );
$error_code = 'error';

if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
$message = $tag->get_error_message();
}

if ( is_wp_error( $tag ) && $tag->get_error_code() ) {
$error_code = $tag->get_error_code();
}

$x->add(
array(
'what' => 'taxonomy',
'data' => new WP_Error( 'error', $message ),
'data' => new WP_Error( $error_code, $message ),
)
);
$x->send();
Expand Down

0 comments on commit 7f94f99

Please sign in to comment.