_wp_ajax_add_hierarchical_term

Ajax handler for adding a hierarchical term.

Description

_wp_ajax_add_hierarchical_term(); 

Usage

  1. if ( !function_exists( '_wp_ajax_add_hierarchical_term' ) ) { 
  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_hierarchical_term(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function _wp_ajax_add_hierarchical_term() { 
  2. $action = $_POST['action']; 
  3. $taxonomy = get_taxonomy(substr($action, 4)); 
  4. check_ajax_referer( $action, '_ajax_nonce-add-' . $taxonomy->name ); 
  5. if ( !current_user_can( $taxonomy->cap->edit_terms ) ) 
  6. wp_die( -1 ); 
  7. $names = explode(', ', $_POST['new'.$taxonomy->name]); 
  8. $parent = isset($_POST['new'.$taxonomy->name.'_parent']) ? (int) $_POST['new'.$taxonomy->name.'_parent'] : 0; 
  9. if ( 0 > $parent ) 
  10. $parent = 0; 
  11. if ( $taxonomy->name == 'category' ) 
  12. $post_category = isset($_POST['post_category']) ? (array) $_POST['post_category'] : array(); 
  13. else 
  14. $post_category = ( isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name]) ) ? (array) $_POST['tax_input'][$taxonomy->name] : array(); 
  15. $checked_categories = array_map( 'absint', (array) $post_category ); 
  16. $popular_ids = wp_popular_terms_checklist($taxonomy->name, 0, 10, false); 
  17.  
  18. foreach ( $names as $cat_name ) { 
  19. $cat_name = trim($cat_name); 
  20. $category_nicename = sanitize_title($cat_name); 
  21. if ( '' === $category_nicename ) 
  22. continue; 
  23. if ( !$cat_id = term_exists( $cat_name, $taxonomy->name, $parent ) ) 
  24. $cat_id = wp_insert_term( $cat_name, $taxonomy->name, array( 'parent' => $parent ) ); 
  25. if ( is_wp_error( $cat_id ) ) { 
  26. continue; 
  27. } elseif ( is_array( $cat_id ) ) { 
  28. $cat_id = $cat_id['term_id']; 
  29. $checked_categories[] = $cat_id; 
  30. if ( $parent ) // Do these all at once in a second 
  31. continue; 
  32.  
  33. ob_start(); 
  34.  
  35. wp_terms_checklist( 0, array( 'taxonomy' => $taxonomy->name, 'descendants_and_self' => $cat_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids )); 
  36.  
  37. $data = ob_get_clean(); 
  38.  
  39. $add = array( 
  40. 'what' => $taxonomy->name,  
  41. 'id' => $cat_id,  
  42. 'data' => str_replace( array("\n", "\t"), '', $data),  
  43. 'position' => -1 
  44. ); 
  45.  
  46. if ( $parent ) { // Foncy - replace the parent and all its children 
  47. $parent = get_term( $parent, $taxonomy->name ); 
  48. $term_id = $parent->term_id; 
  49.  
  50. while ( $parent->parent ) { // get the top parent 
  51. $parent = get_term( $parent->parent, $taxonomy->name ); 
  52. if ( is_wp_error( $parent ) ) 
  53. break; 
  54. $term_id = $parent->term_id; 
  55.  
  56. ob_start(); 
  57.  
  58. wp_terms_checklist( 0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $term_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids)); 
  59.  
  60. $data = ob_get_clean(); 
  61.  
  62. $add = array( 
  63. 'what' => $taxonomy->name,  
  64. 'id' => $term_id,  
  65. 'data' => str_replace( array("\n", "\t"), '', $data),  
  66. 'position' => -1 
  67. ); 
  68.  
  69. ob_start(); 
  70.  
  71. 'taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new'.$taxonomy->name.'_parent', 'orderby' => 'name',  
  72. 'hierarchical' => 1, 'show_option_none' => '— '.$taxonomy->labels->parent_item.' —' 
  73. ) ); 
  74.  
  75. $sup = ob_get_clean(); 
  76.  
  77. $add['supplemental'] = array( 'newcat_parent' => $sup ); 
  78.  
  79. $x = new WP_Ajax_Response( $add ); 
  80. $x->send();