I need help to copy the value of an image custom field to another image custom field on the same post type. I am using a third party form "Super Forms" to create post from frontend and there are two image fields that I have created in the custom fields group:
1: wpcf-img
2: wpcf-img-up
Now from the third party form, If field 2 is filled and saved to post metadata then a function should automatically copy the image field value from field 2 to field 1. I tried a few codes. But the function doesn't work.
So basically I copied code from toolset forum which is basically for Cred_forms but mine is a third party form. So It's not working. The code is below.
add_action('cred_save_data', 'copy_location_to_multiline_field',10,1);
function copy_location_to_multiline_field($post_id, $form_data)
{
// if a specific form
if (isset($_POST['wpcf-img-up']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-img', $_POST['wpcf-img-up']);
}
}
I would request you to please help me make it work. In my third party form, The form data can be populated and updated with a hook "super_front_end_posting_after_insert_post_action" which is already being used for another function to convert image id to image full path. So I think this hook can be used instead of "cred_save_data"
Please let me know if you can help or if you need access to my staging site then I can share.
Hello. Thank you for contacting the Toolset support.
If you were using the Toolset Form, the following code should work to copy the one image field value to another image field.
add_action('cred_save_data', 'copy_location_to_multiline_field',10,1);
function copy_location_to_multiline_field($post_id, $form_data){
$img_2_value = get_post_meta($post_id,'wpcf-img-up',true);
// if a specific form
if ( $img_2_value != ''){
// add it to saved post meta
update_post_meta($post_id, 'wpcf-img', $img_2_value);
}
}
As Super forms is not our product/plugin, you need to adopt the above code in the way that it should work with the super forms.
I just tried to google it and I can see super forms are offering a hook: super_after_saving_contact_entry_action
=> hidden link
For example:
function func_copy_image_field_value( $attr ) {
// DO NOT CHANGE BELOW CODE
$post_id = $attr['post_id'];
$img_2_value = get_post_meta($post_id,'wpcf-img-up',true);
// if a specific form
if ( $img_2_value != ''){
// update another field based on the 2nd image field value
update_post_meta($post_id, 'wpcf-img', $img_2_value);
}
}
add_action('super_front_end_posting_after_insert_post_action', 'func_copy_image_field_value', 10, 1);
Can you please try to use the above code and check if that help you to resolve your issue.