Skip to content

Instantly share code, notes, and snippets.

@ituk
Last active March 25, 2019 16:27
Show Gist options
  • Save ituk/df030c71c96b82025ff5eef086d19cc6 to your computer and use it in GitHub Desktop.
Save ituk/df030c71c96b82025ff5eef086d19cc6 to your computer and use it in GitHub Desktop.
function ituk_listing_register_user(){
if (isset($_POST['ap_form_submit_button'], $_POST['ap_form_nonce'])) { //this is returning false -> WHY?
//create new user from author email
if ( in_array('author_email', $_POST[ 'form_included_fields' ]) && $_POST[ 'ap_form_author_email' ] != '' ) {
$user_email = sanitize_email($_POST[ 'ap_form_author_email' ]);
$user_id = username_exists( $username );
//if no user is registered with this email
if ( !$user_id and email_exists($user_email) == false ) {
//create new user
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
$user_id = wp_create_user( $username, $random_password, $user_email );
$user = new WP_User($user_id);
$user->set_role('listing_author');
//add author name to user
if ( in_array('author_name', $_POST[ 'form_included_fields' ]) && $_POST[ 'ap_form_author_name' ] != '' ) {
$user_first_name = sanitize_text_field($_POST[ 'ap_form_author_name' ]);
update_user_meta( $user_id, 'user_firstname', $user_first_name );
}
//add author url to user
if ( in_array('author_url', $_POST[ 'form_included_fields' ]) && $_POST[ 'ap_form_author_url' ] != '' ) {
$user_url = sanitize_text_field($_POST[ 'ap_form_author_url' ]);
update_user_meta( $user_id, 'user_url', $user_url );
}
//add author phone to user
if ( in_array('listing_phone', $_POST[ 'form_included_fields' ]) && $_POST[ 'listing_phone' ] != '' ) {
$user_phone = sanitize_text_field($_POST[ 'listing_phone' ]);
update_user_meta( $user_id, 'user_phone', $user_phone );
}
//add author phone to user
if ( in_array('listing_city', $_POST[ 'form_included_fields' ]) && $_POST[ 'listing_city' ] != '' ) {
$user_city = sanitize_text_field($_POST[ 'listing_city' ]);
update_user_meta( $user_id, 'user_city', $user_city );
}
// if user already exists
} else {
//redirect to login page and back to form
auth_redirect();
}
// if no author email
} else {
echo "no email";
}
// if user is not logged in
}else{
echo "no \$_POST";
}
}
add_action('template_redirect', 'ituk_listing_register_user');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment