Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save andrewlimaza/4152e4b699045f0ee71f89026a2ee271 to your computer and use it in GitHub Desktop.

Select an option

Save andrewlimaza/4152e4b699045f0ee71f89026a2ee271 to your computer and use it in GitHub Desktop.
Redirect BuddyBoss Users to "Update Avatar" when this is missing. Allows PMPro pages access for checkout/cancellation etc.
<?php
/**
* Redirect logged-in users with no BuddyBoss/BuddyPress avatar to the
* membership account page so they can upload a profile photo.
*/
function my_redirect_missing_avatar_to_account() {
if ( is_admin() || wp_doing_ajax() || wp_doing_cron() ) {
return;
}
if ( ! is_user_logged_in() ) {
return;
}
if ( ! function_exists( 'bp_get_user_has_avatar' ) || ! function_exists( 'bp_core_get_username' ) ) {
return;
}
// Avoid an infinite redirect loop on the change-avatar screen.
if ( function_exists( 'bp_is_user_change_avatar' ) && bp_is_user_change_avatar() ) {
return;
}
// Don't redirect away from any PMPro page (account, checkout, login, etc.).
global $pmpro_pages;
if ( ! empty( $pmpro_pages ) && is_page( array_values( $pmpro_pages ) ) ) {
return;
}
$user_id = get_current_user_id();
if ( ! bp_get_user_has_avatar( $user_id ) ) {
$username = bp_core_get_username( $user_id );
bp_core_add_message( __( 'Please upload a profile photo to complete your account. This is required to resume access to our site.', 'buddyboss' ), 'error' );
wp_safe_redirect( home_url( '/members/' . $username . '/profile/change-avatar/' ) );
exit;
}
}
add_action( 'template_redirect', 'my_redirect_missing_avatar_to_account' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment