Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andrewlimaza/6c3ca0320cf318b5c58bf866e0fab3f5 to your computer and use it in GitHub Desktop.
Save andrewlimaza/6c3ca0320cf318b5c58bf866e0fab3f5 to your computer and use it in GitHub Desktop.
Multiple Depends example for Register Helper and Custom JQuery.
<?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