Last active
September 29, 2020 19:06
-
-
Save ipokkel/9f606cc75a6876cd86052745ea391e56 to your computer and use it in GitHub Desktop.
This recipe creates custom fields for membership registration and has a custom profile field function that may be used to display a Register Helper field only to membership manager or administrators.
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 custom fields for membership registration and | |
* has a custom profile field function that may be used to display a field | |
* only to membership manager or administrators. | |
* | |
* @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/ | |
*/ | |
function my_pmprorh_init_example_display_field_for_admin_and_membership_manager_only() { | |
// don't break if Register Helper is not loaded | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) { | |
return false; | |
} | |
$admin_and_membership_manager = 'only_admin'; | |
if ( is_user_logged_in() && current_user_can( 'pmpro_membership_manager' ) ) { | |
$admin_and_membership_manager = true; | |
} | |
// define the fields | |
$fields = array(); | |
// TEXT FIELD - Basic Example | |
$fields[] = new PMProRH_Field( | |
'basic_example', // input field name, used as meta key | |
'text', // field type | |
array( | |
'label' => 'Basic Example', // field label | |
'profile' => true, // display on user profile | |
'required' => true, // optional field | |
) | |
); | |
$fields[] = new PMProRH_Field( | |
'admin_and_membership_manager_example', // input field name, used as meta key | |
'text', // field type | |
array( | |
'label' => 'Admin and Manager Only', // field label | |
'profile' => $admin_and_membership_manager, // display on user profile if admin or membership manager | |
'required' => true, // optional field | |
) | |
); | |
$fields[] = new PMProRH_Field( | |
'only_admin_example', // input field name, used as meta key | |
'text', // field type | |
array( | |
'label' => 'Admin Only', // field label | |
'profile' => 'only_admin', // display on user profile | |
'required' => true, // optional field | |
) | |
); | |
foreach ( $fields as $field ) { | |
pmprorh_add_registration_field( | |
'checkout_boxes', // location on checkout page | |
$field // PMProRH_Field object | |
); | |
} | |
// that's it. see the PMPro Register Helper readme for more information and examples. | |
} | |
add_action( 'init', 'my_pmprorh_init_example_display_field_for_admin_and_membership_manager_only' ); |
Ahhh, the || operator would return true therefore the if
statement would ignore whether the user was logged in or whether the page request was an admin page. Two conditionals?
if ( is_user_logged_in() && is_admin() ) {
if ( current_user_can ( 'manage_options' ) || current_user_can ( 'pmpro_membership_manager' ) ) {
<RH Fields Setup>
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thunsis, I can see how this works, but can I check on something (code golf)?
If I just wanted to have a set of RH fields that were displayed only to the Administrator and Membership Manager roles, could I simply wrap the RH field setup in a
current_user_can()
conditional? Logically, something like this:Whatever fields are put inside this conditional would only be setup if the logged in user was an Administrator or Membership Manager, right?
I appreciate that your code gist can be used within a 'standard' RH setup i.e. 'all-in-one', but I already have many different RH setups so that I can position them within different sections of the checkout page.