Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active April 13, 2021 18:10
Show Gist options
  • Save strangerstudios/8a6bc0d2aecf015153f6 to your computer and use it in GitHub Desktop.
Save strangerstudios/8a6bc0d2aecf015153f6 to your computer and use it in GitHub Desktop.
Demonstration of the Register Helper "depends" field option.
<?php
function my_pmprorh_depends_fields_init()
{
//don't break if Register Helper is not loaded
if(!function_exists("pmprorh_add_registration_field"))
{
return false;
}
//add a new "About Your Pets" box on checkout form
pmprorh_add_checkout_box("pets", "About Your Pet");
//define the fields
$fields = array();
$fields[] = new PMProRH_Field(
"pet",
"select",
array(
"label"=>"I have a:",
"options"=>array(
"" => "Select",
"cat"=>"Cat",
"dog"=>"Dog"
)));
$fields[] = new PMProRH_Field(
"cat_name",
"text",
array(
"depends"=>array(array('id' => "pet", 'value' => "cat")),
"label"=>"Cat's Name",
));
$fields[] = new PMProRH_Field(
"dog_name",
"text",
array(
"depends"=>array(array('id' => "pet", 'value' => "dog")),
"label"=>"Dog's Name",
));
//add the fields into a new "pets" area of the checkout page
foreach($fields as $field)
pmprorh_add_registration_field(
"pets",
$field
);
}
add_action("init", "my_pmprorh_depends_fields_init");
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Exploring Register Helper: Adding Conditional Fields to Member Profile and Checkout" at Paid Memberships Pro here: https://www.paidmembershipspro.com/exploring-register-helper-adding-conditional-fields-member-profile-checkout/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment