Store uploaded file URL in custom field

I had to tweak the code here a little to get the desired result. For some reason, wp_get_attachment_url() method wasn’t returning the correct values. My version of the code is given below for reference:

add_filter( 'frm_new_post', 'create_a_custom_field', 10, 2 );
function create_a_custom_field( $post, $args ) {
$field_id = 89; //replace 89 with the correct field id
if ( isset( $_POST['item_meta'][ $field_id ] ) && !empty( $_POST['item_meta'][ $field_id ] ) ) {
$field_value = sanitize_text_field( $_POST['item_meta'][ $field_id ] );
$attachmentids = '';
foreach ($_POST['item_meta'][ $field_id ] as $value) {
$attachmentids=$attachmentids.','.$value;
}
$post['post_custom']['_product_image_gallery'] = $attachmentids;
}
return $post;
}

Hope this helps! Please let me know if there is a better way to achieve the same results.

Leave a comment