Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to set display_name = xprofile ‘custom_field’ at the user registration


  • MorgunovVit
    Participant

    @morgunovvit

    Hi, everybody!

    Could you help me to set set display_name = xprofile ‘first_name’ + ‘last_name’ at the user registration?
    I’ve tryed this code:

    
    function change_user_defaults($user_id) {
    	$user = get_userdata($user_id);
            $mv_first_name = xprofile_get_field_data(  26,  $user_id ); 
            $mv_last_name = xprofile_get_field_data(  11,  $user_id );  
            $name = $mv_first_name . ' ' . $mv_last_name; 
            $mv_login = $user_id . stristr($user->user_email, '@', true); 
            if ($name !== ' '){
                $args = array(
                        'ID' => $user_id,
                        'display_name' => $name
                );
                wp_update_user( $args );
            }
    }
    
    add_action( 'xprofile_updated_profile', 'change_user_defaults' ); /* but this works only after manual BuddyPress profile update */
    
    /* I also tried these hooks too */
    add_action( 'bp_core_signup_user',      'change_user_defaults' ); 
    add_action( 'bp_core_activated_user',   'change_user_defaults' );
    /* but it didn't work at the user registration  */
    
Viewing 6 replies - 1 through 6 (of 6 total)

  • shanebp
    Moderator

    @shanebp

    Instead of a register hook, try using an activation hook:
    do_action( 'bp_core_activated_user', $user_id, $key, $user );

    So something like:

    function change_user_defaults( $user_id, $key, $user ) {
        // whatever you need to do
    }
    add_action( 'bp_core_activated_user', 'change_user_defaults', 99, 3 );

    MorgunovVit
    Participant

    @morgunovvit

    Hi, shanebp!
    Thank you for answering the question!

    Could you explain a point: “Instead of a register hook, try using an activation hook”?
    Where I am to use do_action( ‘bp_core_activated_user’, $user_id, $key, $user ); ?

    And why do you think, that I register hook?


    MorgunovVit
    Participant

    @morgunovvit

    So, it seems that I’ve done it!
    Thanks shanebp for the tip.

    Here’s the code of function for changing display-name:

    add_action( 'bp_core_activated_user', 'change_user_defaults', 99, 3 ); 
    function change_user_defaults($user_id, $key, $user) {
    
        if (empty($user_id)) {
            $user_id = bp_loggedin_user_id();
        }
    
        if (empty($user_id)) {
            return false;
        }
    
        $mv_first_name = xprofile_get_field_data(26, $user_id);
        $mv_last_name = xprofile_get_field_data(11, $user_id); 
        $name = $mv_first_name . ' ' . $mv_last_name; 
        
        
        if ($name !== ' ') {
            $args = array(
                'ID' => $user_id,
                'display_name' => $name
            );       
            wp_update_user($args);
        }
    }

    ankerdesign
    Participant

    @ankerdesign

    I am trying to do the same, can you let me know where to paste this code?


    MorgunovVit
    Participant

    @morgunovvit

    Hi!
    Just put it in a functions.php file of your child-theme.


    ali
    Participant

    @kusatra

    I am trying to do the similar thing, I want to set the display name as the name and last name fields of the xprofile. But it does not work.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar