WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 6 years ago

#3799 closed enhancement (fixed)

improvements for submitting new categories via wp-admin/categories.php

Reported by: prokurator Owned by: mdawaffe
Milestone: 2.5 Priority: normal
Severity: normal Version:
Component: Administration Keywords: category has-patch
Focuses: Cc:

Description

There are 2 things I would like to improve wile submitting new category into WP.

  1. If user by accident chose category name which is already exists in the system
  2. If user by accident hit submit without entering any category name (category name is empty).

In order to fix this problems 3 files need to be updated.

  1. wp-admin/admin-ajax.php

add after line 132 (case 'add-cat')
following code:
##################

$cat = wp_insert_category($_POST);
if ( !$cat ) #= wp_insert_category( $_POST ) )

die('4');

$rr = $cat;
if (is_string($rr) && preg_match('/same/',$rr))

die('5');

# if ( !$cat = wp_insert_category( $_POST ) )
# die('0');
##################

  1. wp-admin/admin-db.php

add in the function wp_insert_category($catarr)
after line 85
following code:
#############

$ccat = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->categories WHERE cat_name = '$cat_name'");
if ($ccat)

return 'same';

#############

  1. /wp-includes/js/wp-ajax-js.php

add after line 61 (in ParseAjaxResponse)
following code or make adjustment to existing error definition:
else if ( 4 == r ) {

Element.update(this.myResponseElement,"<div class='error'><p><?php _e("Something strange happened. Category field is empty."); ?></p></div>");
return false;

} else if ( 5 == r ) {

Element.update(this.myResponseElement,"<div class='error'><p><?php _e("Something strange happened. The same category name already exists."); ?></p></div>");
return false;

}


Attachments (5)

admin-ajax-line132.patch (642 bytes) - added by Mikiupdown2 7 years ago.
admin-db-returnsame.patch (437 bytes) - added by Mikiupdown2 7 years ago.
wp-ajax-emptyexists.patch (884 bytes) - added by Mikiupdown2 7 years ago.
duplicate-category-protection.diff (652 bytes) - added by westi 6 years ago.
Updated patch for trunk
3799.diff (2.3 KB) - added by mdawaffe 6 years ago.

Download all attachments as: .zip

Change History (16)

comment:1 Nazgul7 years ago

  • Milestone changed from 2.3 to 2.2

comment:2 markjaquith7 years ago

  • Keywords needs-patch added

Patch it up!

But instead of:

"Something strange happened. The same category name already exists."

Do:

"The same category name already exists."

We know what happened... no need to attribute it to mystical forces. :-)

comment:3 Mikiupdown27 years ago

  • Keywords patched added; needs-patch removed
  • Resolution set to worksforme
  • Status changed from new to closed

comment:4 foolswisdom7 years ago

  • Keywords has-patch added; patched removed
  • Resolution worksforme deleted
  • Status changed from closed to reopened

comment:5 johnbillion7 years ago

I think this fixes #3683 too but I'm not in a position to test at the moment.

comment:6 foolswisdom7 years ago

  • Milestone changed from 2.2 to 2.3

comment:7 ryan7 years ago

  • Milestone changed from 2.3 to 2.4

comment:8 westi6 years ago

On trunk you get visual feedback if the box is empty already.

But you can add an identically named category but you don't end up with two.

comment:9 westi6 years ago

  • Owner changed from anonymous to mdawaffe
  • Status changed from reopened to new

Uploading a patch which stops duplicate category creation.

However the current ajax code doesn't seem to display any errors that are returned.

mdawaffe any clues?

westi6 years ago

Updated patch for trunk

comment:10 mdawaffe6 years ago

  • Status changed from new to assigned

It's because of a JS error in wp-ajax-response.

Attached fixes the error and tells wp-ajax-response what field to turn red on error.

mdawaffe6 years ago

comment:11 ryan6 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [7336]) Don't allow duplicate category creation. Props westi and mdawaffe. fixes #3799

Note: See TracTickets for help on using tickets.