BuddyDev

Search

Redirect users immediately after profile photo upload

  • Participant
    Level: Guru
    Posts: 661
    Tosin on #44904

    Hello,

    Please is it possible to redirect users to a page immediately they upload their profile photo.

    Example
    – User A uploads and crops photo
    – after successful upload redirect user to page (invite-friends)

  • Participant
    Level: Guru
    Posts: 661
    Tosin on #44942

    I saw this in a support thread

     
    function redirectAfterAvatarUpload() {
        $location = '/members/'.bp_core_get_username(bp_loggedin_user_id()).'/profile/edit/group/1/';
        bp_core_redirect($location);
        return;
    }
    add_action('xprofile_avatar_uploaded','redirectAfterAvatarUpload' );
    
     
  • Participant
    Level: Guru
    Posts: 661
    Tosin on #44943

    I also tried this but did not work

    It also seems that profile photo is uploaded via ajax so would a redirect be possible

    
     function redirect_after_avatar_upload() {
    
    	if ( ! function_exists( 'buddypress' ) ) {
    		return;
    	}
    	// check for user's last activity.
    	$last_activity = bp_get_user_last_activity( $user->ID );
    	
    	if ( empty( $last_activity ) ) {
        $page_url = site_url( '/onboard-invite-friends/' );
        bp_core_redirect($page_url);
    }
    }
    add_action('xprofile_avatar_uploaded','redirect_after_avatar_upload' ); 
    
  • Keymaster
    (BuddyDev Team)
    Posts: 21517
    Brajesh Singh on #44946

    Hi Tosin,
    Thank you for the question.

    This needs to be done on client side(via js).

    Please remove the above code that you are using and add the followj g php code

    
    
    /**
     * Redirect user on successful php upload and crop.
     */
    add_action( 'bp_enqueue_scripts', function () {
        // you can set the url value to anything.
    	$url = trailingslashit( trailingslashit( bp_get_members_directory_permalink() ) . 'me/' . bp_get_members_invitations_slug() );
    	ob_start();
    	?>
        bp.Avatar.Attachment.on( 'change:url', function( data ) {
            if( data.get('object') == 'user' ) {
                window.location="<?php echo esc_url( $url ); ?>";
            }
        } );
    	<?php
    	$script = ob_get_clean();
    	wp_add_inline_script( 'bp-avatar', $script, 'after' );
    }, 100 );
    

    That will take care of your redirect. Please let me know if it works for you or not?

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 661
    Tosin on #44949

    It did not work after photo upload

    This is my code

     add_action( 'bp_enqueue_scripts', function () {
        // you can set the url value to anything.
    	$url = site_url( '/refer-friends/' );
    	ob_start();
    	?>
        bp.Avatar.Attachment.on( 'change:url', function( data ) {
            if( data.get('object') == 'user' ) {
                window.location="<?php echo esc_url( $url ); ?>";
            }
        } );
    	<?php
    	$script = ob_get_clean();
    	wp_add_inline_script( 'bp-avatar', $script, 'after' );
    }, 100 ); 
  • Keymaster
    (BuddyDev Team)
    Posts: 21517
    Brajesh Singh on #44952

    Please try on a fresh install. If you have some redirect code, the js event may not be firing.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved