Creates a new category.
Parameters
$args
arrayrequired- Method arguments. Note: arguments must be ordered as documented.
0
intBlog ID (unused).1
stringUsername.2
stringPassword.3
arrayCategory.
Source
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;
}
Hooks
- do_action( ‘xmlrpc_call’,
string $name ,array|string $args ,wp_xmlrpc_server $server ) Fires after the XML-RPC user has been authenticated but before the rest of the method logic begins.
- do_action( ‘xmlrpc_call_success_wp_newCategory’,
int $cat_id ,array $args ) Fires after a new category has been successfully created via XML-RPC.
Changelog
Version | Description |
---|---|
2.2.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.