Home › Forums › Front-end Issues › Registering new user with acf
Hi!
First off.. huge fan of your work. Huuuge.
I am trying to make a user registration / profile form by acf. It works to update information for existing users, but new users do not register. Whyyyy….
Here is the form code:
if ( is_user_logged_in == true ) :
global $current_user;
$user_id = 'user_'.$current_user->ID;
else : $user_id = 'new';
endif;
$args = array(
'post_id' => $user_id,
'field_groups' => array( 1770 )
);
acf_form( $args );
Here is the presave code:
function my_pre_save_user( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
//prepare basic user info
$password = wp_hash_password($_POST['fields']['field_52de6a0b2b106']);
$username = $_POST['fields']['field_52de69e92b104'];
$email = $_POST['fields']['field_52de69f72b105'];
//register user
$user_id = wp_create_user( $username, $password, $email );
//update other user info
wp_update_user( array(
'ID' => $user_id,
'first_name' => $_POST['fields']['field_52de713ca3b92'],
'last_name' => $_POST['fields']['field_52de7149a3b93'],
'display_name' => $_POST['fields']['field_52de713ca3b92'] . $_POST['fields']['field_52de7149a3b93']
));
//update user meta fields
update_user_meta($user_id, 'user_address', $_POST['fields']['field_52de6a292b108'] );
update_user_meta($user_id, 'user_telephone', $_POST['fields']['field_52de6a1c2b107'] );
// return id of the new user
return $user_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_user' );
I must be missing something obvious.
Your code looks good, so there must be a logic or syntax error in your filter.
Can you please go through your code an debug it line by line like so:
<?php
echo '<pre>';
print_r( $password );
echo '</pre>';
die;
?>
This will display the given variable and visually prove that your data is correct and that the code runs to this line successfuly
Hi @maijavilkina,
I’m not definite about this but are you sure the user’s are not being created? I only ask because I think that when using the wp_create_user() function you need to pass it the plain text password, no need to create a hash. If I am right, the user is created but instead of ‘password’ being the password it is actually the has of that: ‘Juwufbfuahduhaf209r32’.
Also, you might want to consider using wp_insert_user() you can pass the first name, last name and display name to that which means you don’t need to create and separately update the user 🙂
I’m not sure either of these have anything to do with your issue, but the first one might 🙂 The user should still be created though!
Daniel
The topic ‘Registering new user with acf’ is closed to new replies.
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.