Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created December 13, 2024 09:59
Show Gist options
  • Save ipokkel/79f0c42fc9c4f3b22ba003b7abaee86d to your computer and use it in GitHub Desktop.
Save ipokkel/79f0c42fc9c4f3b22ba003b7abaee86d to your computer and use it in GitHub Desktop.
Set specific user fields to only show for some levels. This can be used to make individual fields level specific if the fields were created via settings in a field group that displays for all levels.
<?php
/**
* Change user fields to level only.
*
* 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_change_user_fields_to_level_only( $field, $where ) {
// What fields do we want to change?
$level_specific_fields = array( 'field_name_1', 'field_name_2', 'checkbox_group_example' ); // Add your field names here.
// What levels should be able to see these fields?
$levels = array( 2, 3 ); // Add your level IDs here.
// Is this a level specific field?
if ( in_array( $field->name, $level_specific_fields, true ) && ! empty( $levels ) && is_array( $levels ) ) {
// Set levels attribute to only display in the user profile.
$field->levels = $levels;
}
return $field;
}
add_filter( 'pmpro_add_user_field', 'my_pmpro_change_user_fields_to_level_only', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment