Skip to content

Instantly share code, notes, and snippets.

@eighty20results
Created May 5, 2016 13:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eighty20results/21f179b4e39d36f5ff175cad90a92b27 to your computer and use it in GitHub Desktop.
Save eighty20results/21f179b4e39d36f5ff175cad90a92b27 to your computer and use it in GitHub Desktop.
Create Register Helper fields & sync them with the corresponding BuddyPress fields
<?php
/*
Plugin Name: PMPro Customizations
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/
Description: Customizations for Paid Memberships Pro
Version: .2
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
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(
"name", // input name, will also be used as meta key
"text", // type of field
array(
"label" => "Name", //label
"size" => 40, // input size
"class" => "name pmpro-custom-field", // custom class
"profile" => true, // show in user profile
"required" => true // make this field required
));
$fields[] = new PMProRH_Field(
"gender", // input name, will also be used as meta key
"select", // type of field
array(
"label" => "Gender", //label
"options" => array(
"" => "—-", // blank option – cannot be selected if this field is required
"Male" => "Male", // <option value="male">Male</option>
"Female" => "Female" // <option value="female">Female</option>
),
"profile" => true, // show in user profile
"class" => "company pmpro-custom-field", // add a class
"required" => true // make this field required
));
$fields[] = new PMProRH_Field(
"country", // input name, will also be used as meta key
"select", // type of field
array(
"label" => "Country", //label
"options" => array(
"Abkhazia" => "Abkhazia",
"Afghanistan" => "Afghanistan",
"Aland" => "Aland",
"Albania" => "Albania",
),
"profile" => true, // show in user profile
"class" => "company pmpro-custom-field", // add a class
"required" => true // make this field required
));
//add the fields into after email position on checkout page
foreach ($fields as $field) {
pmprorh_add_registration_field(
"after_email", // 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");
/*
Sync PMPro fields to BuddyPress profile fields.
*/
function pmprobuddy_update_user_meta($meta_id, $object_id, $meta_key, $meta_value)
{
//make sure buddypress is loaded
if (! defined('BP_VERSION')) {
return;
}
//array of user meta to mirror
$um = array(
"name" => "Name",
"gender" => "Gender",
"country" => "Country", //usermeta field => buddypress profile field
);
//check if this user meta is to be mirrored
foreach ($um as $umeta_field => $bp_field) {
if ($meta_key == $umeta_field) {
//find the buddypress field
$field = xprofile_get_field_id_from_name($bp_field);
//update it
if (!empty($field)) {
xprofile_set_field_data($field, $object_id, $meta_value);
}
}
}
}
add_action('updated_user_meta', 'pmprobuddy_update_user_meta', 10, 4);
add_action('added_user_meta', 'pmprobuddy_update_user_meta', 10, 4);
/*
* // need to add the meta_id for add filter
function pmprobuddy_add_user_meta($meta_id = null, $object_id, $meta_key, $meta_value)
{
pmprobuddy_update_user_meta($meta_id, $object_id, $meta_key, $meta_value);
}
*/
// adds user to level when they sign up via register page
function my_user_register($user_id)
{
if (function_exists('pmpro_changeMembershipLevel')) {
pmpro_changeMembershipLevel(2, $user_id);
}
}
add_action('user_register', 'my_user_register');
@ittyart
Copy link

ittyart commented Mar 28, 2017

when i create additional fields that are required & go to test the field it says that i didnt answer those questions. any way you can help.. my code below:

<?php
/*
Plugin Name: Register Helper Example
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/
Description: Register Helper Initialization Example
Version: .1
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
//we have to put everything in a function called on init, so we are sure Register Helper is loaded
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(
        "name", // input name, will also be used as meta key
        "text", // type of field
        array(
            "label" => "Name",    //label
            "size" => 40, // input size
            "class" => "name pmpro-custom-field", // custom class
            "profile" => true, // show in user profile
            "required" => true // make this field required
        ));

    $fields = array();
    $fields[] = new PMProRH_Field(
        "birth month (mm)",              // input name, will also be used as meta key
        "text",                 // type of field
        array(
            "size"=>40,         // input size
            "class"=>"month", // custom class
            "profile"=>true,    // show in user profile
	    "required"=>true    // make this field required
        ));
    $fields[] = new PMProRH_Field(
	"birth year (yyyy)",              // input name, will also be used as meta key
        "text",                 // type of field
        array(
            "size"=>40,         // input size
            "profile"=>true,    // show in user profile
	    "required"=>true    // make this field required
        ));
    $fields[] = new PMProRH_Field(
        "gender", // input name, will also be used as meta key
        "select", // type of field
        array(
            "label" => "Gender",    //label
            "options" => array(
                "Female" => "Female",
                "Male" => "Male",
            ),
            "profile" => true, // show in user profile
            "class" => "pmpro-custom-field", // add a class
            "required" => true // make this field required
        ));

    $fields[] = new PMProRH_Field(
        "ethnicity",                   // input name, will also be used as meta key
        "select",                   // type of field
        array(
	    "label" => "Ethnicity",    //label
            "options" => array(       // <option> elements for select field
                    "" => "",       // blank option - cannot be selected if this field is required
                "american_indian"=>"American Indian", 
                "asian"=>"Asian",
                "american_indian"=>"American Indian", 
                "african_american"=>"African American",
                "native_hawaiian"=>"Native Hawaiian", 
                "white"=>"White",
                "hispanic"=>"Hispanic"
            ),
            "profile" => true, // show in user profile
            "required" => true // make this field required
        ));

    $fields[] = new PMProRH_Field(
	"yearly household income",                   // input name, will also be used as meta key
        "select",                   // type of field
        array(
	    "label" => "Yearly Household Income",    //label
            "options"=>array(       // <option> elements for select field
                    "" => "",       // blank option - cannot be selected if this field is required
                "less_than_20k"=>"Less than $20,000",   
                "20_49"=>"$20,000 - $49,999", 
                "50_99"=>"$50,000 - $99,999",
                "100_249"=>"$100,000 - $249,999", 
                "250_up"=>"$250,000 or More"
            ),
            "profile" => true, // show in user profile
            "required" => true // make this field required
        ));

    $fields[] = new PMProRH_Field(
        "party affiliation",                   // input name, will also be used as meta key
        "select",                   // type of field
        array(
            "options"=>array(       // <option> elements for select field
                    "" => "",       // blank option - cannot be selected if this field is required
                "democratic"=>"Democratic",
                "republican"=>"Republican", 
                "independent"=>"Independent",    
                "Green"=>"green", 
                "libertarian"=>"Libertarian",
		"other"=>"Other"
            ),
            "profile" => true, // show in user profile
            "required" => true // 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");

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