wp_ajax_add_tag

The timeline below displays how wordpress function wp_ajax_add_tag has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Handles adding a tag via AJAX.
 *
 * @since 3.1.0
 */
function wp_ajax_add_tag()
{
    check_ajax_referer('add-tag', '_wpnonce_add-tag');
    $taxonomy = (!empty($_POST['taxonomy'])) ? $_POST['taxonomy'] : 'post_tag';
    $taxonomy_object = get_taxonomy($taxonomy);
    if (!current_user_can($taxonomy_object->cap->edit_terms)) {
        wp_die(-1);
    }
    $x = new WP_Ajax_Response();
    $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST);
    if ($tag && !is_wp_error($tag)) {
        $tag = get_term($tag['term_id'], $taxonomy);
    }
    if (!$tag || is_wp_error($tag)) {
        $message = __('An error has occurred. Please reload the page and try again.');
        $error_code = 'error';
        if (is_wp_error($tag) && $tag->get_error_message()) {
            $message = $tag->get_error_message();
        }
        if (is_wp_error($tag) && $tag->get_error_code()) {
            $error_code = $tag->get_error_code();
        }
        $x->add(array('what' => 'taxonomy', 'data' => new WP_Error($error_code, $message)));
        $x->send();
    }
    $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen']));
    $level = 0;
    $noparents = '';
    if (is_taxonomy_hierarchical($taxonomy)) {
        $level = count(get_ancestors($tag->term_id, $taxonomy, 'taxonomy'));
        ob_start();
        $wp_list_table->single_row($tag, $level);
        $noparents = ob_get_clean();
    }
    ob_start();
    $wp_list_table->single_row($tag);
    $parents = ob_get_clean();
    require ABSPATH . 'wp-admin/includes/edit-tag-messages.php';
    $message = '';
    if (isset($messages[$taxonomy_object->name][1])) {
        $message = $messages[$taxonomy_object->name][1];
    } elseif (isset($messages['_item'][1])) {
        $message = $messages['_item'][1];
    }
    $x->add(array('what' => 'taxonomy', 'data' => $message, 'supplemental' => array('parents' => $parents, 'noparents' => $noparents, 'notice' => $message)));
    $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag));
    $x->send();
}

WordPress Version: 6.1

/**
 * Ajax handler to add a tag.
 *
 * @since 3.1.0
 */
function wp_ajax_add_tag()
{
    check_ajax_referer('add-tag', '_wpnonce_add-tag');
    $taxonomy = (!empty($_POST['taxonomy'])) ? $_POST['taxonomy'] : 'post_tag';
    $taxonomy_object = get_taxonomy($taxonomy);
    if (!current_user_can($taxonomy_object->cap->edit_terms)) {
        wp_die(-1);
    }
    $x = new WP_Ajax_Response();
    $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST);
    if ($tag && !is_wp_error($tag)) {
        $tag = get_term($tag['term_id'], $taxonomy);
    }
    if (!$tag || is_wp_error($tag)) {
        $message = __('An error has occurred. Please reload the page and try again.');
        $error_code = 'error';
        if (is_wp_error($tag) && $tag->get_error_message()) {
            $message = $tag->get_error_message();
        }
        if (is_wp_error($tag) && $tag->get_error_code()) {
            $error_code = $tag->get_error_code();
        }
        $x->add(array('what' => 'taxonomy', 'data' => new WP_Error($error_code, $message)));
        $x->send();
    }
    $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen']));
    $level = 0;
    $noparents = '';
    if (is_taxonomy_hierarchical($taxonomy)) {
        $level = count(get_ancestors($tag->term_id, $taxonomy, 'taxonomy'));
        ob_start();
        $wp_list_table->single_row($tag, $level);
        $noparents = ob_get_clean();
    }
    ob_start();
    $wp_list_table->single_row($tag);
    $parents = ob_get_clean();
    require ABSPATH . 'wp-admin/includes/edit-tag-messages.php';
    $message = '';
    if (isset($messages[$taxonomy_object->name][1])) {
        $message = $messages[$taxonomy_object->name][1];
    } elseif (isset($messages['_item'][1])) {
        $message = $messages['_item'][1];
    }
    $x->add(array('what' => 'taxonomy', 'data' => $message, 'supplemental' => array('parents' => $parents, 'noparents' => $noparents, 'notice' => $message)));
    $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag));
    $x->send();
}

WordPress Version: 5.9

/**
 * Ajax handler to add a tag.
 *
 * @since 3.1.0
 */
function wp_ajax_add_tag()
{
    check_ajax_referer('add-tag', '_wpnonce_add-tag');
    $taxonomy = (!empty($_POST['taxonomy'])) ? $_POST['taxonomy'] : 'post_tag';
    $tax = get_taxonomy($taxonomy);
    if (!current_user_can($tax->cap->edit_terms)) {
        wp_die(-1);
    }
    $x = new WP_Ajax_Response();
    $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST);
    if ($tag && !is_wp_error($tag)) {
        $tag = get_term($tag['term_id'], $taxonomy);
    }
    if (!$tag || is_wp_error($tag)) {
        $message = __('An error has occurred. Please reload the page and try again.');
        if (is_wp_error($tag) && $tag->get_error_message()) {
            $message = $tag->get_error_message();
        }
        $x->add(array('what' => 'taxonomy', 'data' => new WP_Error('error', $message)));
        $x->send();
    }
    $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen']));
    $level = 0;
    $noparents = '';
    if (is_taxonomy_hierarchical($taxonomy)) {
        $level = count(get_ancestors($tag->term_id, $taxonomy, 'taxonomy'));
        ob_start();
        $wp_list_table->single_row($tag, $level);
        $noparents = ob_get_clean();
    }
    ob_start();
    $wp_list_table->single_row($tag);
    $parents = ob_get_clean();
    require ABSPATH . 'wp-admin/includes/edit-tag-messages.php';
    $message = '';
    if (isset($messages[$tax->name][1])) {
        $message = $messages[$tax->name][1];
    } elseif (isset($messages['_item'][1])) {
        $message = $messages['_item'][1];
    }
    $x->add(array('what' => 'taxonomy', 'data' => $message, 'supplemental' => compact('parents', 'noparents')));
    $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag));
    $x->send();
}

WordPress Version: 5.3

/**
 * Ajax handler to add a tag.
 *
 * @since 3.1.0
 */
function wp_ajax_add_tag()
{
    check_ajax_referer('add-tag', '_wpnonce_add-tag');
    $taxonomy = (!empty($_POST['taxonomy'])) ? $_POST['taxonomy'] : 'post_tag';
    $tax = get_taxonomy($taxonomy);
    if (!current_user_can($tax->cap->edit_terms)) {
        wp_die(-1);
    }
    $x = new WP_Ajax_Response();
    $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST);
    if ($tag && !is_wp_error($tag)) {
        $tag = get_term($tag['term_id'], $taxonomy);
    }
    if (!$tag || is_wp_error($tag)) {
        $message = __('An error has occurred. Please reload the page and try again.');
        if (is_wp_error($tag) && $tag->get_error_message()) {
            $message = $tag->get_error_message();
        }
        $x->add(array('what' => 'taxonomy', 'data' => new WP_Error('error', $message)));
        $x->send();
    }
    $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen']));
    $level = 0;
    $noparents = '';
    if (is_taxonomy_hierarchical($taxonomy)) {
        $level = count(get_ancestors($tag->term_id, $taxonomy, 'taxonomy'));
        ob_start();
        $wp_list_table->single_row($tag, $level);
        $noparents = ob_get_clean();
    }
    ob_start();
    $wp_list_table->single_row($tag);
    $parents = ob_get_clean();
    $x->add(array('what' => 'taxonomy', 'supplemental' => compact('parents', 'noparents')));
    $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag));
    $x->send();
}

WordPress Version: 5.2

/**
 * Ajax handler to add a tag.
 *
 * @since 3.1.0
 */
function wp_ajax_add_tag()
{
    check_ajax_referer('add-tag', '_wpnonce_add-tag');
    $taxonomy = (!empty($_POST['taxonomy'])) ? $_POST['taxonomy'] : 'post_tag';
    $tax = get_taxonomy($taxonomy);
    if (!current_user_can($tax->cap->edit_terms)) {
        wp_die(-1);
    }
    $x = new WP_Ajax_Response();
    $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST);
    if (!$tag || is_wp_error($tag) || !$tag = get_term($tag['term_id'], $taxonomy)) {
        $message = __('An error has occurred. Please reload the page and try again.');
        if (is_wp_error($tag) && $tag->get_error_message()) {
            $message = $tag->get_error_message();
        }
        $x->add(array('what' => 'taxonomy', 'data' => new WP_Error('error', $message)));
        $x->send();
    }
    $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen']));
    $level = 0;
    $noparents = '';
    if (is_taxonomy_hierarchical($taxonomy)) {
        $level = count(get_ancestors($tag->term_id, $taxonomy, 'taxonomy'));
        ob_start();
        $wp_list_table->single_row($tag, $level);
        $noparents = ob_get_clean();
    }
    ob_start();
    $wp_list_table->single_row($tag);
    $parents = ob_get_clean();
    $x->add(array('what' => 'taxonomy', 'supplemental' => compact('parents', 'noparents')));
    $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag));
    $x->send();
}

WordPress Version: 4.7

/**
 * Ajax handler to add a tag.
 *
 * @since 3.1.0
 */
function wp_ajax_add_tag()
{
    check_ajax_referer('add-tag', '_wpnonce_add-tag');
    $taxonomy = (!empty($_POST['taxonomy'])) ? $_POST['taxonomy'] : 'post_tag';
    $tax = get_taxonomy($taxonomy);
    if (!current_user_can($tax->cap->edit_terms)) {
        wp_die(-1);
    }
    $x = new WP_Ajax_Response();
    $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST);
    if (!$tag || is_wp_error($tag) || !$tag = get_term($tag['term_id'], $taxonomy)) {
        $message = __('An error has occurred. Please reload the page and try again.');
        if (is_wp_error($tag) && $tag->get_error_message()) {
            $message = $tag->get_error_message();
        }
        $x->add(array('what' => 'taxonomy', 'data' => new WP_Error('error', $message)));
        $x->send();
    }
    $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen']));
    $level = 0;
    if (is_taxonomy_hierarchical($taxonomy)) {
        $level = count(get_ancestors($tag->term_id, $taxonomy, 'taxonomy'));
        ob_start();
        $wp_list_table->single_row($tag, $level);
        $noparents = ob_get_clean();
    }
    ob_start();
    $wp_list_table->single_row($tag);
    $parents = ob_get_clean();
    $x->add(array('what' => 'taxonomy', 'supplemental' => compact('parents', 'noparents')));
    $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag));
    $x->send();
}

WordPress Version: 4.3

/**
 * Ajax handler to add a tag.
 *
 * @since 3.1.0
 *
 * @global WP_List_Table $wp_list_table
 */
function wp_ajax_add_tag()
{
    global $wp_list_table;
    check_ajax_referer('add-tag', '_wpnonce_add-tag');
    $taxonomy = (!empty($_POST['taxonomy'])) ? $_POST['taxonomy'] : 'post_tag';
    $tax = get_taxonomy($taxonomy);
    if (!current_user_can($tax->cap->edit_terms)) {
        wp_die(-1);
    }
    $x = new WP_Ajax_Response();
    $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST);
    if (!$tag || is_wp_error($tag) || !$tag = get_term($tag['term_id'], $taxonomy)) {
        $message = __('An error has occurred. Please reload the page and try again.');
        if (is_wp_error($tag) && $tag->get_error_message()) {
            $message = $tag->get_error_message();
        }
        $x->add(array('what' => 'taxonomy', 'data' => new WP_Error('error', $message)));
        $x->send();
    }
    $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen']));
    $level = 0;
    if (is_taxonomy_hierarchical($taxonomy)) {
        $level = count(get_ancestors($tag->term_id, $taxonomy, 'taxonomy'));
        ob_start();
        $wp_list_table->single_row($tag, $level);
        $noparents = ob_get_clean();
    }
    ob_start();
    $wp_list_table->single_row($tag);
    $parents = ob_get_clean();
    $x->add(array('what' => 'taxonomy', 'supplemental' => compact('parents', 'noparents')));
    $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag));
    $x->send();
}

WordPress Version: 4.1

/**
 * Ajax handler to add a tag.
 *
 * @since 3.1.0
 */
function wp_ajax_add_tag()
{
    global $wp_list_table;
    check_ajax_referer('add-tag', '_wpnonce_add-tag');
    $taxonomy = (!empty($_POST['taxonomy'])) ? $_POST['taxonomy'] : 'post_tag';
    $tax = get_taxonomy($taxonomy);
    if (!current_user_can($tax->cap->edit_terms)) {
        wp_die(-1);
    }
    $x = new WP_Ajax_Response();
    $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST);
    if (!$tag || is_wp_error($tag) || !$tag = get_term($tag['term_id'], $taxonomy)) {
        $message = __('An error has occurred. Please reload the page and try again.');
        if (is_wp_error($tag) && $tag->get_error_message()) {
            $message = $tag->get_error_message();
        }
        $x->add(array('what' => 'taxonomy', 'data' => new WP_Error('error', $message)));
        $x->send();
    }
    $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen']));
    $level = 0;
    if (is_taxonomy_hierarchical($taxonomy)) {
        $level = count(get_ancestors($tag->term_id, $taxonomy, 'taxonomy'));
        ob_start();
        $wp_list_table->single_row($tag, $level);
        $noparents = ob_get_clean();
    }
    ob_start();
    $wp_list_table->single_row($tag);
    $parents = ob_get_clean();
    $x->add(array('what' => 'taxonomy', 'supplemental' => compact('parents', 'noparents')));
    $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag));
    $x->send();
}

WordPress Version: 4.0

/**
 * Ajax handler to add a tag.
 *
 * @since 3.1.0
 */
function wp_ajax_add_tag()
{
    global $wp_list_table;
    check_ajax_referer('add-tag', '_wpnonce_add-tag');
    $taxonomy = (!empty($_POST['taxonomy'])) ? $_POST['taxonomy'] : 'post_tag';
    $tax = get_taxonomy($taxonomy);
    if (!current_user_can($tax->cap->edit_terms)) {
        wp_die(-1);
    }
    $x = new WP_Ajax_Response();
    $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST);
    if (!$tag || is_wp_error($tag) || !$tag = get_term($tag['term_id'], $taxonomy)) {
        $message = __('An error has occurred. Please reload the page and try again.');
        if (is_wp_error($tag) && $tag->get_error_message()) {
            $message = $tag->get_error_message();
        }
        $x->add(array('what' => 'taxonomy', 'data' => new WP_Error('error', $message)));
        $x->send();
    }
    $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen']));
    $level = 0;
    if (is_taxonomy_hierarchical($taxonomy)) {
        $level = count(get_ancestors($tag->term_id, $taxonomy));
        ob_start();
        $wp_list_table->single_row($tag, $level);
        $noparents = ob_get_clean();
    }
    ob_start();
    $wp_list_table->single_row($tag);
    $parents = ob_get_clean();
    $x->add(array('what' => 'taxonomy', 'supplemental' => compact('parents', 'noparents')));
    $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag));
    $x->send();
}

WordPress Version: 3.7

function wp_ajax_add_tag()
{
    global $wp_list_table;
    check_ajax_referer('add-tag', '_wpnonce_add-tag');
    $post_type = (!empty($_POST['post_type'])) ? $_POST['post_type'] : 'post';
    $taxonomy = (!empty($_POST['taxonomy'])) ? $_POST['taxonomy'] : 'post_tag';
    $tax = get_taxonomy($taxonomy);
    if (!current_user_can($tax->cap->edit_terms)) {
        wp_die(-1);
    }
    $x = new WP_Ajax_Response();
    $tag = wp_insert_term($_POST['tag-name'], $taxonomy, $_POST);
    if (!$tag || is_wp_error($tag) || !$tag = get_term($tag['term_id'], $taxonomy)) {
        $message = __('An error has occurred. Please reload the page and try again.');
        if (is_wp_error($tag) && $tag->get_error_message()) {
            $message = $tag->get_error_message();
        }
        $x->add(array('what' => 'taxonomy', 'data' => new WP_Error('error', $message)));
        $x->send();
    }
    $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => $_POST['screen']));
    $level = 0;
    if (is_taxonomy_hierarchical($taxonomy)) {
        $level = count(get_ancestors($tag->term_id, $taxonomy));
        ob_start();
        $wp_list_table->single_row($tag, $level);
        $noparents = ob_get_clean();
    }
    ob_start();
    $wp_list_table->single_row($tag);
    $parents = ob_get_clean();
    $x->add(array('what' => 'taxonomy', 'supplemental' => compact('parents', 'noparents')));
    $x->add(array('what' => 'term', 'position' => $level, 'supplemental' => (array) $tag));
    $x->send();
}