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.

I'm trying to add the URL of the post images in to a default custom field. Here is the code I got so far...

Here is how I add the default custom 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', '', true);
        }
        return true;
    }

Now I need a function that fills the value with the URL from the image uploaded. If there is no image uploaded the field can be empty.

Here is the code to fill the value with info, but I need the thumbnail url in the value

add_post_meta($post_id, 'custom field name', 'custom field value', true);
share|improve this question
    
Why are you trying to store attachment-image URLs in post custom meta data, when that information is already stored, and easily accessible via core functions? –  Chip Bennett Feb 5 '12 at 2:57
    
Because i'm exporting posts between blogs in a multisite setup. Long story short... I need to do it this way... So, can you help me? –  Demilio Feb 5 '12 at 8:43
    
And the built-in core export/import functionality doesn't work for you, or isn't suitable? –  Chip Bennett Feb 5 '12 at 18:04
add comment

1 Answer

Can you explain why you need a custom field for the image ? if there is no special reason for that , you should use the_post_thumbnail() function that is built in wordpress and will not have you mess with custom fields.

You can read more here :

Display thumbnail from custom field

EDIT - Well, if you insist on using it :

add_action('wp_insert_post', 'mk_set_default_custom_fields');
    function mk_set_default_custom_fields($post_id)
    {
        if ( $_GET['post_type'] != 'post' ) {
        $image1 = wp_get_attachment_image_src(get_post_thumbnail_id()) ;
            update_post_meta($post_id, 'image', $image1[0],true);
        }
        return true;
    }

Of course the feature image needs o be defined (or set a default) , and you can choose the sizes in the get_post_thumbnail() function.

share|improve this answer
    
I need it because I'm using it on a multisite setup i have created. I know i have to do it this way so how do I include the thumbnail url in the code above? I'm not a pro php programmer so please help me! I've been searchin long for this. I cant use the built in function because i need to import a custom field from one blog to another etc etc... Log story short: i need to have the image url in a vustom field automaticly –  Demilio Feb 5 '12 at 8:38
add comment

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.