wp_xmlrpc_server::blogger_newPost( array $args ): int|IXR_Error

In this article

Creates a new post.

Parameters

$argsarrayrequired
Method arguments. Note: arguments must be ordered as documented.
  • string
    App key (unused).
  • 1 int
    Blog ID (unused).
  • 2 string
    Username.
  • 3 string
    Password.
  • 4 string
    Content.
  • 5 int
    Publish flag. 0 for draft, 1 for publish.

Return

int|IXR_Error

Source

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

	$username = $args[2];
	$password = $args[3];
	$content  = $args[4];
	$publish  = $args[5];

	$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', 'blogger.newPost', $args, $this );

	$cap = ( $publish ) ? 'publish_posts' : 'edit_posts';
	if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) || ! current_user_can( $cap ) ) {
		return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
	}

	$post_status = ( $publish ) ? 'publish' : 'draft';

	$post_author = $user->ID;

	$post_title    = xmlrpc_getposttitle( $content );
	$post_category = xmlrpc_getpostcategory( $content );
	$post_content  = xmlrpc_removepostdata( $content );

	$post_date     = current_time( 'mysql' );
	$post_date_gmt = current_time( 'mysql', 1 );

	$post_data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status' );

	$post_id = wp_insert_post( $post_data );
	if ( is_wp_error( $post_id ) ) {
		return new IXR_Error( 500, $post_id->get_error_message() );
	}

	if ( ! $post_id ) {
		return new IXR_Error( 500, __( 'Sorry, the post could not be created.' ) );
	}

	$this->attach_uploads( $post_id, $post_content );

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

	return $post_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_blogger_newPost’, int $post_id, array $args )

Fires after a new post has been successfully created via the XML-RPC Blogger API.

Changelog

VersionDescription
1.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.