wp_ajax_add_link_category

Ajax handler for adding a link category.

Description

wp_ajax_add_link_category( (string) $action ); 

Parameters (1)

0. $action (string)
Action to perform.

Usage

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

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_add_link_category( $action ) { 
  2. if ( empty( $action ) ) 
  3. $action = 'add-link-category'; 
  4. check_ajax_referer( $action ); 
  5. $tax = get_taxonomy( 'link_category' ); 
  6. if ( ! current_user_can( $tax->cap->manage_terms ) ) { 
  7. wp_die( -1 ); 
  8. $names = explode(', ', wp_unslash( $_POST['newcat'] ) ); 
  9. $x = new WP_Ajax_Response(); 
  10. foreach ( $names as $cat_name ) { 
  11. $cat_name = trim($cat_name); 
  12. $slug = sanitize_title($cat_name); 
  13. if ( '' === $slug ) 
  14. continue; 
  15. if ( !$cat_id = term_exists( $cat_name, 'link_category' ) ) 
  16. $cat_id = wp_insert_term( $cat_name, 'link_category' ); 
  17. if ( is_wp_error( $cat_id ) ) { 
  18. continue; 
  19. } elseif ( is_array( $cat_id ) ) { 
  20. $cat_id = $cat_id['term_id']; 
  21. $cat_name = esc_html( $cat_name ); 
  22. $x->add( array( 
  23. 'what' => 'link-category',  
  24. 'id' => $cat_id,  
  25. 'data' => "<li id='link-category-$cat_id'><label for='in-link-category-$cat_id' class='selectit'><input value='" . esc_attr($cat_id) . "' type='checkbox' checked='checked' name='link_category[]' id='in-link-category-$cat_id'/> $cat_name</label></li>",  
  26. 'position' => -1 
  27. ) ); 
  28. $x->send();