This topic contains 6 reply and 2 voices, and was last updated by Tareq Hasan 5 years, 10 months ago
Viewing 6 Posts - 1 through 6 (of 6 total)
Author Posts
December 29, 2013 at 1:51 am 14008
Tareq Hasan Hi. Few weeks ago I wrote a topic about populating a field with username. I retrieve an article talking about hooks: it works, but in this case I get a problem: - In the article doesn't appear how can I read username data - With that example, I can't save username data like a post tag Basically, I need to read username data from logged in users and write it just like a post tag. Any suggestion to do this?
December 29, 2013 at 2:10 am 14012
Tareq Hasan Tareq Hasan

You can get the current logged in users username like this:
[php]
$current_user = wp_get_current_user();
$user_name = $current_user->user_login;
[/php]

I am not sure what else informations do you need.

December 29, 2013 at 2:31 am 14013
Danilo Danilo

Thank you very much for quick reply, but after this, I can save it in a custom field, but not like a post tag.
Is it possible to save retrieved username like a post tag?

December 29, 2013 at 11:56 am 14015
Tareq Hasan Tareq Hasan

Take a look at wp_set_post_terms() function.

December 29, 2013 at 7:17 pm 14018
Danilo Danilo

I’ll try to get the code.
Thank you.

It would be good to create a new section on documentation with snippets for no-programmers…
Just only a suggestion…

December 30, 2013 at 2:42 am 14029
Danilo Danilo

Sorry, I can’t update post tags from given username.

December 30, 2013 at 12:57 pm 14042
Tareq Hasan Tareq Hasan

Try like this:
[php]
function wpufe_update_tags( $post_id ) {
$current_user = wp_get_current_user();
$user_name = $current_user->user_login;

wp_set_post_terms( $post_id, array( $user_name ), ‘post_tag’ );
}

add_action( ‘wpuf_add_post_after_insert’, ‘wpufe_update_tags’ );
add_action( ‘wpuf_edit_post_after_update’, ‘wpufe_update_tags’ );
[/php]

Haven’t tested it, so not sure if it’ll work.

Viewing 6 Posts - 1 through 6 (of 6 total)