Hi Guys im having a bit of trouble using acf_form. Im attempting to create a new user but the data isn’t saving to my custom fields only to the wp user ones! i need them to save to both! any help would be much appreciated!
template:
<?php acf_form_head() ?>
<?php get_header(); ?>
<?php /* Template name: Homepage */ ?>
<?php
if ( is_user_logged_in() == true ) :
global $current_user;
$user_id = 'user_'.$current_user->ID;
else : $user_id = 'new';
endif;
?>
<?php
$args = array(
'post_id' => $user_id,
'submit_value' => 'Register',
'return' => get_permalink(54),
'uploader' => 'wp',
'field_groups' => array(142)
);
acf_form($args);
?>
<?php get_footer(); ?>
functions.php
function user_register_pre_save( $post_id ) {
// check if this is to be a new post
if( $post_id != 'new' ) {
return $post_id;
}
$userdata = array(
'ID' => $user_id,
'user_login' => $_POST['acf']['field_567a4c3c6d6c0'],
'user_pass' => $_POST['acf']['field_567a4c4c6d6c2'],
'user_email' => $_POST['acf']['field_567a4c416d6c1'],
'first_name' => $_POST['acf']['field_567a4d0a839b3'],
'last_name' => $_POST['acf']['field_567a4d10839b4'],
'display_name' => $_POST['acf']['field_567a4d0a839b3'] . ' ' . $_POST['acf']['field_567a4d10839b4'],
'role' => $_POST['acf']['field_567ac0b49e48b']
);
//register user
$user_id = wp_insert_user($userdata);
return $post_id;
}
add_filter('acf/pre_save_post' , 'user_register_pre_save');
In order to create a new user the arguments for acf_form would need to look something like this
$args = array(
'post_id' => 'new_user',
'submit_value' => 'Register',
'return' => get_permalink(54),
'uploader' => 'wp',
'field_groups' => array(142)
);
function user_register_pre_save( $post_id ) {
// check if this is to be a new post
if( $post_id != 'new_user' ) {
return $post_id;
}
// rest of your function here...
and then the beginning of your pre_save_post filter would look something like this
Thanks for the reply John! The bit that’s confusing me is that my code is successfully creating a new user in wp – what it’s not doing is updating my custom fields for ie. First name, last name, username, email etc.
Thanks!
Is this for creating a new user or for adding these fields to a CPT? or are you trying to do both at the same time?
in your code $args for acf_form 'post_id' => $user_id,
I did miss this, so your form should be either creating a new user or updating and existing one, that looks right.
if ( is_user_logged_in() == true ) :
global $current_user;
$user_id = 'user_'.$current_user->ID;
else : $user_id = 'new';
endif;
The other problem you have that I just noticed is this at the end of your pre_save_post filter
//register user
$user_id = wp_insert_user($userdata);
return $post_id;
first thing is that you’re returning $post_id instead of $user_id and the second thing is that you can’t return the bare user ID for ACF to work, this needs to look like this
$user_id = wp_insert_user($userdata);
$user_id = 'user_'.$user_id;
return $user_id;
ahhh you leggend! of course! i had previously realised i was returning the post_id instead of the user_id but it was this line that fixed it – thanks a bunch!
$user_id = 'user_'.$user_id;
🙂
Oops sorry John! wp docs state that wp_insert_user should also update details if an ID is supplied, for some reason now when updating info only my custom field data is updating and not the actual wp user fields 🙁
Im updating user details from a separate template..any idea what could be causing this? (sorry!)
$args = array(
'post_id' => $user_id,
'submit_value' => 'Update',
'return' => get_permalink(583),
'uploader' => 'wp',
'fields' => array(field_5683c4ad44c9e, // first name
field_5683c4ba44c9f, // last name
field_5677c781d76ae, // email
field_5683c73b75c26, // standard employee fields
field_5683c76375c27,
field_5683c77b75c28,
field_5683c79675c29,
field_5683c7b675c2a,
field_5683c7bf75c2b,
field_5683c7d275c2c,
field_5683c7e775c2d,
field_5683c80275c2e,
field_5683c81f75c2f,
field_5683c83575c30,
field_5683c85375c31,
field_5683c86475c32)
);
acf_form($args);
Not sure what you code looks like not, but for a new user you need to use a different id for wp_insert_user than what you return for ACF. WP needs just the bare user ID while acf needs the prefix of ‘user_’. The same would hold true for updating a user.
Thanks pal. Sorted it like this:
function user_register_pre_save( $post_id ) {
// check if this is to be a new post
if( $post_id != 'new' ) {
preg_match_all('!\d+!', $post_id, $num_id);
$num_id = implode(' ', $num_id[0]);
$num_id = (int)$num_id;
$xuserdata = array(
'ID' => $num_id,
'first_name' => $_POST['acf']['field_567a4d0a839b3'],
'last_name' => $_POST['acf']['field_567a4d10839b4'],
'user_email' => $_POST['acf']['field_567a4c416d6c1'],
'display_name' => $_POST['acf']['field_567a4d0a839b3'] . ' ' . $_POST['acf']['field_567a4d10839b4']
);
wp_update_user($xuserdata);
return $post_id;
}
$userdata = array(
'ID' => $user_id,
'user_login' => $_POST['acf']['field_567a4c3c6d6c0'],
'user_pass' => $_POST['acf']['field_567a4c4c6d6c2'],
'user_email' => $_POST['acf']['field_567a4c416d6c1'],
'first_name' => $_POST['acf']['field_567a4d0a839b3'],
'last_name' => $_POST['acf']['field_567a4d10839b4'],
'display_name' => $_POST['acf']['field_567a4d0a839b3'] . ' ' . $_POST['acf']['field_567a4d10839b4'],
'role' => $_POST['acf']['field_567ac0b49e48b']
);
//register user
$user_id = wp_insert_user($userdata);
$user_id = 'user_'.$user_id;
return $user_id;
}
add_filter('acf/pre_save_post' , 'user_register_pre_save');
You could get all the users and then loop through them to see if there is a match. get_users()
also has a search argument so you can search by several of the user settings. https://codex.wordpress.org/Function_Reference/get_users
<?php if(username_exists( $username )){
//do something
} ?>
<?php if(email_exists( $email )){
//do something
} ?>
Are both handy functions!
The topic ‘ACF Form not saving data’ 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.