Last active
January 8, 2021 07:44
-
-
Save ipokkel/4c0392099f4b578131484e0fe12e8e05 to your computer and use it in GitHub Desktop.
Set Register Helper field to read only for members but not administrators or membership managers with the readonly field option.
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 | |
/** | |
* This recipe creates a custom fields for membership registration | |
* that is readonly on the user edit profile for members and | |
* editable by administrators or users with the membership manager role. | |
* | |
* This recipe assumes that the registration field is required during checkout. | |
* | |
* @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_read_only_init() { | |
// 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( | |
'phone', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'Example Read Only', // custom field label | |
'profile' => 'read_only', // show in user profile | |
'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 to readonly for the user on the profile page if field is not empty. | |
Administrators and Membership Managers are able to edit the field. | |
*/ | |
if ( 'read_only' === $field->profile || 'read_only' === $field->attr['profile'] ) { | |
if ( is_user_logged_in() && ! empty( get_user_meta( get_current_user_id(), $field->meta_key, true ) ) ) { | |
$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_read_only_init' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment