Skip Navigation

[Resolved] Update a Post title with a Custom Field value on CRED Submit

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can I update the Post title with a Custom Field value on CRED Form submit?

Solution:
You will use the CRED and WordPress API.
This is an example of the Custom Code:
http://pastebin.com/2BTbVbcs

Relevant Documentation:
https://toolset.com/documentation/user-guides/cred-api/#csd
https://codex.wordpress.org/Function_Reference/update_post_meta

This support ticket is created 7 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by Walter Kamer 7 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#453585

I am trying to:
Use the CRED User Forms (eg. ID=250) to add a new user, but I also want to add a new post to my CPT 'UO Member' (with two fields, default field 'Title' and custom field 'ou-member_username'

Eg.
Add new user:
Username: testuser
Password: *****
Firstname: MyFirstname
Lastname: MyLastname

After submitting this information,I also want to create a post in CPT 'UO Members'
(Default field, this is the Member ID) Title: UO<generated number>
(Types field) Username; <username entered in the CRED form)

I know I need to use the use the following function:

add_action('cred_save_data_250', 'my_save_data_action',10,2);
function my_save_data_action($post_id)
{
$postdata=array(
'$post_title' => 'UO12345'
[types field='ou-member_username' => '<username filled in trhough the CRED form>'
)
wp_update_post($postdata)
}

But I don't know how to put the data in my CPT and Custom Types field.

Has anyone done this before?

#453896

Yes, I have done this.

You need to use the WordPress API in the CRED API. This is not Toolset code, but handled by the WordPress API.

The functions to use are here:
https://developer.wordpress.org/reference/functions/wp_insert_post/

Then to update the post with new postmeta, you can use the update_post_meta() function:
https://codex.wordpress.org/Function_Reference/update_post_meta

To get the values to pass, from your just created user, you can use get_user_meta() and get_userdata()
https://codex.wordpress.org/Function_Reference/get_userdata
https://codex.wordpress.org/Function_Reference/get_user_meta

The User ID is the $post_id of the CRED form.

This is a very elaborated example code of "Updating a user Profile and creating a Post with this data on submit":
http://pastebin.com/Y1pWeJnP

This is about updating user fields only, from the front end:
http://pastebin.com/wz6dQVBE

This is for updating a post with certain values from a Custom Field:
http://pastebin.com/2BTbVbcs

Please let me know if you need further help with the CRED API.
For the WordPress API I would suggest to also study the WordPress functions listed above, they will come in handy at further Custom Development you might want to do.

#454995

Thank you very much for you support, I will look at the examples and give it a try.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.