Created
June 24, 2024 07:11
-
-
Save ipokkel/40cabee45bc44c2a1db28aebcc6d5260 to your computer and use it in GitHub Desktop.
Fetch and set the value for the display name user field in admin.
This file contains 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 | |
/** | |
* Set the default value for the display_name field when editing a user in the WordPress admin. | |
* | |
* This recipe assumes a PMPro User field for the display_name exists. | |
* | |
* 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 the default value for the display_name field. | |
function pmpro_set_default_value_for_display_name_field( $field, $where ) { | |
// Only set the default value for the display_name field in the admin. | |
if ( is_admin() && current_user_can( 'edit_users' ) ) { | |
// Skip if we are not on a page where a user's ID is passed? | |
if ( ! isset( $_GET['user_id'] ) || empty( $_GET['user_id'] ) ) { | |
return $field; | |
} | |
// get the user's display name | |
if ( 'display_name' === $field->name ) { | |
$user_id = intval( $_GET['user_id'] ); | |
$user_data = get_userdata( $user_id ); | |
if ( $user_data ) { | |
$display_name = $user_data->display_name; | |
$field->value = $display_name; | |
} | |
} | |
} | |
return $field; | |
} | |
add_filter( 'pmpro_add_user_field', 'pmpro_set_default_value_for_display_name_field', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment