wp_ajax_add_tag

Ajax handler to add a tag.

Description

wp_ajax_add_tag(); 

Usage

  1. if ( !function_exists( 'wp_ajax_add_tag' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_add_tag(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_add_tag() { 
  2. check_ajax_referer( 'add-tag', '_wpnonce_add-tag' ); 
  3. $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag'; 
  4. $tax = get_taxonomy($taxonomy); 
  5.  
  6. if ( !current_user_can( $tax->cap->edit_terms ) ) 
  7. wp_die( -1 ); 
  8.  
  9. $x = new WP_Ajax_Response(); 
  10.  
  11. $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST ); 
  12.  
  13. if ( !$tag || is_wp_error($tag) || (!$tag = get_term( $tag['term_id'], $taxonomy )) ) { 
  14. $message = __('Anerrorhas occurred. Please reload the page and try again.'); 
  15. if ( is_wp_error($tag) && $tag->get_error_message() ) 
  16. $message = $tag->get_error_message(); 
  17.  
  18. $x->add( array( 
  19. 'what' => 'taxonomy',  
  20. 'data' => new WP_Error(error, $message ) 
  21. ) ); 
  22. $x->send(); 
  23.  
  24. $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => $_POST['screen'] ) ); 
  25.  
  26. $level = 0; 
  27. if ( is_taxonomy_hierarchical($taxonomy) ) { 
  28. $level = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) ); 
  29. ob_start(); 
  30. $wp_list_table->single_row( $tag, $level ); 
  31. $noparents = ob_get_clean(); 
  32.  
  33. ob_start(); 
  34. $wp_list_table->single_row( $tag ); 
  35. $parents = ob_get_clean(); 
  36.  
  37. $x->add( array( 
  38. 'what' => 'taxonomy',  
  39. 'supplemental' => compact('parents', 'noparents') 
  40. ) ); 
  41. $x->add( array( 
  42. 'what' => 'term',  
  43. 'position' => $level,  
  44. 'supplemental' => (array) $tag 
  45. ) ); 
  46. $x->send();