Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ipokkel/87b172cb67879bb0074172289a56aa04 to your computer and use it in GitHub Desktop.
Save ipokkel/87b172cb67879bb0074172289a56aa04 to your computer and use it in GitHub Desktop.
<?php
/**
* Set default values for specific checkbox group fields.
* This function hooks into the fields before they're added to a form
*
* Set your checkbox group field names and default values in the $defaults array.
*
* 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_set_checkbox_group_defaults( $field ) {
// Only modify checkbox_grouped fields.
if ( is_object( $field ) && isset( $field->type ) && 'checkbox_grouped' === $field->type ) {
// List of field names and their default values.
$defaults = array(
// Modify this array to match your specific checkbox group fields.
// Format: 'field_name' => array('option1_value', 'option2_value').
'checkbox_group_example' => array( 'cats', 'dogs' ),
);
// Check if this field is in our list of fields to modify.
if ( isset( $field->name ) && array_key_exists( $field->name, $defaults ) ) {
// Set the default values for this field.
$field->default = $defaults[ $field->name ];
}
}
return $field;
}
add_filter( 'pmpro_add_user_field', 'my_pmpro_set_checkbox_group_defaults', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment