Skip Navigation

[Resolved] How to update a custom field (in a CPT post) with data from a User Form?

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by KeithM7209 2 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#2019459

Tell us what you are trying to do?
I have a custom post type ('Clients'), and I also have a user role setup for Clients.
In my 'Client' post type, I have a custom field of 'First Name'.

I have a user form setup which allows me to set up a new user (with the user role of Client), and at the same time as doing this, I also want my form to create a post (under my custom post type 'Client'), using the same details which are captured by the user form.
I want for the post title of my 'Client' post to be the email address captured by my user form. And I also want my 'First Name' custom field to be populated with the First Name captured by the user form when setting up the user.

I have been successful is some ways, but unsuccessful in others - using the code snippet here (https://toolset.com/forums/topic/auto-create-custom-post-after-new-user-submission/#post-493092) I am able to setup the 'Client' post which has the Post Title of the email address captured by the user form.
However, I am unable to get the 'First Name' custom field (in my Client post) to populate with the First Name from my user form. I am aware that the code snippet above does not contain anything to do with custom fields, so I also found this thread (https://toolset.com/forums/topic/using-woocommerce-and-cred_save_data-to-create-a-custom-post-type/) which seemed to suggest how to auto-populate a custom field, however I cannot get it to work (like I say, after I submit my user form, the Client 'User' is created, and also the Client 'Post' is created - also the Post Title of the Client Post is being populated with the email address, however my custom field of 'First Name' is NOT being populated with anything, when it should be getting populated with the 'First Name' from the User Form.

I hope that all makes sense! 🙂

Here is the code which I am using in my functions.php file:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{

if ($form_data['id']==146)
{
// Create post object
$my_post = array(
'post_title' => $_POST['user_email'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $post_id,
'post_type' => 'client'
);

// Insert the post into the database
wp_insert_post( $my_post );

// Update any custom fields on Client Post
$clientFirstName = get_user_meta( $user_id, $_POST['first_name'], true );

update_post_meta( $post_id, 'client-first-name', $clientFirstName );

}
}

** 146 is the ID of my User Form
** client-first-name is the slug of my custom field in my 'Client' custom post type

Can you tell me what it is that I am doing wrong?

Thank you very much for your help,

Keith

Is there any documentation that you are following?

https://toolset.com/forums/topic/auto-create-custom-post-after-new-user-submission/#post-493092
https://toolset.com/forums/topic/using-woocommerce-and-cred_save_data-to-create-a-custom-post-type/

Is there a similar example that we can see?
No

What is the link to your site?
Under development but can provide access if needed (please allow me to enter these in a private box), thanks 🙂

#2019585

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hi, I'll be glad to help. It looks like you've got a User Form set up and you're trying to copy some information from that Form submission into a custom post generated programmatically. I see a few potential problems here.

You need to know the post ID of the $my_post submission, so you can update it correctly later. To do that, create a variable like this when you insert $my_post, then use that variable when updating the custom field value in the new post:

// Insert the post into the database and save new Client post ID
$new_post_id = wp_insert_post( $my_post );

If you want to get the "first_name" Types custom field value from the User's profile, you access it like this:

$clientFirstName = get_user_meta( $post_id, 'wpcf-first_name', true );

Note that $post_id in this case refers to the User ID created by the Form, not the new Client post ID. This also assumes the first name field slug is first_name. If not, replace first_name with the slug of the custom field value you want to retrieve. Types field slugs use a wpcf- prefix in the database, so it is required here and again in the update_post_meta function:

update_post_meta( $new_post_id, 'wpcf-client-first-name', $clientFirstName );

So putting it all together, I think you need something like this:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{

if ($form_data['id']==146)
{
// Create post object
$my_post = array(
'post_title' => $_POST['user_email'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $post_id,
'post_type' => 'client'
);

// Insert the post into the database and save new Client post ID
$new_post_id = wp_insert_post( $my_post );

// Update any custom fields on Client Post
$clientFirstName = get_user_meta( $post_id, 'wpcf-first_name', true );
update_post_meta( $new_post_id, 'wpcf-client-first-name', $clientFirstName );

}
}

If that's not working as expected, I'll need a bit more information about the first name field in the User form.

#2026201

Hi Christian, thank you so much for your help. The code snippet you gave me wasn't quite correct, but with some testing I got it to work - it seems that in $clientFirstName, the field of 'wpcf-first_name' is incorrect (I'm guessing because First Name is not a custom field, but instead it is in fact a core WordPress user field). So soon as I changed this to just 'first_name', then it worked 🙂
Thanks again for your help, much appreciated 🙂

#2026209

My issue is resolved now. Thank you!