Forked from MaryOJob/my_pmpro_custom_select_and_text_fields_to_register_helper.php
Last active
January 8, 2020 10:15
-
-
Save ipokkel/265948d2a1a6ab94aa25e9c42898fa9a to your computer and use it in GitHub Desktop.
Create custom Register Helper fields and hook them into a different location to display in Signup Shortcode #pmpro #signup #shortcode
This file contains hidden or 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 code example serves as an experimental example. | |
The preferred method of showing custom fields in the signup shortcode is to use the shortcode attribute custom_fields | |
[pmpro_signup submit_button="Unlock this Post Now!" level="1" login="1" redirect="referrer" custom_fields="true"] | |
See - https://www.paidmembershipspro.com/add-ons/pmpro-signup-shortcode/ | |
*/ | |
/** | |
* Register Helper example for a select and text field. | |
* Please add the below code to your custom plugin or Code Snippets Plugin by following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmprorh_init() { | |
//don't break if Register Helper is not loaded | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) { | |
return false; | |
} | |
$fields = array(); | |
$fields[] = new PMProRH_Field( | |
'school_or_organization_1', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'School or Organization', | |
'required' => true, // make this field required | |
'profile' => true, // show in user profile | |
) | |
); | |
$fields[] = new PMProRH_Field( | |
'school_or_organization_type', // input name, will also be used as meta key | |
'select', // type of field | |
array( | |
'label' => 'School or Organization Type', | |
'required' => true, // make this field required | |
'profile' => true, // show in user profile | |
'options' => array( // <option> elements for select field | |
'' => 'Please Select', // blank option - cannot be selected if this field is required | |
'school or educator' => 'School or Educator', | |
'ag host' => 'AG Host', | |
'business' => 'Business', | |
), | |
) | |
); | |
// set default location | |
$location = 'checkout_boxes'; | |
// change location to after_email to display in signup-shortcode | |
/* note: my_pmprorh_init needs to be hooked in at earliest at `wp` hook, | |
* to correctly return the bool query `is_page( $pmpro_pages['checkout'] )`, | |
* which runs later than the default `init` hook used to call register helper | |
*/ | |
global $pmpro_pages; | |
if ( function_exists( 'pmprosus_load_shortcode' ) && ! is_admin() && ! is_page( $pmpro_pages['checkout'] ) ) { | |
$location = 'after_email'; | |
} | |
//add the fields into a new checkout_boxes are of the checkout page | |
foreach ( $fields as $field ) { | |
pmprorh_add_registration_field( | |
$location, // location on checkout page | |
$field // PMProRH_Field object | |
); | |
} | |
//that's it. see the PMPro Register Helper readme for more information and examples. | |
} | |
add_action( 'wp', 'my_pmprorh_init' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment