Skip Navigation

[Résolu] How do I populate user field with permalink?

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

Problem: I have a Form that is used to create a new post. I also have a custom field on the User's profile that will be used to hold a URL. I would like to take the permalink URL of the new post and save it in the User URL field using the Forms API cred_submit_complete.

Solution: Use update_user_meta to set the value of a User custom field. Use get_permalink to get the permalink of the new post that was just created.

add_action('cred_submit_complete', 'after_save_data_form_529',10,2);
function after_save_data_form_529($post_id, $form_data) {
      
    if ($form_data['id']==529) {
      
    $user_id = get_current_user_id();   
    $permalink = get_permalink($post_id); 
    update_user_meta( $user_id, 'wpcf-post-slug', $permalink);
    }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://developer.wordpress.org/reference/functions/get_permalink/
https://codex.wordpress.org/Function_Reference/update_user_meta

This support ticket is created Il y a 6 années et 3 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Marqué : 

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

Dernière mise à jour par julieP Il y a 6 années et 3 mois.

Assisté par: Christian Cox.

Auteur
Publications
#1080238

Title says it all!

The user will only ever create one instance of the post type. I've tried like this but it's not working:-

add_action('cred_submit_complete', 'after_save_data_form_529',10,2);
function after_save_data_form_529($post_id, $form_data) {
	
    if ($form_data['id']==529) {
	
	$user_id = get_current_user_id();	
	$slug = get_post_field($post_id, 'post_name', true); 
	add_user_meta( $user_id, 'wpcf-post-slug', $slug);
	}
}

Any ideas please? Thanks

#1080677

Are you trying to save the permalink, or the post slug? The title of this ticket says permalink, but the code says slug. If you want the permalink, try this:

add_action('cred_submit_complete', 'after_save_data_form_529',10,2);
function after_save_data_form_529($post_id, $form_data) {
     
    if ($form_data['id']==529) {
     
    $user_id = get_current_user_id();   
    $permalink = get_permalink($post_id); 
    update_user_meta( $user_id, 'wpcf-post-slug', $permalink);
    }
}
#1081121

Sorry that was a bit ambiguous! I had in mind to use the post slug but I can work with either so this does just what I need, thank you.

I'm still curious as to why my code to extract the slug didn't work. It would be handy to know as I have another requirement where using the slug would be preferable to the permalink.

Many thanks

#1081402

Maybe it was the third parameter of your get_post_field call. "true" is not an accepted value, according to the documentation here:
https://codex.wordpress.org/Function_Reference/get_post_field

#1081477

Ah, yes - thank you!!

Sometimes I can't see the wood for the trees 🙂