Thank you! Your feedback has been delivered
Thank you! Your feedback has been sent

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 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.

If I var_dump($post_id) it brings back the correct ID for the post I just submitted. Which is really odd.

Any help is really, really appreciated and anything else you need just let me know.

R

User Gravatar

user-1043706

Posted Apr 8 2014 9:54 UTC

$30


  • Assigned To tripflex
  • Solved
  • php
    wordpress
  • 3952 Views

17 Replies


From what my research is showing you should be using an action hook not a filter for what your attempting please see documenation

so your code would be

add_action('save_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);
}
User Gravatar

th3fallen

Posted Apr 8 2014 10:05 UTC

Hi @th3fallen

Unfortunately not. Regardless of using action or filter, it still produces the same results. The post and user is always inserted... this part works fine... but it's not getting the variables I set to insert to the user, except, strangely, the user_login, which uses get_the_title($post_id).

Could it be that the ACF fields are not in the database yet when it creates the user so it has nothing to pull?

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" }
User Gravatar

user-1043706

Posted Apr 8 2014 10:11 UTC

@rdck Sorry i misread the first time yes thats most likely the issue in that case try using http://codex.wordpress.org/Plugin_API/Action_Reference/save_post

User Gravatar

th3fallen

Posted Apr 8 2014 10:16 UTC

Have you tried $_REQUEST instead of get_post_meta ?

User Gravatar

rashidul04

Posted Apr 8 2014 10:31 UTC

The problem is that you still don't have post metas when action is called. Try to use REQUEST as rashidul04 suggested.

User Gravatar

fulippo

Posted Apr 8 2014 10:37 UTC

Can you elaborate @rashidul04? So, for example, instead of:

$email = get_post_meta($post_id, 'email_address', true);

I would put

$email = $_REQUEST['email_address']

Is this correct? Or does $post_id need to be added somewhere?

User Gravatar

user-1043706

Posted Apr 8 2014 10:47 UTC

@rdck the issue is your trying to catch it before the metas are saved did you try using the save_post hook i linked above? i did something similar once and that worked fine for me.

User Gravatar

th3fallen

Posted Apr 8 2014 10:48 UTC

Because you are using ACF you 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, you 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 you're looking for.

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

EDIT added the IF statement

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);
    }
}
User Gravatar

tripflex

Posted Apr 8 2014 10:50 UTC

Solution

This didn't solve your task? Get your own custom solution.

simple:

$firstname = $_REQUEST['fields']['first_name'];

instead of

$firstname = get_post_meta($post_id, 'first_name', true);

You can check var_dump($_REQUEST['fields'])

User Gravatar

rashidul04

Posted Apr 8 2014 10:53 UTC

tripflex solution is better

User Gravatar

rashidul04

Posted Apr 8 2014 10:54 UTC

Okay, I went with @tripflex's solution only because of the related documentation to ACF. I don't doubt that the other solutions wouldn't have worked, at all.

I appreciate all the help. And I am so glad this actually works now. Thank you so much for this. I have spent many days trying to work this out.

-R

User Gravatar

user-1043706

Posted Apr 8 2014 10:56 UTC

Here's it with the If statement:

EDIT: added this to the post above as well

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);
    }
}
User Gravatar

tripflex

Posted Apr 8 2014 10:57 UTC

Thank you so much @triplex. Although adding it with the if statement prevents the user from being created?

User Gravatar

user-1043706

Posted Apr 8 2014 11:05 UTC

Is that the only ACF fields you are using? If so then you don't need the if statement, I wasn't able to test the code but make sure that your custom post type is exactly "People", you may need to change it to "people", you can check by going to the database and looking at the post_type to see.

There's a really good plugin called "Adminer" that will allow you to browse the database, open it up, click on "wp_posts" and then click on the link at the top "select data"

That will show you all of the posts, if you find one of your posts, look at the "post_type" to see exactly what it's being saved as.

You probably just need to change it to a lowercase "people" that was my fault for using it capitalized

If you're still unable to get the if statement to work email me myles AT hostt.net and i'll work through it with you

User Gravatar

tripflex

Posted Apr 8 2014 11:09 UTC

Yep, think you're dead on. The CPT is 'people' But hey, it works and I am forever thankful. Been working on this for days! Really appreciate all the help.

User Gravatar

user-1043706

Posted Apr 8 2014 11:17 UTC

Lowercased 'people' and it worked!

User Gravatar

user-1043706

Posted Apr 8 2014 11:19 UTC

Awesome, and no problem glad I could help :)

User Gravatar

tripflex

Posted Apr 8 2014 11:36 UTC

Add a reply

By posting a reply on CodersClan you agree to our Terms & Conditions