Home › Forums › Front-end Issues › Front-end user registration › Reply To: Front-end user registration
Thanks! Got it working, mostly.
calling acf_form_head() is required before get_header() or before any output is created in header.php
Noted. I’ve moved this bit to the page template, called it in at right place.
// if you don’t want the above fields also saved as custom fields for the user
Regarding unset()
, good call. I wish I had done this sooner, now I have hundreds of instances of orphaned post data I have to delete…
I’ve simplified the code (and also complicated it a bit). It’s solved most issues, created others.
<?php
// set ACF pre-save stuff
add_filter('acf/pre_save_post' , 'acf_create_new_user' );
function acf_create_new_user( $post_id ) {
error_log( 'post id: ' . $post_id);
// // exit if user already logged in, front-end form sent his ID
// if ( $post_id != 'new' ) {
// error_log( 'post not new: ' . $post_id);
// // wp_redirect( home_url() );
// return $post_id;
// }
$field_group = acf_get_fields_by_id('302');
//prepare basic user info
foreach ( $field_group as $key => $value ) {
$$value['name'] = $_POST['acf'][$value['key']];
}
$password = wp_generate_password( 10, true, false );
$username = $candidate_email;
$display_name = $candidate_given_name_romaji . ' ' . $candidate_family_name_romaji;
// check if user exists, exit if so
if ( email_exists( $candidate_email ) ) {
error_log( 'email exists: ' . $candidate_email );
wp_redirect( home_url() . '/sign-up/?email_exists&user_email=' . $candidate_email );
return $candidate_email;
}
// register user
$user_id = wp_create_user( $username, $password, $candidate_email );
error_log( 'guy just created: ' . $user_id );
// update other user info
wp_update_user( array(
'ID' => $user_id,
'first_name' => $candidate_given_name_romaji,
'last_name' => $candidate_family_name_romaji,
'display_name' => $display_name
) );
// update other user meta, prevent creation of post data
foreach ( $field_group as $key => $value ) {
update_user_meta( $user_id, $value['name'], $_POST['acf'][$value['key']] );
unset( $_POST['acf'][$value['key']] );
}
// redirect to login page after finish
wp_redirect( home_url() . '/sign-in/?new_user=true&user_email=' . $candidate_email );
return 'user_' . $user_id;
}
?>
(1) // exit if user already logged in
bit has been commented out because it just grabs the ID of the page, not the post data. I can’t remember if this was working before.
(2) wp_redirect()
bits are not working. I know I can add redirects to acf_form()
but they’re conditional: a successful registration will go to one place and an unsuccessful one will go elsewhere.
Please note, the way I use acf_form()
code has changed. I ran into some issues with calling in an entire field group and had to call in the fields one-by-one. It looks like this:
<?php
// ensures that user is not logged in
if ( is_user_logged_in() == true ) :
global $current_user;
$user_id = 'user_' . $current_user->ID;
else : $user_id = 'new';
endif;
acf_form( array(
'post_id' => $user_id,
'form' => false,
) );
?>
Then further down the page I have a bunch of these:
<?php
acf_form( array(
'fields' => array('some_acf_field'),
'form' => false,
) );
?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.