Navigation überspringen

[Gelöst] How do I populate user field with permalink?

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 vor 6 Jahren, 4 Monaten. 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
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)

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von julieP vor 6 Jahren, 4 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#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 🙂