Thank you for waiting, as this troubleshooting took longer than I initially expected.
On my test website, I was able to make this work, using the following steps:
1. For my temp post type, I add a checkboxes type field with slug "post-checkboxes" and for the users, added checkboxes type custom field with slug "user-checkboxes".
It is important to set the post custom field in a way that option titles are shown when the field is selected, using the "Show one of these two values" setting.
( screenshot: hidden link )
And these titles should also match the option titles used in the user checkboxes type custom field, in which you'd like to copy these values.
2. The "ts_checkboxes" function from the other thread only works for the post custom fields and not for the user custom fields.
For this specific requirement, I added another custom function:
function custom_checkboxes_copy_post_to_user( $user_id, $field, $options ){
if ( isset($user_id) && isset($field) && isset($options) ){
$field_settings = types_get_field( $field, 'usermeta' );
$field_options = $field_settings['data']['options'];
$meta = array();
for ($i=0; $i < sizeof($options); $i++) {
$key = array_search( $options[$i], array_column( $field_options, 'title' ) );
$keys = array_keys( $field_options );
$option_key = $keys[$key];
$meta[$option_key] = array($field_options[$option_key]['set_value']);
}
if(!empty($meta)) {
update_user_meta( $user_id, 'wpcf-'.$field, $meta );
}
}
}
This function accepts 3 parameters:
$user_id: ID of the user to which field value should be copied.
$field: Slug of the user checkboxes type custom field (without 'wpcf-' prefix).
$options: An array of option titles, which should be checked/selected.
3. In the last step, I used this new function in the function attached to the "cred_save_data" hook for user form, like this:
add_action('cred_save_data', 'populate_temp_data_to_new_subscriber',10,2);
function populate_temp_data_to_new_subscriber($post_id, $form_data)
{
if ($form_data['id']==5) // user registration form
{
$temp_post_id = $_GET['post-id'];
$post_field_options = types_render_field( "post-checkboxes", array( "separator" => ",", "item" => $temp_post_id ) );
$options = explode(',', $post_field_options);
custom_checkboxes_copy_post_to_user( $post_id, 'user-checkboxes', $options );
}
}
This function gets the target temp post's ID from the ULR parameter "post-id", gets the titles of all the checked checkboxes options from this post, and then passes them on to the "custom_checkboxes_copy_post_to_user" function.
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/