Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrajesh/b3b7346bb3fd5b868a5c3d56e8c9944d to your computer and use it in GitHub Desktop.
Save sbrajesh/b3b7346bb3fd5b868a5c3d56e8c9944d to your computer and use it in GitHub Desktop.
Redirect User after successful profile photo upload on BuddyPress
/**
* Redirect after successful upload of profile photo on BuddyPress.
*/
class BP_Profile_Photo_Successful_Upload_Redirection_Helper {
/**
* Singleton instance.
*
* @var BP_Profile_Photo_Successful_Upload_Redirection_Helper
*/
private static $instance;
/**
* Factory method for singleton.
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
self::$instance->setup();
}
}
/**
* Constructor.
*/
public function __construct() {
$this->setup();
}
/**
* Setup hooks.
*/
private function setup() {
add_action( 'xprofile_avatar_uploaded', array( $this, 'log' ) );
add_action( 'bp_template_redirect', array( $this, 'maybe_redirect' ) );
}
/**
* Save a meta to identify that there was a successfull avatar upload.
*
* @param int $user_id user id.
*/
public function log( $user_id ) {
update_user_meta( $user_id, '_avatar_upload_successful', 1 );
}
/**
* Check and redirect.
*/
public function maybe_redirect() {
if ( ! is_user_logged_in() || ! bp_get_user_meta( get_current_user_id(), '_avatar_upload_successful' ) ) {
return;
}
delete_user_meta( get_current_user_id(), '_avatar_upload_successful' );
$location = site_url('/sample-page');//'http://yoursite.com/some-page/'; // Please replace it. You may use site_url( '/some-page') for dynamic lik.
bp_core_redirect( $location );
}
}
BP_Profile_Photo_Successful_Upload_Redirection_Helper::get_instance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment