How can I create a new user account while creating a new post, and then set that new user as the author of that new post?
I have a custom meta box with some user information fields, and when the post is published, the new user account is created, along with the new post. However, the new post's author is set as the current user. Is there a way to set that post's author to be the newly created user?
I have the following code:
function osb_save_meta_box( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) DOING_AUTOSAVE ) return;
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$post_id = $parent_id;
}
$fields = [
'osb-speaker_first_name',
'osb-speaker_last_name',
'osb-speaker_full_name',
'osb-speaker_email',
'osb-short_bio'
];
foreach ( $fields as $field ) {
if ( array_key_exists( $field, $_POST ) ) {
update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
}
}
}
add_action( 'save_post', 'osb_save_meta_box' );
function create_account() {
$userdata = array(
'ID' = 0, //(int) User ID. If supplied, the user will be updated.
'user_pass' = '', //(string) The plain-text user password.
'user_login' = ( isset($_POST['osb-speaker_email']) ? $_POST['osb-speaker_email'] : '' ), //(string) The user's login username.
'user_nicename' = '', //(string) The URL-friendly user name.
'user_url' = '', //(string) The user URL.
'user_email' = ( isset($_POST['osb-speaker_email']) ? $_POST['osb-speaker_email'] : '' ), //(string) The user email address.
'display_name' = ( isset($_POST['osb-speaker_full_name']) ? $_POST['osb-speaker_full_name'] : '' ), //(string) The user's display name. Default is the user's username.
'nickname' = '', //(string) The user's nickname. Default is the user's username.
'first_name' = ( isset($_POST['osb-speaker_first_name']) ? $_POST['osb-speaker_first_name'] : '' ), //(string) The user's first name. For new users, will be used to build the first part of the user's display name if $display_name is not specified.
'last_name' = ( isset($_POST['osb-speaker_last_name']) ? $_POST['osb-speaker_last_name'] : '' ), //(string) The user's last name. For new users, will be used to build the second part of the user's display name if $display_name is not specified.
'description' = '', //(string) The user's biographical description.
'rich_editing' = true, //(string|bool) Whether to enable the rich-editor for the user. False if not empty.
'syntax_highlighting' = '', //(string|bool) Whether to enable the rich code editor for the user. False if not empty.
'comment_shortcuts' = '', //(string|bool) Whether to enable comment moderation keyboard shortcuts for the user. Default false.
'admin_color' = '', //(string) Admin color scheme for the user. Default 'fresh'.
'use_ssl' = '', //(bool) Whether the user should always access the admin over https. Default false.
'user_registered' = '', //(string) Date the user registered. Format is 'Y-m-d H:i:s'.
'show_admin_bar_front' = false, //(string|bool) Whether to display the Admin Bar for the user on the site's front end. Default true.
'role' = contributor, //(string) User's role.
);
if ( !username_exists( $user ) !email_exists( $email ) ) {
$user_id = wp_insert_user( $userdata );
//if( !is_wp_error($user_id) ) {
//user has been created
//$user = new WP_User( $user_id );
//$user-set_role( 'contributor' );
//Redirect
//wp_redirect( get_permalink( $post-ID ) );
//exit;
//} else {
//$user_id is a WP_Error object. Manage the error
//}
}
}
add_action('init','create_account');
Topic wp-create-user save-post Wordpress
Category Web