BuddyDev

Search

[Resolved] Redirect users immediately after profile photo upload

  • Participant
    Level: Guru
    Posts: 900
    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: 900
    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: 900
    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: 24623
    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: 900
    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: 24623
    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

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47293

    Tested this again and it now works there was a conflict with perfmatters (https://perfmatters.io/) plugin

    I also noticed that there is a redirect when photo is deleted, how can I stop this the redirect should only work for photo upload and not photo delete.

    Thanks

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47294

    With perfmatters I had defer javascript option enabled, is there a work around?

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #47300

    Hi Tosin,
    You will need to contact perfmatters about the defer.

    Here is slightly modified script to avoid redirect on deletion.

    
    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' && 'deleted' !== data.get('action') ) {
        window.location="<?php echo esc_url( $url ); ?>";
        }
        } );
    	<?php
    	$script = ob_get_clean();
    	wp_add_inline_script( 'bp-avatar', $script, 'after' );
    }, 100 );
    
    

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #47309

    Thanks Brajesh

    The code worked perfectly, I solved the perfmatters issue by disabling defer javascript function when viewing logged in user profile with the code below

     /* Turn off perfmatters jd defer on change avatar page */
    add_filter('perfmatters_defer_js', function($defer) {
          if(bp_is_my_profile()) {
            return false;
        }
        return $defer;
    }); 

    It would have been awesome if their was a function to target only the (change profile photo) page instead of using bp_is_my_profile()

    Alternatively which javascript code does your code work with and where is it located in the buddypress folder maybe I could just exclude the js file path from the defer function

You must be logged in to reply to this topic.

This topic is: resolved