How do I insert the URL of an uploaded image into a custom field?

This topic contains 5 reply and 3 voices, and was last updated by dektech 10 years, 8 months ago
Viewing 5 Posts - 1 through 5 (of 5 total)
Author Posts
July 16, 2013 at 4:15 am 6005
dektech Hello, I am trying to have the user upload an image, then store the URL to the image in a custom field automatically. Is there a piece of code (hook) that I can use to do this? Thanks in advance!
July 16, 2013 at 6:44 pm 6029
MDelcour MDelcour

You can also simply get the url of attached images.

http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

July 16, 2013 at 7:31 pm 6033
Tareq Hasan Tareq Hasan

As @MDelcour already provided the function reference, you can use like this

[php]
$image_id = get_post_meta( $post->ID, ‘meta_key_name’, true );
$url = wp_get_attachment_url( $image_id ); // returns full URL
$image_html = wp_get_attachment_image( $image_id ); // returns image with HTML tag
[/php]

August 1, 2013 at 9:03 am 6550
dektech dektech

Sorry, I guess I should have elaborated.

I am using the WP-front-end-pro form to upload an image that is attached to a custom field called Thumbnail.. works great, except it inserts the image ID number. What I wold like it to do is insert the url for the image instead. Where can I change this in the plug-in code? Simply replace the image ID with the image URL when someone uploads the image.

August 2, 2013 at 1:13 am 6576
Tareq Hasan Tareq Hasan

That ID is needed when editing the attachment, otherwise it’ll break. One thing you could do, save the attachment URL in another custom field. Put the custom field name in the $field_name variable and another_field_to_save_the_url will have the attachment URL if any.

[php]
function wpufe_copy_att_url( $post_id ) {
$field_name = ‘my_custom_field_name_in_the_form’;
$attach_id = get_post_meta( $post_id, $field_name, true );

if ( $attach_id ) {
$meta_value = wp_get_attachment_url( $attach_id );
update_post_meta( $post_id, ‘another_field_to_save_the_url’, $meta_value );
}
}

add_action( ‘wpuf_add_post_after_insert’, ‘wpufe_copy_att_url’ );
add_action( ‘wpuf_edit_post_after_update’, ‘wpufe_copy_att_url’ );
[/php]

August 2, 2013 at 3:41 am 6581
dektech dektech

Worked Perfectly! Thank you!

Viewing 5 Posts - 1 through 5 (of 5 total)