Skip to:
Content
Pages
Categories
Search
Top
Bottom

Query runs on x-profile edit, but not on registration


  • zoleest
    Participant

    @zoleest

    Hey,

    I had a problem with a code which I customized from a forum topic, but absolutely have no idea why it doesn’t work.
    I have a query in the code to upgrade the display name for the user on registration, activation, and x-profile edit. Unfortunately it only works on profile editing. The strange thing is that I put another query (a simple INSERT INTO query, which insert the user ID and the new display name into a test table) to test if my code run, and that query runs both registration and activation too. I work with 5.1.1 WordPress version and BuddyPress version 4.2.0. Profile sync is also enabled.

    Here is my code:

    function xprofile_sync_wp_profile2( $user_id = 0 ) {
    	
    
    // Bail if profile syncing is disabled
    	if ( bp_disable_profile_sync() ) {
    		return true;
    	}
    
    	if ( empty( $user_id ) ) {
    		$user_id = bp_loggedin_user_id();
    	}
    
    	if ( empty( $user_id ) ) {
    		return false;
    	}
    
    	global $wpdb;
      
        
        // Get name from x-fields
        $display_name = xprofile_get_field_data('Név',$user_id );
        
    	//Test query to detect if the function run, works every time
    	$wpdb->query("INSERT INTO test (id, name) VALUES ('".$user_id."', '".$display_name."');");
    
            //Query to update display_name, works only on x-profile edit
    	$wpdb->query( $wpdb->prepare( "UPDATE wphu_users SET display_name = '%s' WHERE ID = %d", $display_name,  $user_id ) );
    	
    }
    add_action( 'xprofile_updated_profile', 'xprofile_sync_wp_profile2' );
    add_action( 'bp_core_signup_user',      'xprofile_sync_wp_profile2' );
    add_action( 'bp_core_activated_user',   'xprofile_sync_wp_profile2' );

    Thanks for your help & replies,
    David

Viewing 2 replies - 1 through 2 (of 2 total)

  • Venutius
    Moderator

    @venutius

    Hi, I’m thinking that the user_id is not in the users dp before activation is complete and you might be better hooking on `bp_core_activated_user’ instead?


    zoleest
    Participant

    @zoleest

    Still not worked, but I found out a not too elegant solution. As far as I understand there is an order how the hooks run. There is a hook in class-buddypress.php add_action( 'bp_core_signup_user', array( $this, 'subscribe_from_form' ), 10, 4 ); so I added to my hooks 11, 4 so this is my code now:

    function xprofile_sync_wp_profile2( $user_id = 0 ) {	
    	
    	
    	
    	// Bail if profile syncing is disabled
    	if ( bp_disable_profile_sync() ) {
    		return true;
    	}
    
    	if ( empty( $user_id ) ) {
    		$user_id = bp_loggedin_user_id();
    	}
    
    	if ( empty( $user_id ) ) {
    		return false;
    	}
        
        global $wpdb;
    	
        // Get name from x-fields
        $display_name = xprofile_get_field_data('Név',$user_id );
        
    
        $wpdb->query( $wpdb->prepare( "UPDATE wphu_users SET display_name = '%s' WHERE ID = %d", $display_name,  $user_id ) );
    	
    	
    }
    add_action( 'xprofile_updated_profile', 'xprofile_sync_wp_profile2', 11, 4);
    add_action( 'bp_core_signup_user',      'xprofile_sync_wp_profile2', 11, 4);
    add_action( 'bp_core_activated_user',   'xprofile_sync_wp_profile2', 11, 4);

    And fortunately it works now 🙂 I don’t know if this is a correct way, but work, so I’m okay with it.

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