Datafeedr Support Forums

Datafeedr Support Forums (http://www.datafeedr.com/forums/index.php)
-   Questions (http://www.datafeedr.com/forums/forumdisplay.php?f=67)
-   -   Drip post and Feature Images (http://www.datafeedr.com/forums/showthread.php?t=3800)

k9marketing January 27th, 2011 11:19 AM

Drip post and Feature Images
 
I am using a theme which utilizes the featured image functionality in WP. Currently, I have to go in and edit each post to upload and add the featured image to the post. Anyone have an easier way to accomplish this?

Eric January 27th, 2011 12:14 PM

You should be able to edit your template and access a different custom field. You will need to access the field store_image in order to display a product's image.

Here's more about editing your template and displaying custom field values:

http://codex.wordpress.org/Custom_Fields

Eric

ayorofrance January 27th, 2011 07:52 PM

If it is to show thumbnails on excerpt pages have a look at a plugin called "Thumbnail For Excerpts"... it looks in your post and adds an image (if found) to the excerpt page automatically...

Reson8 May 3rd, 2011 11:24 PM

Hi Eric,

Could you give a little bit more info on how to do this. The link to wordpress.org you supplied gives quite a complex solution. I need a featured image for each of my drip posts as well. I presume this is a common request.

Thanks,

Eric May 4th, 2011 08:26 AM

Quote:

Originally Posted by Reson8 (Post 20376)
Could you give a little bit more info on how to do this. The link to wordpress.org you supplied gives quite a complex solution. I need a featured image for each of my drip posts as well. I presume this is a common request.

I'm not quite sure what you need.... The "Featured Image" feature of WP Posts is not related to the custom field value. It's another value and I have no idea how to make the "Featured Image" field pull from a Custom Field value.

Reson8 May 4th, 2011 10:16 PM

OK, but do you think (in theory) it's possible? If so I will use the link you provided and try to find a solution.

Eric May 5th, 2011 08:36 AM

Hi

In theory, anything is possible. I'm not trying to be flip about that. But, you might have to write a plugin which accomplishes what you are trying to do or you might have to hack core WP code.

I have never really hacked around with the Featured Image feature so I'm not sure where to start but I guess I would start with googling about it.

Eric

Reson8 May 9th, 2011 08:48 AM

Hi Eric,

Here is what I used to fix this up in my site if it's useful for other users. Paste the code below into the functions.php file. The uploads folder needs to be writable (permissions 777) and the hosting server Must Support file_get_content/fopen/fputs:

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

// Get Image Location and File Extention
$img = get_post_meta($post->ID, "store_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 Libraby
$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');

Cheers

Eric May 9th, 2011 09:26 AM

Thanks for sharing, Reson8!

bigcroc September 3rd, 2011 07:22 PM

Reason8, thanks but isn't there something more simple than this, I'm trying it now but if you look at for example two themes, twentyten 1.1 and pagelines platform, set reading to latest posts, images show in twenty ten but not platform, why I don't know and I think this is thumbnail related.

Furthermore, when using your php in functions, the images are far too big and break the post but getting host to change access now, is there a way of getting DFR images whether external or uploaded to site to work with the vast array of plugins automatically? For instance sliders etc. I just want the images from dripped posts to show up as visual is obviously more appealing.

And Eric, Hello, I do enjoy this product but is there not an easier way to get images to show? I am using the 'special recent posts' plugin and thanks to reason8, images will work in that but is this possibly something you can consider making automatic in DFR please? Ability to make images easily used within a wp theme. If reason8 enables 'featured posts slideshow' to work also then even better as I've spent hours on this tonight.

Thanks
Lee

Eric September 3rd, 2011 07:26 PM

Hi Lee

You can post feature requests here: http://www.datafeedr.com/forums/forumdisplay.php?f=69

Eric

bigcroc September 3rd, 2011 08:13 PM

Reson8, when you say host must accept fopen, do you mean

allow_url_fopen On On

in phpinfo.php as I'm getting some errors and site seems slower (platform theme) functions.php dump

jerseyal December 10th, 2012 08:58 PM

Reson8,

Your code does just what you said it does, but unfortunately, it slows down my site to where it take 30 seconds for pages to load. Have you noticed any performance issues after inserting this code? Also, the featured images are very large, and if I click on it in the WP post page, and look in media gallery, there seems to be over 20 of the same image. Don't know if any of this is related...

What do you think?

Al


Quote:

Originally Posted by Reson8 (Post 20521)
Hi Eric,

Here is what I used to fix this up in my site if it's useful for other users. Paste the code below into the functions.php file. The uploads folder needs to be writable (permissions 777) and the hosting server Must Support file_get_content/fopen/fputs:

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

// Get Image Location and File Extention
$img = get_post_meta($post->ID, "store_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 Libraby
$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');

Cheers


Eric December 10th, 2012 09:39 PM

Hi,

Here's the updated code which goes in your theme's functions.php file. I've only tested that it works... it has not been tested for security or anything else.

Use at your own risk!

PHP Code:

function postmeta_img_update() {
    
    global 
$post;

    
// If Custom Post Meta Field for a Image/Thumnail Exists
    
if( get_post_meta($post->ID"store_image"true) ) {
    
        
$dfr_featured_image_is_set get_post_meta($post->ID"dfr_featured_image_is_set"true);
        
        if (
$dfr_featured_image_is_set == 'yes') {
            return;
        }

        
// Get Image Location and File Extention
        
$img get_post_meta($post->ID"store_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 Libraby
        
$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.
        
add_post_meta($post->ID"dfr_featured_image_is_set""yes");

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

Eric

jerseyal December 10th, 2012 09:56 PM

It works! Thank you!

froogly October 15th, 2013 03:10 PM

Hi, do I copy that code anywhere inside my functions.php file? Do I enclose this code with any other code? I really would love for this to work, to get creative with datafeedr, most plugins, use the featured image.

Thank You

Eric October 15th, 2013 03:12 PM

Hi,

That code should go at the bottom of your theme's functions.php file. This can sometimes be tricky.

Make sure it appears before the final line that contains ?>

Eric

froogly October 17th, 2013 01:54 PM

Hi Eric, is this what the server needs to be able to do this - allow_url_fopen = On - for some reason, some pictures loaded fine, and some did not load, after I placed the code.

thank you

Eric October 17th, 2013 01:58 PM

Hi,

No, that code doesn't require allow_url_fopen... at least I don't see use of that function in the code. That code requires:

file_get_content()
fopen()
fputs()

However, some pictures might not load fine on your site because the merchant has blocked the remote downloading of their images. There's not much you can do about that...

Eric

froogly October 17th, 2013 02:52 PM

Hi Eric thank you for the prompt response, I think that might be the case, is there a way to filter stores, based on remote download capabilities?


All times are GMT -5. The time now is 10:21 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.