Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active March 24, 2022 06:56
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/dadcbf03e362785c32b0bf779c619af9 to your computer and use it in GitHub Desktop.
Save ipokkel/dadcbf03e362785c32b0bf779c619af9 to your computer and use it in GitHub Desktop.
Set a custom registration field for PMPro Register Helper as readonly on the profile edit page.
<?php
/**
* Readonly field on profile edit page.
*
* Add profile_readonly to field and set to true.
*
* Fields with this option will remain editable if the field is empty, or
* if the current user can manage options (administrators), or
* if the current user is a membership manager that have a
* membership manager role.
*
* Note: This will override the readonly option set for a field.
*
* @requires Register Helper Add On
* @link https://www.paidmembershipspro.com/add-ons/pmpro-register-helper-add-checkout-and-profile-fields/
*
* 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/
*/
// We have to put everything in a function called on init, so we are sure Register Helper is loaded.
function my_pmprorh_init_profile_read_only() {
// Don't break if Register Helper is not loaded.
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return false;
}
// Define the fields.
$fields = array();
$fields[] = new PMProRH_Field(
'example_readonly', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'Example Read Only', // custom field label
'profile' => true, // show in user profile
'profile_readonly' => true, // readonly on user profile edit page
'readonly' => true,
'required' => true, // make this field required
'addmember' => true, // shows on Admin Add Member page
'memberslistcsv' => true, // include when using export members to csv
)
);
// Add the fields into a new checkout_boxes are of the checkout page.
foreach ( $fields as $field ) {
// Set readonly status on profile if profile_readonly was set to true.
global $current_user;
if ( ( $field->profile || $field->attr['profile'] ) ) {
if ( ( $field->profile_readonly || $field->attr['profile_readonly'] ) && ( empty( get_user_meta( $current_user->ID, $field->meta_key, true ) ) || current_user_can( 'manage_options' ) || current_user_can( 'pmpro_membership_manager' ) ) ) {
$field->readonly = false;
$field->attr['readonly'] = false;
} else {
$field->readonly = true;
$field->attr['readonly'] = true;
}
}
// Add the fields
pmprorh_add_registration_field(
'checkout_boxes', // location on checkout page
$field // PMProRH_Field object
);
}
unset( $field );
// That's it. See the PMPro Register Helper readme for more information and examples.
}
add_action( 'init', 'my_pmprorh_init_profile_read_only' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment