Skip Navigation

[Résolu] How to create checkbox field data through code?

This support ticket is created Il y a 4 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Marqué : 

This topic contains 5 réponses, has 2 voix.

Last updated by Minesh Il y a 4 années et 10 mois.

Assisted by: Minesh.

Auteur
Publications
#1250731

Hi,

I have created a custom page and few custom fields. I am able to create a page through code and populate field data using update_post_meta function.

I have a checkbox with multi select field and an image field also.

I would like to know the way to populate checkbox and image field data.

Regards,
Lokesh

#1250747

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I have a checkbox with multi select field and an image field also.
==> Do you mean that you have checkboxes field? and one image field? is those fields created using Types? If yes, Can you please share screenshot of your custom fields group and show me how those fields are configured?

#1250749
toolsetimage.PNG
toolsetcheckbox.PNG

Yes, both fields are added in custom post type using toolset plugin. Attached images are of a post created manually. However I want to create post via a code. Can you help me?

#1250905

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - both images and checkboxes fields are stored in postmeta table.

Images:
- Images fields are stored with full URL of the image.

Checkboxes:
- Checkboxes fields are stored in a special format which is called serialized array. To save checkboxes values you should try to use the following code:

$types_field = "replace-checkboxes-field-slug";

	$fields = get_option( 'wpcf-fields', array() );;
			
	$arr = array();
    if(isset($fields[$types_field]['data']['options'])){
        foreach ($fields[$types_field]['data']['options'] as $k=> $v){
             $arr[$k] = array(0=>$v['set_value']);
        }
	}
	
update_post_meta($post_id,"wpcf-".$types_field,$arr);
#1250909

Thanks for revert. Can you help a bit more. I have following 3 values to be stored e.g. value1, value 2, value 3. How would this fit in your sample code?

$types_field = "replace-checkboxes-field-slug";

$fields = get_option( 'wpcf-fields', array() );;

$arr = array();
if(isset($fields[$types_field]['data']['options'])){
foreach ($fields[$types_field]['data']['options'] as $k=> $v){
$arr[$k] = array(0=>$v['set_value']);
}
}

update_post_meta($post_id,"wpcf-".$types_field,$arr);

#1250915

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you should try to run the code, once you run it by replacing your field name, it will checkmark all the checkboxes you have automatically with the values you assigned already to your Types checkboxes field.

If you want to checkmark specific checkboxes, you can check the specific option within the following loop and make sure to unset that key.

foreach ($fields[$types_field]['data']['options'] as $k=> $v){
$arr[$k] = array(0=>$v['set_value']);
} 
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.