Support

Account

Home Forums General Issues CPT and adding a user using ACF

Solved

CPT and adding a user using ACF

  • Hi folks,

    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_field('first_name', $post_id);
    	$lastname = get_field('last_name', $post_id);
    	$username = preg_replace('/[^A-Za-z0-9]/', '', strtolower(get_the_title($post_id)));
    	$email = get_field('email_address', $post_id);
    	$password = get_field('password', $post_id);
        $userargs = array(
        	'first_name' => $firstname,
        	'last_name' => $lastname,
        	'user_login' => $username,
        	'user_email' => $email,
        	'user_password' => $password,
        	'role' => 'basic'
        );
        var_dump($userargs);
    	$status = wp_insert_user($userargs);  
    	if ( is_wp_error($userargs) )  {
    		echo "User not created.";
    	} else { 
    		echo "User created.";
    	}
    }

    Now, if I change the $userargs to something obvious in the code to test with, it all works. But, using the variables below I have set, it doesn’t seem to work and fails.

    $firstname = get_field('first_name', $post_id);
    $lastname = get_field('last_name', $post_id);
    $username = preg_replace('/[^A-Za-z0-9]/', '', strtolower(get_the_title($post_id)));
    $email = get_field('email_address', $post_id);
    $password = get_field('password', $post_id);

    I have added a var_dump($userargs); before inserting the user and as you can see below in it’s output (using the name ‘Nathan Barley)… it doesn’t seem to be getting the actual fields. Could this be that it’s firing too quickly? Or could it be that get_field(''), $post_id is wrong? You can see that user_login is working, which uses get_the_title but get_field doesn’t.

    array(6) { ["first_name"]=> bool(false) ["last_name"]=> bool(false) ["user_login"]=> string(12) "nathanbarley" ["user_email"]=> bool(false) ["user_password"]=> bool(false) ["role"]=> string(5) "basic" }

    I have tried hard-coding a post ID into the people_postdata function, so for example, $firstname = get_field('first_name', 1421) and this works. So, for some reason, the ID isn’t getting fetched. However, putting var_dump('$post_id') right before this variable, and it fetches the right ID.

    Any bright ideas on where I’m going wrong?

  • After many days, I managed to solve this (although no thanks to this support forum which seems almost deprecated now)

    As I am using ACF, I should use the built-in acf/save_post to do what I want after the data is saved. As this action will be called every time a post is created/saved, etc, I also needed to add an if statement to check if it’s the post type I’m 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;
    	if ( $post_id && (get_post_type($post_id) == 'People') ) {
    		$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 2 posts - 1 through 2 (of 2 total)

The topic ‘CPT and adding a user using ACF’ is closed to new replies.