• Resolved richgcook

    (@richgcook)


    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 and wp_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(), and get_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 I var_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

Viewing 1 replies (of 1 total)
  • Thread Starter richgcook

    (@richgcook)

    After many days, I managed to solve this.

    Because I am using ACF, I should use the built-in action to do what you want after the data is saved.

    Because this action will be called every time a post is created/saved, etc, I will probably need to add an if statement to check if it’s the post type you’re looking for.

    All of the fields should be available via $_POST['fields'] as well if get_post_meta does not return what I’m looking for.

    The modified code below uses the ACF action AFTER the post has been saved, so the meta should be available. I can do a var_dump on the $_POST['fields'] to see what POST data is available as well.

    http://www.advancedcustomfields.com/resources/actions/acfsave_post/

    add_action('acf/save_post', 'people_postdata', 20);
    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);
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Using wp_insert_user on a CPT post submission – odd results’ is closed to new replies.