Sauter la navigation

[Résolu] Dynamic Title with Toolset Forms based on Logged in User First and Last Name

Ce fil est résolu. Voici une description du problème et la solution proposée.

This support ticket is created Il y a 5 années et 4 mois. 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par Dallin Chase Il y a 5 années et 4 mois.

Assisté par: Luo Yang.

Auteur
Publications
#1325585

Hey guys,

I have a custom post type for applicants on my website. I'm trying to create a post with a title that is dynamically created from the current logged in users First Name and Last Name. I have it working with a custom field thanks to the code on this page (https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data) but can't seem to get it to work with the users first name and last name.

This is what I was trying.

`//Create a dynamic post title by the CRED form.
add_action('cred_save_data','applicant_post_title',10,2);
function applicant_post_title($post_id,$form_data) {
if ($form_data['id']==929) {
$firstName = get_the_author_meta($post_id, 'first_name', true);
$lastName = get_the_author_meta($post_id, 'last_name', true);
$title= $firstName. '-' . $lastName;
$args = array('ID' => $post_id, 'post_title' => $title);
wp_update_post($args);
}
}`

Any thoughts on how to do this?

I also made a video explaining if that is more helpful: lien caché

#1325593

Hello,

There should be something wrong in your PHP codes, please try to modify these two lines from:

$firstName = get_the_author_meta($post_id, 'first_name', true);
$lastName = get_the_author_meta($post_id, 'last_name', true);

To:

$author_id = get_post_field( 'post_author', $post_id );
$firstName = get_the_author_meta( 'first_name', $author_id);
$lastName = get_the_author_meta( 'last_name', $author_id);

More help:
https://developer.wordpress.org/reference/functions/get_the_author_meta/
https://developer.wordpress.org/reference/functions/get_post_field/

#1325595

Worked like a charm. Thank you Luo!