Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active February 24, 2020 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/9f83d2115cce7321d7bc8a31adb99558 to your computer and use it in GitHub Desktop.
Save ipokkel/9f83d2115cce7321d7bc8a31adb99558 to your computer and use it in GitHub Desktop.
Register helper example to make a dependant field required
<?php
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(
"profession", // input name, will also be used as meta key
"select", // type of field
array(
"label" => "I am a…",
"class" => "profession", // custom class
"profile" => true, // show in user profile
"required" => true, // make this field required
"options" => array( // <option> elements for select field
"" => "(Please Select an Option)", // blank option – cannot be selected if this field is required
"physician" => "Physician",
"nurse" => "Nurse",
"healthcare" => "Other Healthcare Provider",
"student" => "Student",
"educator" => "Educator",
"patient" => "Patient",
"family" => "Family Member",
"other" => "Other (Please Specify)"
),
)
);
$fields[] = new PMProRH_Field(
"other_profession",
"text",
array(
"depends" => array( array( "id" => "profession", "value" => "other" )),
"class" => "other-profession", // custom class
"profile" => true, // show in user profile
/* dependant will return required when required is set to true
* for a workaround set the required attribute to false
* enforcing it as required will be handled by a JQuery script
* that we're adding to the footer
*/
"required" => false, // make this field required
)
);
//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' );
/* Add JQuery function to page footer
* to make dependant field required when visiable
*/
function add_to_footer_profession_formfield_check() {
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('#other_profession_div').hide();
// Show fields.
jQuery('#profession').change(function(){
var val = jQuery('#profession').val();
if ( val == 'other') {
jQuery('#other_profession_div').show();
jQuery('#other_profession').prop('required', true).after(' *');; // make the input field required and add * after it.
} else {
jQuery('#other_profession_div').hide();
jQuery('#other_profession').prop('required', false);
}
});
});
</script>
<?php
}
}
add_action( 'wp_footer', 'add_to_footer_profession_formfield_check' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment