Home Forums Plugin Support WP User Frontend Pro Problem saving a field to another

This topic is: not resolved

This topic contains 6 replies, has 2 voices, and was last updated by  Tareq Hasan 5 years, 5 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #14008

    Danilo
    Participant
    Post count: 43

    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?

    #14012

    Tareq Hasan
    Keymaster
    Post count: 2831

    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.

    #14013

    Danilo
    Participant
    Post count: 43

    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?

    #14015

    Tareq Hasan
    Keymaster
    Post count: 2831

    Take a look at wp_set_post_terms() function.

    #14018

    Danilo
    Participant
    Post count: 43

    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…

    #14029

    Danilo
    Participant
    Post count: 43

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

    #14042

    Tareq Hasan
    Keymaster
    Post count: 2831

    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 7 posts - 1 through 7 (of 7 total)

The topic ‘Problem saving a field to another’ is closed to new replies.