Created
April 25, 2022 14:21
-
-
Save ipokkel/d4504c988e739ecaa4da223dc6016cea to your computer and use it in GitHub Desktop.
Override the display name for a user in the PMPro Member Directory and Profile pages by changing the display name to user meta, e.g. company name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This recipe changes a user's display name on directory and | |
* profile pages with custom meta data if the data is available. | |
* | |
* This requires the Member Directory and Profile Pages Add On version 1.0 or later. | |
* https://www.paidmembershipspro.com/add-ons/member-directory/ | |
* | |
* A use case for this would be to display a company name instead of the user's name(s). | |
* | |
* 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_pmpro_member_directory_custom_display_name( $display_name, $user ) { | |
#--- SETTINGS ---# | |
$meta_key = 'company_name'; // change this to the meta key you want to use | |
/* That's it, no further editing required */ | |
// Get user meta | |
$custom_name = get_user_meta( $user->ID, $meta_key, true ); | |
// If we have a custom name, use it | |
if ( $custom_name ) { | |
// Let's override the display name with the custom name. | |
$display_name = $custom_name; | |
} | |
return $display_name; | |
} | |
add_filter( 'pmpro_member_directory_display_name', 'my_pmpro_member_directory_custom_display_name', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment