Using wp_insert_user on a CPT post submission – odd results
-
Hi folks,
I have been stuck on this for days now, so any advice is really appreciated. I am using ACF (Advanced Custom Fields) on a CPT (custom post type).
I have a CPT set up called ‘People’, and upon adding a ‘person’, I am trying to add that person as a user upon publishing too. My CPT all works well, and this is what I have got so far using
add_filter
andwp_insert_user
.add_filter('publish_people', 'people_postdata', 100); function people_postdata($post_id) { global $wpdb; $firstname = get_post_meta($post_id, 'first_name', true); $lastname = get_post_meta($post_id, 'last_name', true); $email = get_post_meta($post_id, 'email_address', true); $password = get_post_meta($post_id, 'password', true); $username = preg_replace('/[^A-Za-z0-9]/', '', strtolower(get_the_title($post_id))); $userargs = array( 'first_name' => $firstname, 'last_name' => $lastname, 'user_login' => $username, 'user_email' => $email, 'user_pass' => $password, 'role' => 'basic' ); // var_dump($userargs); wp_insert_user($userargs); }
Now, if I replace
$post_id
with a real post ID, it all works perfectly. But, using$post_id
to dynamically get the ID of the post I just submitted, it fails to work and leaves first_name, last_name, user_email and user_pass as blank. However, for some reason, user_login works.When I add in
var_dump($userargs)
to see what it’s pulled, it outputs this:array(6) { ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["user_login"]=> string(11) "richardcook" ["user_email"]=> string(0) "" ["user_pass"]=> string(0) "" ["role"]=> string(5) "basic" }
I have tried using
get_the_ID()
, andget_field
but they all have the same result.Any ideas? I find it odd that it works on
get_the_title
but nothing else. Also, if Ivar_dump($post_id)
it brings back the right ID… but is not using it!Any help is really, really appreciated and anything else you need just let me know.
Many thanks,
R
- The topic ‘Using wp_insert_user on a CPT post submission – odd results’ is closed to new replies.