[-] Add Options to Checkboxes (unofficial filter)

This is an unofficial filter, not documented, but works, whereas the documented does not.
Use at your own risk, this is not public…

//filter wpcf_fields_{field-type}_meta_data
//$field_options (options array of field)
//$field (whole field array)
add_filter( 'wpcf_fields_checkboxes_meta_data', 'add_options_to_checkboxes', 10, 2 );

function add_options_to_checkboxes($field_options,$field){
	
	//$options array() 
	//key = option unique ID and 
	//value = arrray() of options settings
	//Of course in real life you generate this, maybe using user data, or post data, whatever you want to add as options to your field
	$options = array(

		//array key used as unique ID of each option
		'option_one' => array( 
			'title' => 'Checkbox 2',//The option title
			'set_value' => '1',//value to save if set
			'display'	=> 'db',//value to display if set
			'display_value_not_selected' =>  '',//value to display if not selected custom
			'display_value_selected' => ''//value to display if selected custom
		),

		'option_two' => array( 
			'title' => 'Checkbox 3',
			'set_value' => '1',
			'display'	=> 'db',
			'display_value_not_selected' =>  '',
			'display_value_selected' => ''
		)

	);

	if ($field['name'] == 'My Checkboxes Field') {//Change to your custom field Name (not slug)
		foreach ($options as $key => $value) {
			$option_slug = 'wpcf-fields-checkboxes-option-' . md5($key);
			if (!in_array($option_slug, $field_options['options'])) {
				$field_options['options'][$option_slug] = $value;	
			}	
		}
	}
	return $field_options;
}

Select all checkboxes or radio buttons

From time to time I have had clients ask if there is a way to select all checkboxes in the translation editor or all radio buttons in the WPML -> Settings -> Custom Fields section. Although there is not a way to do this in the WPML interface, here is a little workaround.

When using the translation editor, if you would like to select all checkboxes so that you can mark all as complete, open up your browser console and add this in the Console:

jQuery(document).ready(function(){ jQuery(“.icl_tm_finished”).attr(“checked”, “checked”); });

Then, hit enter and check one box on the page. This should select all boxes.

Similarly, to set all custom fields to a specific value on WPML -> Settings -> Custom Fields, add this in the browser console:

jQuery(document).ready(function(){ jQuery(‘[value=”3″]’).attr(‘checked’,true); });

Here, the value 3 sets all fields to Copy Once. You can adjust this value as needed for Copy, Translate, Don’t Translate, etc.

Just a little tidbit that I hope you find helpful 🙂

Require at least one checkbox from a checkboxes group custom field in a CRED form

In a CRED form, you may want to require the User to select at least one checkbox in a checkboxes group custom field. It’s not possible to set up this requirement in wp-admin, but it’s possible to enforce it with the cred_form_validate API hook. Add the following code to your child theme’s functions.php file:

add_filter('cred_form_validate','at_least_one_checkbox_validation', 10, 2);
function at_least_one_checkbox_validation( $field_data, $form_data ) {
    // field data are field values and errors
    list($fields,$errors)=$field_data;
    // forms is a comma-separated list of form IDs where this validation should be applied
    $forms = array( 12345,67890 );
    // validate if the correct CRED form ID
    if ( in_array( $form_data['id'], $forms ) ) {
      if ( !isset($fields['wpcf-cbs']['value'][0]))
        $errors['cbs'] = 'At least one checkbox is required';
    }

    return array( $fields, $errors );
}

Replace 12345,67890 with a comma-separated list of CRED form IDs where you want to apply this validation. Replace ‘cbs’ in both places to match your checkboxes field slug. For example if your checkboxes field slug is “features” then your code should be as follows:

      if ( !isset($fields['wpcf-features']['value'][0]))
        $errors['features'] = 'At least one checkbox is required';

See the documentation for the cred_form_validate API hook for more information.