wp_xmlrpc_server::wp_newCategory()publicWP 2.2.0

Creates a new category.

Метод класса: wp_xmlrpc_server{}

Возвращает

int|IXR_Error. Category ID.

Использование

$wp_xmlrpc_server = new wp_xmlrpc_server();
$wp_xmlrpc_server->wp_newCategory( $args );
$args(массив) (обязательный)

Method arguments. Note: arguments must be ordered as documented.

  • 0(int)
    Blog ID (unused).

  • 1(строка)
    Username.

  • 2(строка)
    Password.

  • 3(массив)
    Category.

Список изменений

С версии 2.2.0 Введена.

Код wp_xmlrpc_server::wp_newCategory() WP 6.5.2

public function wp_newCategory( $args ) {
	$this->escape( $args );

	$username = $args[1];
	$password = $args[2];
	$category = $args[3];

	$user = $this->login( $username, $password );
	if ( ! $user ) {
		return $this->error;
	}

	/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
	do_action( 'xmlrpc_call', 'wp.newCategory', $args, $this );

	// Make sure the user is allowed to add a category.
	if ( ! current_user_can( 'manage_categories' ) ) {
		return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a category.' ) );
	}

	/*
	 * If no slug was provided, make it empty
	 * so that WordPress will generate one.
	 */
	if ( empty( $category['slug'] ) ) {
		$category['slug'] = '';
	}

	/*
	 * If no parent_id was provided, make it empty
	 * so that it will be a top-level page (no parent).
	 */
	if ( ! isset( $category['parent_id'] ) ) {
		$category['parent_id'] = '';
	}

	// If no description was provided, make it empty.
	if ( empty( $category['description'] ) ) {
		$category['description'] = '';
	}

	$new_category = array(
		'cat_name'             => $category['name'],
		'category_nicename'    => $category['slug'],
		'category_parent'      => $category['parent_id'],
		'category_description' => $category['description'],
	);

	$cat_id = wp_insert_category( $new_category, true );
	if ( is_wp_error( $cat_id ) ) {
		if ( 'term_exists' === $cat_id->get_error_code() ) {
			return (int) $cat_id->get_error_data();
		} else {
			return new IXR_Error( 500, __( 'Sorry, the category could not be created.' ) );
		}
	} elseif ( ! $cat_id ) {
		return new IXR_Error( 500, __( 'Sorry, the category could not be created.' ) );
	}

	/**
	 * Fires after a new category has been successfully created via XML-RPC.
	 *
	 * @since 3.4.0
	 *
	 * @param int   $cat_id ID of the new category.
	 * @param array $args   An array of new category arguments.
	 */
	do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase

	return $cat_id;
}