Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Does anyone know how I automatically add the URL of the uploaded image to a custom field?

In other words, When I upload a image I want the url to the image to get a custom field variable automatically so I dont have to add the URL manually to the custom field all the time.

I need a seamless integration to make is user-friendly. I want to use scripts like timthumb instead of the built in WP functionality.

-----UPDATE:

Here is some code I have. Just missing the thumbnail variable in the custom value field.

add_action('wp_insert_post', 'mk_set_default_custom_fields');

function mk_set_default_custom_fields($post_id)
{
    if ( $_GET['post_type'] != 'post' ) {

        add_post_meta($post_id, 'Image', 'I WANT THE THUMBNAIL HERE', true);

    }

    return true;
}
share|improve this question
1  
have you read: codex.wordpress.org/Function_Reference/… ? where is the image url coming from? –  Michael Feb 4 '12 at 0:41
    
The image-url is the first image in the post. i.e. the thumbnail –  Demilio Feb 4 '12 at 2:27
    
Why would you not want to use WP thumbnail functionality? You're placing more load on your server per page view using TimThumb. –  Brian Fegter Feb 4 '12 at 3:06
    
Because I'm using Wordpress Multiuser with sitewide tagss. I want to show user images in the loop by using custom fields, and I got that ufnctioon working. Now I'm trying to make it more userfriendly by automating the timthumb-image-link. So I really need this one to customie my front page –  Demilio Feb 4 '12 at 9:44

2 Answers 2

up vote 0 down vote accepted

Here is the final code that adds the thumbnail url to a custom field named Image.

function w_thumbnail_src() {
    if (has_post_thumbnail()) {
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
        return $thumb[0]; // thumbnail url
    } else {
        return '';  // or a default thumbnail url
    }
}


add_action('publish_page', 'add_custom_field_automatically', 'w_thumbnail_src');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_id) {
global $wpdb;
if(!wp_is_post_revision($post_id)) {
add_post_meta($post_id, 'Image', w_thumbnail_src(), true);
}
}
share|improve this answer

There's no need to store the URL in a custom field, the thumbnail id is already stored in a custom field under the key _thumbnail_id. You can fetch the ID via get_post_meta and use wp_get_attachment_image_src to get the URL.

share|improve this answer
    
I'm using Wordpress Multiuser with sitewide tags. I want to show user images in the loop by using custom fields, and I got that function working. Now I'm trying to make it more userfriendly by automating the timthumb-image-link. So I really need this one to customie my front page. I have tried what you describe but cant get it working with my setup. So the easyes and most userfriendly is if I could add the first uploaded image on a post automaticly to a custom field by pressing a button or something in the ipload-menu –  Demilio Feb 4 '12 at 9:47
    
Show us the code you tried and we'll help you get it working, as I'm sure you'll agree requiring your users do nothing is the most user-friendly. –  Milo Feb 4 '12 at 15:14
    
ok, I hva updated the post with the code I got. Can you take a look at it? :) –  Demilio Feb 4 '12 at 16:47
    
I meant the code you tried regarding my original answer for just fetching the thumbnail from existing meta, not adding the URL to a custom field. again, that will be far easier than what you're trying to do here, as you'll have to check that the thumbnail hasn't changed every time the post is updated. –  Milo Feb 4 '12 at 16:51
    
I'm no pro in PHP, so I cant see where you are going. I need the URL in the custom field because I use multisite with sitewide tags. So I do a import of all blogs with custom fields to show all posts on my main blog.. –  Demilio Feb 4 '12 at 17:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.