Skip to content

Instantly share code, notes, and snippets.

@ituk
Last active May 7, 2019 08:06
Show Gist options
  • Save ituk/41d346229379db5b1068cd30f0f0484a to your computer and use it in GitHub Desktop.
Save ituk/41d346229379db5b1068cd30f0f0484a to your computer and use it in GitHub Desktop.
AccessPress create new user on post submission
/** ITUK's Register user on form submission
*
* OPEN `inc/core/save-post.php`.
* LOOK FOR LINE 179, it should be something like: `add_post_meta($post_id, 'ap_author_email', $author_email, false);`
* ADD THE FOLLOWING SNIPPET AFTER THAT LINE
*
**/
//if no user is registered with this email
if ( !email_exists($author_email) ){
//create new user
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false ); //random password
$user_id = wp_create_user( $author_email, $random_password, $author_email ); //create user
$user = new WP_User($user_id); //user object
$user->set_role('listing_author'); //set user role
//add author name to user
if ( $author_name ) {
update_user_meta( $user_id, 'user_firstname', $author_name );
}
//add author url to user
if ( $author_url ) {
update_user_meta( $user_id, 'user_url', $author_url );
}
//set new user as post author
$arg = array(
'ID' => $post_id,
'post_author' => $user_id,
);
wp_update_post( $arg );
//send welcome email to user and admin
wp_new_user_notification( $user_id, null, 'both' );
//log in user
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id );
// if user already exists
} else {
//redirect to login page and back to form
auth_redirect();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment