Created
August 29, 2018 14:04
-
-
Save andrewlimaza/6c3ca0320cf318b5c58bf866e0fab3f5 to your computer and use it in GitHub Desktop.
Multiple Depends example for Register Helper and Custom JQuery.
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 is a simple example that references a select drop down and when a certain option is selected will it show. | |
* Add this code to your PMPro Customizations and tweak to your liking. | |
*/ | |
function my_pmprorh_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( | |
'depends_1', | |
'select', | |
array( | |
'label' => 'Select Something', | |
'profile' => true, | |
'options' => array( | |
'' => 'Select one', | |
'm' => 'male', | |
'f' => 'female', | |
'o' => 'other' | |
) | |
) | |
); | |
$fields[] = new PMProRH_Field( | |
'depends_2', | |
'text', | |
array( | |
'label' => 'This Depends', | |
'profile' => true, | |
'required' => false, | |
) | |
); | |
//add the fields into a new checkout_boxes are of the checkout page | |
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' ); | |
function add_to_footer_example() { | |
global $pmpro_pages; | |
// Only load script on checkout page. | |
if ( is_page( $pmpro_pages['checkout'] ) ) { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function(){ | |
// Hide fields by default that may depend on something. | |
jQuery('#depends_2_div').hide(); | |
// Show fields. | |
jQuery('#depends_1').change(function(){ | |
var val = jQuery('#depends_1').val(); | |
switch( val ) { | |
case 'o': | |
jQuery('#depends_2_div').show(); | |
jQuery('#depends_2').prop('required', true).after(' *');; // make the input field required and add * after it. | |
break; | |
case 'f': | |
jQuery('#depends_2_div').hide(); | |
jQuery('#depends_2').prop('required', false); | |
break; | |
case 'm': | |
jQuery('#depends_2_div').hide(); | |
jQuery('#depends_2').prop('required', false); | |
break; | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} | |
add_action( 'wp_footer', 'add_to_footer_example' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment