Skip Navigation

[Resolved] Update checkboxes field via php

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.

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)

This topic contains 10 replies, has 2 voices.

Last updated by Minesh 1 year, 5 months ago.

Assisted by: Minesh.

Author
Posts
#2682755

Tell us what you are trying to do?
Update the checkboxes field to specific values via php.
Is there any documentation that you are following?
None that is adequate, that explain the exact datatypes expeced by the update_post_metadata() funciton
Is there a similar example that we can see?
This is essentially the same question as posted below.
https://toolset.com/forums/topic/update-checkboxes-checked-state-via-php/

My code is like this:
` $first_entry = array(
"wpcf-fields-checkboxes-option-6e20d12e07fc5ce114f9456756fc5b5c-1" => array("Wiederholungen"),
"wpcf-fields-checkboxes-option-25d9ca35050dd66242dcbd169bf65168-1" => 0,
"wpcf-fields-checkboxes-option-3adf42187da5b3898cb12e302d297cba-1" => 0,
"wpcf-fields-checkboxes-option-53b36d5ad031b3f2cb343d899c3092aa-1" => 0
);
update_post_meta( $post_id, 'wpcf-the-showing-type' , $first_entry );`

Which then ends up looking like this in the database
`a:4:{s:64:"wpcf-fields-checkboxes-option-6e20d12e07fc5ce114f9456756fc5b5c-1";i:0;s:64:"wpcf-fields-checkboxes-option-25d9ca35050dd66242dcbd169bf65168-1";i:0;s:64:"wpcf-fields-checkboxes-option-3adf42187da5b3898cb12e302d297cba-1";i:0;s:64:"wpcf-fields-checkboxes-option-53b36d5ad031b3f2cb343d899c3092aa-1";i:0;}`

where it should look like this

`a:4:{s:64:"wpcf-fields-checkboxes-option-6e20d12e07fc5ce114f9456756fc5b5c-1";a:1:{i:0;s:14:"Wiederholungen";}s:64:"wpcf-fields-checkboxes-option-25d9ca35050dd66242dcbd169bf65168-1";i:0;s:64:"wpcf-fields-checkboxes-option-3adf42187da5b3898cb12e302d297cba-1";i:0;s:64:"wpcf-fields-checkboxes-option-53b36d5ad031b3f2cb343d899c3092aa-1";i:0;`

When I run the code everything runs smoothly but the field itself does not get updated.
I have also played around with the code here https://toolset.com/forums/topic/update-checkboxes-field-with-php/ but in the end everything is behaving the same..

#2682816

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#benefits-of-adding-custom-code-using-toolset

/**
 * Unofficial API function to check/uncheck checkboxes options
 * 
 * @param int $post_id
 * @param string $field // slug of checkboxes field
 * @param string $option // title of checkbox option to manipulate
 * @param string $action : 'check' | 'uncheck' | 'value' (default, returns current value)
 * 
 * Important: assumes recommended checkboxes setting of save nothing to database when unchecked
 */
function ts_checkboxes( $post_id, $field, $option, $action = 'value' ){
 
    if ( isset($post_id) && isset($field) && isset($option) ){
 
        $field_settings = types_get_field( $field );
         
        $field_options = $field_settings['data']['options'];
 
        // Locate the option key
        $key = array_search( $option, array_column( $field_options, 'title' ) );
        $keys = array_keys( $field_options );
        $option_key = $keys[$key];
 
        // Get the current post meta value
        $meta = get_post_meta( $post_id, 'wpcf-'.$field, true );
        if ( !is_array($meta) ){
            $meta = [];
        }
 
        // If action = 'value' just return the value
        if ( $action == 'value' && isset( $meta[$option_key] ) ){
            return $meta[$option_key][0];
        } else {
            // Remove the existing key if it exists (i.e. uncheck)
            // because recommended setting is to save nothing when unchecked
            unset( $meta[$option_key] );
             
            // If $checked == true then add back the key + value
            if ( $action == 'check' ){
                $meta[$option_key] = array($field_options[$option_key]['set_value']);
            }
            update_post_meta( $post_id, 'wpcf-'.$field, $meta );        
        }
    }
}

You can call the it as:

$post_id = 99999;
$field = 'event-format';
$option_title = 'Wiederholungen';
$action = 'check';
ts_checkboxes( $post_id, $field, $option_title, $action );

Where:
- Replace 99999 with your original post ID you want to update the checkboxes field
- Replace 'event-format' with your original checkboxes custom field slug

Can you please confirm it works as expected.

#2682834
IMG_1116.png

I used the code above, no errors but the field does not save.

This is what gets populated in the db
a:4:{s:64:"wpcf-fields-checkboxes-option-6e20d12e07fc5ce114f9456756fc5b5c-1";i:0;s:64:"wpcf-fields-checkboxes-option-25d9ca35050dd66242dcbd169bf65168-1";i:0;s:64:"wpcf-fields-checkboxes-option-3adf42187da5b3898cb12e302d297cba-1";i:0;s:64:"wpcf-fields-checkboxes-option-53b36d5ad031b3f2cb343d899c3092aa-1";i:0;}

The DB gets populated but wrong values are inserted into the db
Array
(
[wpcf-fields-checkboxes-option-6e20d12e07fc5ce114f9456756fc5b5c-1] => 0
[wpcf-fields-checkboxes-option-25d9ca35050dd66242dcbd169bf65168-1] => 0
[wpcf-fields-checkboxes-option-3adf42187da5b3898cb12e302d297cba-1] => 0
[wpcf-fields-checkboxes-option-53b36d5ad031b3f2cb343d899c3092aa-1] => 0
)

#2682852

Minesh
Supporter

Languages: English (English )

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

Can you please share admin access details and where you are trying to use the code I shared.

What option exactly you want to check using PHP?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2683145

Minesh
Supporter

Languages: English (English )

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

Can you please tell me from where using what PHP file you are trying to checkmark the checkbox?

Where you added the code and where can I test it?

ts_checkboxes( $post_id, $field, $option_title, $action );
#2683233

Its in functions.php line 687

#2683235

Minesh
Supporter

Languages: English (English )

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

Thank you for sharing that information. But when I try to login to FTP using the access details you shared, I can only see index.php file. Can you please grant access so that I can access the functions.php file of your theme.

#2683236

I know this is not ideal but could you have a look at it via the "Theme file editor"? its in "Hello Elementor Child" theme

#2683238

Minesh
Supporter

Languages: English (English )

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

Sorry - that will not work. Can you please add the code I shared to "Custom Code" section as in that section I can add/modify the code but if I try to edit in functions.php file if something goes wrong it will result in that the site will be broken and I will not be able to make any changes.

Can you please try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#benefits-of-adding-custom-code-using-toolset

#2683240

should now be there under checkbox-save

#2683286

Minesh
Supporter

Languages: English (English )

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

I've updated the code in "Custom Code" section with your code snippet.

Can you please confirm it works as expected now.