# wp_ajax_add_tag()

URL: https://chugunov.pro/api-wordpress/functions/wp_ajax_add_tag/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 3.1.0.

## Сигнатура

```php
wp_ajax_add_tag()
```

## Описание

Обрабатывает добавление метки через AJAX.

## Исходный код

Файл: `wp-admin/includes/ajax-actions.php:1092`

```php
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 );
	}

	$response = 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();
		}

		$response->add(
			array(
				'what' => 'taxonomy',
				'data' => new WP_Error( $error_code, $message ),
			)
		);
		$response->send();
	}

	$wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => $_POST['screen'] ) );

	$level      = 0;
	$no_parents = '';

	if ( is_taxonomy_hierarchical( $taxonomy ) ) {
		$level = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) );
		ob_start();
		$wp_list_table->single_row( $tag, $level );
		$no_parents = 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];
	}

	$response->add(
		array(
			'what'         => 'taxonomy',
			'data'         => $message,
			'supplemental' => array(
				'parents'   => $parents,
				'noparents' => $no_parents,
				'notice'    => $message,
			),
		)
	);

	$response->add(
		array(
			'what'         => 'term',
			'position'     => $level,
			'supplemental' => (array) $tag,
		)
	);

	$response->send();
}
```

## История изменений

- 3.1.0 — Introduced.

## Связанные

Использует: `WP_List_Table::single_row`, [`_get_list_table`](https://chugunov.pro/api-wordpress/functions/_get_list_table/), `WP_List_Table`, [`get_ancestors`](https://chugunov.pro/api-wordpress/functions/get_ancestors/), [`wp_insert_term`](https://chugunov.pro/api-wordpress/functions/wp_insert_term/), [`is_taxonomy_hierarchical`](https://chugunov.pro/api-wordpress/functions/is_taxonomy_hierarchical/), `WP_Ajax_Response::__construct`, `WP_Ajax_Response::add`, [`current_user_can`](https://chugunov.pro/api-wordpress/functions/current_user_can/), [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`check_ajax_referer`](https://chugunov.pro/api-wordpress/functions/check_ajax_referer/), [`wp_die`](https://chugunov.pro/api-wordpress/functions/wp_die/), [`get_taxonomy`](https://chugunov.pro/api-wordpress/functions/get_taxonomy/), [`get_term`](https://chugunov.pro/api-wordpress/functions/get_term/), [`is_wp_error`](https://chugunov.pro/api-wordpress/functions/is_wp_error/), `WP_Error::__construct`.

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_ajax_add_tag/
