Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dwanjuki/2cbf7af2272d55e481683ae8788cbb0e to your computer and use it in GitHub Desktop.

Select an option

Save dwanjuki/2cbf7af2272d55e481683ae8788cbb0e to your computer and use it in GitHub Desktop.
Redirect BuddyBoss/BuddyPress directory and profile pages to PMPro Member Directory Directory and Profile pages.
<?php
/**
* Redirect BuddyBoss/BuddyPress directory and profile pages to PMPro Member Directory
* Directory and Profile pages.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_buddypress_directory_and_profiles_redirect() {
global $pmpro_pages;
// Bail if PMPro or the PMPro Member Directory Add On aren't active.
if ( empty( $pmpro_pages ) || ! defined( 'PMPRO_MEMBER_DIRECTORY_VERSION' ) ) {
return;
}
// If this is the BuddyPress/BuddyBoss directory page.
if ( bp_is_members_directory() ) {
// Set redirect destination to the PMPro Member Directory page if it's set.
if ( ! empty( $pmpro_pages['directory'] ) ) {
$redirect_url = get_permalink( $pmpro_pages['directory'] );
}
} elseif ( bp_is_user_profile() ) { // If this is a BuddyPress/BuddyBoss profile page.
// Get profile user ID.
$profile_user_id = bp_displayed_user_id();
// Bail if the user is viewing their own BuddyPress profile page.
if ( $profile_user_id === bp_loggedin_user_id() ) {
return;
}
// If the Member Directory Profile page is set.
if ( ! empty( $pmpro_pages['profile'] ) ) {
// Get the user's ID.
$profile_user = get_user_by( 'ID', $profile_user_id );
// Set redirect destination to the PMPro profile URL if available.
$redirect_url = pmpromd_build_profile_url( $profile_user );
}
} else {
return;
}
// Redirect if a destination URL was set.
if ( ! empty( $redirect_url ) ) {
wp_redirect( $redirect_url );
exit;
}
}
add_action( 'bp_template_redirect', 'my_buddypress_directory_and_profiles_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment