Home Forums Plugin Support WP User Frontend Pro How do I insert the URL of an uploaded image into a custom field?

This topic is: resolved

This topic contains 5 replies, has 3 voices, and was last updated by  dektech 5 years, 10 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #6005

    dektech
    Participant
    Post count: 5

    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!

    #6029

    MDelcour
    Participant
    Post count: 46

    You can also simply get the url of attached images.

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

    #6033

    Tareq Hasan
    Keymaster
    Post count: 2831

    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]

    #6550

    dektech
    Participant
    Post count: 5

    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.

    #6576

    Tareq Hasan
    Keymaster
    Post count: 2831

    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]

    #6581

    dektech
    Participant
    Post count: 5

    Worked Perfectly! Thank you!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘How do I insert the URL of an uploaded image into a custom field?’ is closed to new replies.