Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active November 9, 2021 13:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/dba301ec5118887014baa1c72dc03002 to your computer and use it in GitHub Desktop.
Save ipokkel/dba301ec5118887014baa1c72dc03002 to your computer and use it in GitHub Desktop.
Redirect to the BuddyPress profile page of a user when a visitor clicks on the "View Profile" link in the PMPro Members Directory page.
<?php
/**
* This recipe changes the PMPro Member Directory's "View Profile"
* link on the directory page to the BuddyPress member directory and
* redirects to the BuddyPress Profile page of the member if the
* URL parameter "pu" is present in the querystring.
*
* 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/
*/
// Set profile URL to BuddyPress Members Directory
function my_pmpromd_profile_url_buddypress( $url ) {
// Only change the URL if BuddyPress and our custom redirect function exists.
if ( function_exists( 'bp_get_members_directory_permalink' ) && function_exists( 'my_buddpress_directory_to_profile_redirect' ) ) {
$url = bp_get_members_directory_permalink();
}
return $url;
}
add_filter( 'pmpromd_profile_url', 'my_pmpromd_profile_url_buddypress' );
function my_buddpress_directory_to_profile_redirect() {
// Bail if any of the required parts are missing.
if ( ! function_exists( 'bp_is_directory' ) || ! function_exists( 'bp_core_get_user_domain' ) || ! function_exists( 'my_pmpromd_profile_url_buddypress' ) || empty( $_REQUEST['pu'] ) ) {
return;
}
// Are we on the BuddyPress member directory page.
if ( bp_is_directory() ) {
// Check for the required URL parameter.
$pu = $_REQUEST['pu'];
// Get user
if ( ! is_numeric( $pu ) ) {
$user = get_user_by( 'login', $pu ); // get user with login name
$user = $user ? $user : get_user_by( 'slug', $pu ); //get user by slug
} else {
$user = get_user_by( 'ID', $pu ); // get user with User ID
}
// Bail if user doesn't exist
if ( empty( $user ) ) {
return;
}
}
// Get the BuddyPress member profile URL.
if ( function_exists( 'bp_core_get_user_domain' ) ) {
$url = bp_core_get_user_domain( $user->ID );
}
// Bail if necessary
if ( empty( $url ) ) {
return;
}
// Redirect to BuddyPress profile page.
wp_safe_redirect( $url );
exit;
}
add_action( 'template_redirect', 'my_buddpress_directory_to_profile_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment