Home › Forums › General Issues › Saving fields to user registration form
I have tried and tried to find the documentation as to whether this is possible but can’t seem to find a proper answer, so…
I have a user registration form on my front end and have combined it with some ACF forms:
<form name="registerform" id="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
<p>
<label for="user_login">Username</label>
<input type="text" name="user_login" id="user_login" class="input" value="">
</p>
<p>
<label for="user_email">E-mail</label>
<input type="text" name="user_email" id="user_email" class="input" value="">
</p>
<?php $options = array(
'post_id' => 'user_'.$current_user->ID,
'field_groups' => array(136),
'form' => false,
//'return' => add_query_arg( 'updated', 'true', get_permalink() ),
'html_before_fields' => '',
'html_after_fields' => '',
//'submit_value' => 'Update'
);
acf_form( $options );
?>
<p style="display:none">
<label for="confirm_email">Please leave this field empty</label>
<input type="text" name="confirm_email" id="confirm_email" class="input" value="">
</p>
<p id="reg_passmail">A password will be e-mailed to you.</p>
<input type="hidden" name="redirect_to" value="login/?action=register&success=1" />
<p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="Register" /></p>
</form>
It saves and a new user is created but none of the other values are added, i’ve seen some things about hooking in to the registration but I can’t see how i’d do that here?
Any help that could point me in the right direction would be massively appreciated!
Thanks in advance
Dan
Hi @dhod and @bowersinit
when using acf_form within another field like this there’s no automagic saving happening so you’ll have to do that yourself (as you figured out).
I think what you need is to hook into the user_register
action:
https://codex.wordpress.org/Plugin_API/Action_Reference/user_register
and save the ACF input values to their respective fields. Something like:
function save_additional_user_meta( $user_id ) {
if ( isset( $_POST['NAME PARAMETER OF THE ACF INPUT FIELD'] ) )
update_user_meta($user_id, 'first_name', $_POST['NAME PARAMETER OF THE ACF INPUT FIELD']);
}
add_action( 'user_register', 'save_additional_user_meta', 10, 1 );
Thanks Jonathan, but i dont seem to be able to make this work, this it the markup of one of the fields im trying to save:
<input type="number" id="acf-field-price_from" class="number" min="1" max="10000000000000000" step="1" name="fields[field_55e57136adc2e]" value="" placeholder="">
and this is the hook im adding to my themes functions file:
function save_additional_user_meta( $user_id ) {
if ( isset( $_POST['field_55e57136adc2e'] ) )
update_user_meta($user_id, 'number_of_bedrooms', $_POST['field_55e57136adc2e']);
}
add_action( 'user_register', 'save_additional_user_meta', 10, 1 );
ive tried using the id (acf-field-price_from), name (fields[field_55e57136adc2e]) and field key (field_55e57136adc2e) for the post id, and nothing is getting saved to the db
i tested the hook by removing the if isset, like this
function save_additional_user_meta( $user_id ) {
update_user_meta($user_id, 'number_of_bedrooms', 'test');
}
add_action( 'user_register', 'save_additional_user_meta', 10, 1 );
and it saves the “number_of_bedrooms” as test, so i know the hook is working, just the syntax might be wrong on the field save function?
any help is much apreciated
Hi @bowersinit
When an input has a name value like fields[field_55e57136adc2e]
the $_POST value will actually be an array called fields with a fieldkey of field_55e57136adc2e which in term contains the value.
So try this out:
function save_additional_user_meta( $user_id ) {
if ( isset( $_POST['fields']['field_55e57136adc2e'] ) )
update_user_meta($user_id, 'number_of_bedrooms', $_POST['fields']['field_55e57136adc2e']);
}
add_action( 'user_register', 'save_additional_user_meta', 10, 1 );
Thanks Jonathan, that works when i hook it into the register form, but if i add it into a modal popup login form (using AJAX) the fields are empty, im guessing it has something to do with the ajax saving things in a different way? or am i way off the point here?
Are your modal login form also for registering then? Is it part of a plugin?
yes, the modal popups handle login and registration, here is the script that handles all that stuff:
<?php
function ajax_auth_init(){
wp_register_style( 'ajax-auth-style', get_template_directory_uri() . '/css/ajax-auth-style.css' );
wp_enqueue_style('ajax-auth-style');
wp_register_script('validate-script', get_template_directory_uri() . '/js/jquery.validate.js', array('jquery') );
wp_enqueue_script('validate-script');
wp_register_script('ajax-auth-script', get_template_directory_uri() . '/js/ajax-auth-script.js', array('jquery') );
wp_enqueue_script('ajax-auth-script');
wp_localize_script( 'ajax-auth-script', 'ajax_auth_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'redirecturl' => home_url(),
'loadingmessage' => __('Sending user info, please wait...')
));
// Enable the user with no privileges to run ajax_login() in AJAX
add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
// Enable the user with no privileges to run ajax_register() in AJAX
add_action( 'wp_ajax_nopriv_ajaxregister', 'ajax_register' );
}
// Execute the action only if the user isn't logged in
if (!is_user_logged_in()) {
add_action('init', 'ajax_auth_init');
}
function ajax_login(){
// First check the nonce, if it fails the function will break
check_ajax_referer( 'ajax-login-nonce', 'security' );
// Nonce is checked, get the POST data and sign user on
// Call auth_user_login
auth_user_login($_POST['username'], $_POST['password'], 'Login');
die();
}
function ajax_register(){
// First check the nonce, if it fails the function will break
check_ajax_referer( 'ajax-register-nonce', 'security' );
// Nonce is checked, get the POST data and sign user on
$info = array();
$info['user_nicename'] = $info['nickname'] = $info['display_name'] = $info['first_name'] = $info['user_login'] = sanitize_user($_POST['username']) ;
$info['user_pass'] = sanitize_text_field($_POST['password']);
$info['user_email'] = sanitize_email( $_POST['email']);
// Register the user
$user_register = wp_insert_user( $info );
if ( is_wp_error($user_register) ){
$error = $user_register->get_error_codes() ;
if(in_array('empty_user_login', $error))
echo json_encode(array('loggedin'=>false, 'message'=>__($user_register->get_error_message('empty_user_login'))));
elseif(in_array('existing_user_login',$error))
echo json_encode(array('loggedin'=>false, 'message'=>__('This username is already registered.')));
elseif(in_array('existing_user_email',$error))
echo json_encode(array('loggedin'=>false, 'message'=>__('This email address is already registered.')));
} else {
auth_user_login($info['nickname'], $info['user_pass'], 'Registration');
}
die();
}
function auth_user_login($user_login, $password, $login)
{
$info = array();
$info['user_login'] = $user_login;
$info['user_password'] = $password;
$info['remember'] = true;
$user_signon = wp_signon( $info, false );
if ( is_wp_error($user_signon) ){
echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.')));
} else {
wp_set_current_user($user_signon->ID);
echo json_encode(array('loggedin'=>true, 'message'=>__($login.' successful, redirecting...')));
}
die();
}
ive tried putting code after the Register the user like this:`
$user_register = wp_insert_user( $info );
update_user_meta($info_id, ‘number_of_bedrooms’, $_POST[‘fields’][‘field_55e56dfdabccb’]);`
but this doesnt even save an empty value to “number_of_bedrooms” in the usermeta table.
as i understand it, wp_insert_user returns the user id as $info once it successfully registers the user, i guess im missing something somewhere?
The hook user_register should run just the same as when you’re registering a user not through AJAX..
https://codex.wordpress.org/Function_Reference/wp_insert_user#Notes
However the function wp_insert_user will return the new user id if successful so you should be able to do this right after:
update_user_meta($user_register, 'number_of_bedrooms', $_POST['fields']['field_55e56dfdabccb']);
The topic ‘Saving fields to user registration form’ 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.