From 7f94f9906245e74b6d50e5b21c37cfc163d5c61f Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 7 Apr 2022 03:48:16 +0000 Subject: [PATCH] Taxonomy: Show error message for terms without a name. 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 --- src/js/_enqueues/admin/tags.js | 11 +++++++---- src/wp-admin/includes/ajax-actions.php | 9 +++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/js/_enqueues/admin/tags.js b/src/js/_enqueues/admin/tags.js index 4ff7aaa440793..474088646e31c 100644 --- a/src/js/_enqueues/admin/tags.js +++ b/src/js/_enqueues/admin/tags.js @@ -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; @@ -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(); diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 964aba357f323..a6ba2b678ca9b 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -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();