wp_ajax_add_link_category() WordPress Function

The wp_ajax_add_link_category() function allows you to add a new link category from the WordPress admin panel. This function is only available to logged in users who have the manage_categories capability.

wp_ajax_add_link_category( string $action ) #

Ajax handler for adding a link category.


Parameters

$action

(string)(Required)Action to perform.


Top ↑

Source

File: wp-admin/includes/ajax-actions.php

1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
function wp_ajax_add_link_category( $action ) {
    if ( empty( $action ) ) {
        $action = 'add-link-category';
    }
 
    check_ajax_referer( $action );
    $tax = get_taxonomy( 'link_category' );
 
    if ( ! current_user_can( $tax->cap->manage_terms ) ) {
        wp_die( -1 );
    }
 
    $names = explode( ',', wp_unslash( $_POST['newcat'] ) );
    $x     = new WP_Ajax_Response();
 
    foreach ( $names as $cat_name ) {
        $cat_name = trim( $cat_name );
        $slug     = sanitize_title( $cat_name );
 
        if ( '' === $slug ) {
            continue;
        }
 
        $cat_id = wp_insert_term( $cat_name, 'link_category' );
 
        if ( ! $cat_id || is_wp_error( $cat_id ) ) {
            continue;
        } else {
            $cat_id = $cat_id['term_id'];
        }
 
        $cat_name = esc_html( $cat_name );
 
        $x->add(
            array(
                'what'     => 'link-category',
                'id'       => $cat_id,
                '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>",
                'position' => -1,
            )
        );
    }
    $x->send();
}


Top ↑

Changelog

Changelog
VersionDescription
3.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More