Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ipokkel/73d09aa4857b3015f773c189ed08ec33 to your computer and use it in GitHub Desktop.
Save ipokkel/73d09aa4857b3015f773c189ed08ec33 to your computer and use it in GitHub Desktop.
<?php
/**
* Example code recipe to add a checkbox grouped user field.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_example_checkbox_grouped_user_field() {
// Don't break if PMPro is out of date or not loaded.
if ( ! function_exists( 'pmpro_add_user_field' ) ) {
return false;
}
// Store our field settings in an array.
$fields = array();
$fields[] = new PMPro_Field(
'example_checkbox_grouped', // input name and user meta key
'checkbox_grouped', // type of field
array(
'label' => 'Example Checkbox Grouped', // custom field label
'profile' => true, // display on user profile
'memberslistcsv' => true, // include in CSV export
'required' => true, // make this field required
'options' => array( // <option> elements for select field
'option1' => 'Option 1',
'option2' => 'Option 2',
'option3' => 'Option 3',
),
)
);
// Add a field group to put our fields into.
pmpro_add_field_group( 'Example User Fields' );
// Add all of our fields into that group.
foreach ( $fields as $field ) {
pmpro_add_user_field(
'Example User Fields', // Which group to add to.
$field // PMPro_Field object
);
}
// That's it. See the PMPro User Fields docs for more information.
}
add_action( 'init', 'my_pmpro_example_checkbox_grouped_user_field' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment