Topic: Image via Custom Field

I am really impressed with Arras Theme. I am running a blog on a free hosting service. So i use other image hosting service to host all my images.

Arras theme doesn't use custom field for its featured post image or thumbnails images. Please tell me how can i use custom field in Arras theme to link to external images for thumbs and other images.

Thanks

Thumbs up

Re: Image via Custom Field

hi promy...

create a custom field called (image) ... note that is case sensitive ok...
then paste the code below into the functions.php file. --- you will have to make your uploads folder writable for good, means change its permisions to 777..

function postmeta_img_update() {
global $post;
// If Custom Post Meta Field for a Image/Thumnail Exists
if( get_post_meta($post->ID, "image", true) ):

// Get Image Location and File Extention
$img = get_post_meta($post->ID, "image", true);
$ext = substr(strrchr($img,'.'),1);

// WordPress Upload Directory to Copy to (must be CHMOD to 777)
$uploads = wp_upload_dir();
$copydir = $uploads['path']."/";

// Code to Copy Image to WordPress Upload Directory (Server Must Support file_get_content/fopen/fputs)
$data = file_get_contents($img);
$filetitle = strtolower(str_replace(array(' ', '-', '.', '(', ')', '!', '@', '#', '$', '%', '^', '&', '*', '_', '=', '+'), "-", get_the_title()));
$file = fopen($copydir . "$filetitle-$post->ID.$ext", "w+");
fputs($file, $data);
fclose($file);

// Insert Image to WordPress Media Libarby
$filepath = $uploads['path']."/$filetitle-$post->ID.$ext";

$wp_filetype = wp_check_filetype(basename($filepath), null );
$attachment = array(
     'post_mime_type' => $wp_filetype['type'],
     'post_title' => get_the_title(),
     'post_content' => 'Image for '.get_the_title(),
     'post_status' => 'inherit'
    );

wp_insert_attachment( $attachment, $filepath, $post->ID);

// Get Attachment ID for Post
global $wpdb; $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
// Attached Image as Featured Thumbnail in Post
update_post_meta($post->ID, "_thumbnail_id", $attachment_id);

// Removes Custom Meta Field Image URL. This stop this function running again for the updated post.
delete_post_meta($post->ID, "image");

endif;

}
add_action('the_post','postmeta_img_update');

please report back...

just try....

Thumbs up

Re: Image via Custom Field

Thanks for your reply. It works. But still there is a problem. I want the image to be loaded from free image hosting like tinypic.com. When i create a custom field named image and give the tinypic image url, wordpress download the image from tinypic and put it into uploads folder and load from it.

I want that my upload folder to be empty and load the images from external server like tinypic or imageshack.
Please tell me how to do that.

Last edited by promy (2010-07-30 04:48:11)

Thumbs up

Re: Image via Custom Field

wordpress doesn't set featured image from external link url...(that's a wordpress thing ... when you set a featured image, wordpress create various sizes of that image you add and uploaded," to be used within diferent areas into the theme" , but if the image comes from external link  wordpress can't do this process "because the image isn't available in your server to be processed" .. so it's disabled for that reason for external link.....

just try....

Thumbs up

Re: Image via Custom Field

son_azules, is there some similar code that will help me in my situation?

I recently upgraded arras from 3.9 to the most recent version. So for all my old posts, the main image is entered as a custom field "thumb". But now none of them are displaying as featured images.

I was hoping there would be some automated way of turning all the images linked to as "thumb" into featured images.

All of my thumbs are already uploaded to the wordpress uploads.

Thumbs up

Re: Image via Custom Field

maybe you will have to update each post once!!!! long process... or you can use arras theme 1.5 it sets a featured image automatically...

just try....

Thumbs up

Re: Image via Custom Field

Hi Son_azules

i tried to implemnt your solution on my Blog and it doesnt work
i can see it changed from teh default to a missing image

im using the latest Arras theme

can you please have a look?

my testing blug is http://yaniv83.byethost18.com/wordpress/

all the best
Erik

Thumbs up

Re: Image via Custom Field

Hi Son_Azules,

I have been searching to solve this exact problem in the Arras theme for some time now and have come across many other people doing the same. So far your solution has come the closest.

I have put your code in the functions.php file but unfortunately all I get in the media library and consequently the featured images are image titles? Information on exactly where to drop the code int the functions.php would be really useful because I have tried different methods with different results.

The folder I'm copying the files to on my server from the remote URL identified in the custom field has 777 permissions. I have checked the links in the custom field and there are images there that can be copied, no problem. I'm just waiting on an answer from support re file_get_content, fopen, fput commands being allowed on the server. If they are, I can see no other reason why the images are not being retrieved.

Any advise is greatly appreciated, thanks

Thumbs up