Skip to content

Instantly share code, notes, and snippets.

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 plugin-republic/faa6467ca9748cf67e7af5a5a911ba54 to your computer and use it in GitHub Desktop.
Save plugin-republic/faa6467ca9748cf67e7af5a5a911ba54 to your computer and use it in GitHub Desktop.
Enable a default value for checkbox groups
<?php
/**
* Enable a default value for checkbox groups.
* Your default value must be an array.
* You'll need to change the value of the $field_id to match your own Field ID
* @param $value The current value of the field
* @param $id The field ID in the format pewc_group_888_999
* @param $field The field object
* @param $posted The $_POST object
*/
function prefix_default_field_value( $value, $id, $field, $posted ) {
// Change this to the ID of the field you are setting a default for
$field_id = 999;
// Don't overwrite a value that's already there
if( $value ) {
return $value;
}
// Now look for our Field ID
if( $field_id == $field['field_id'] ) {
// You need to enter the default values here, copied exactly from your checkbox group options
$value = array(
'My first default option',
'My second default option'
);
}
return $value;
}
add_filter( 'pewc_default_field_value', 'prefix_default_field_value', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment