Using custom meta boxes to generate the Featured Image thumbnail

26 views
Skip to first unread message

Perry

unread,
Mar 29, 2012, 6:13:30 AM3/29/12
to manchester-word...@googlegroups.com
I've created a custom post type and a custom meta box that accepts a reference number which is also part of a related image file name e.g.:

In the meta box I enter 1993.83.04.09 which relates to the image 1993.83.04.09.jpg

So far I have been able to create a single page custom template that pulls this custom metadata, appends '.jpg' and displays the related image for the post once it is published.

This makes adding images to the custom posts very easy and supports my ultimate aim of bulk uploading all post data via csv.

The only thing is I still have to manually create the thumbnail for the Featured Image by going into the post editor (there will be 1000+ images on the site so I want to avoid this if possible).

Is there any way that I can use the meta box data again to generate the post thumbnail too?

Perry

Mike Little

unread,
Mar 29, 2012, 7:25:41 AM3/29/12
to manchester-word...@googlegroups.com
You might be able to use the add from server plugin 
which can be used to point at a folder of images on your server (e.g. after they have been ftp-ed there) and it will add them into the media library. I think it will also generate the thumbnails as it does so too.

If that one doesn't generate the thumbnails, you can use the regenerate thumbnails plugin

Or you could pick apart the latter's code and call the generate code as you need it.



Mike
--
Mike Little
http://zed1.com/

Perry

unread,
Mar 29, 2012, 7:50:46 AM3/29/12
to manchester-word...@googlegroups.com
Cheers Mike - I'm already using these actually, the thumbnails are there on the server.

Unless I set the Featured Image in the post editor manually no thumbnail appears alongside my posts on my archive pages or next to excerpts (see the link at top of this page: http://spentest.blmsbox.co.uk/photograph)

I am using the Prototype theme (based on the Hybrid theme framework) as a parent for my child theme btw, in case this has any bearing.

Mike Little

unread,
Mar 29, 2012, 8:07:35 AM3/29/12
to manchester-word...@googlegroups.com
On Thu, Mar 29, 2012 at 12:50, Perry <pbon...@gmail.com> wrote:
Cheers Mike - I'm already using these actually, the thumbnails are there on the server.

Unless I set the Featured Image in the post editor manually no thumbnail appears alongside my posts on my archive pages or next to excerpts (see the link at top of this page: http://spentest.blmsbox.co.uk/photograph)

I am using the Prototype theme (based on the Hybrid theme framework) as a parent for my child theme btw, in case this has any bearing.


OK, I see what you mean now. Basically, you need to write some more code!

After you save the data from the meta box (presumably on the save_post hook), look for a matching image, check the correct sized version exists, and add it to (or replace) the post as the featured image.

Basically automating what would be done by hand.
 
Note: if you want to make these changes in the save_post hook, and it involves modifying the post record itself, you must use the database directly (but still via the DB API), you cannot use wp_update_post()! The save_post hook is called near the end of wp_insert_post(), which is called by wp_update_post(),  so your hook will get called again and you'll be in an endless loop. Been there, done that, killed apache! If the modifications only involve meta data, you are good to go.
 
An alternative is to use the wp_insert_post hook, which is called after save_post.

Mike

Perry

unread,
Mar 29, 2012, 8:17:00 AM3/29/12
to manchester-word...@googlegroups.com
Cheers Mike - I'll have a look into that - precious little on the web (or at least I'm not searching for the right thing!)

Perry

unread,
Apr 11, 2012, 5:59:20 AM4/11/12
to manchester-word...@googlegroups.com
I think I may have a possible solution via wp_insert_attachment() but I can't find much documentation and my PHP and WP understanding is too basic to work out how to use it (or when to call it).

The situation so far is this:

My bulk images have been imported and processed (via the add from server and regenerate thumbnails plugins) all I need to do is attach the images to the relevant posts.

I have a custom field for a reference number (e.g. 1993.93.02.09) which refers to the relevant image filenames (e.g. 1993.93.02.09.jpg 1993.93.02.09-138x200.jpg etc).

I'm sure that it must be possible to enter the reference number and on saving reconstruct the image file path ( wp_upload_dir() + filename +.jpg) and pass it to wp_insert_attachment() to attach the image to the post.

Once the image is attached all the thumbnail generation takes care of itself.

Am I barking up the wrong tree?

Mike Little

unread,
Apr 11, 2012, 6:20:42 AM4/11/12
to manchester-word...@googlegroups.com
On Wed, Apr 11, 2012 at 10:59, Perry <pbon...@gmail.com> wrote:
I think I may have a possible solution via wp_insert_attachment() but I can't find much documentation and my PHP and WP understanding is too basic to work out how to use it (or when to call it).

The situation so far is this:

My bulk images have been imported and processed (via the add from server and regenerate thumbnails plugins) all I need to do is attach the images to the relevant posts.

I have a custom field for a reference number (e.g. 1993.93.02.09) which refers to the relevant image filenames (e.g. 1993.93.02.09.jpg 1993.93.02.09-138x200.jpg etc).

I'm sure that it must be possible to enter the reference number and on saving reconstruct the image file path ( wp_upload_dir() + filename +.jpg) and pass it to wp_insert_attachment() to attach the image to the post.

Once the image is attached all the thumbnail generation takes care of itself.

Am I barking up the wrong tree?


That's nearly the right tree. 
If the images are already in the media library (via the add from server plugin) then the wp_insert_attachment part has already been done.

I mentioned the 'save+post' hook previously that will get called as you save the post after adding the reference number.  
You need to grab that reference number (from the _POST array) and look for your image file in the media library (using either get_posts or a standard wp_query - I think the post_title for the attachment will be the image filename without the extension), 
then, once you have the attachment ID, you can use it to update the post_meta (meta_key '_thumbnail_id', meta_value, the attachment ID) for the post being saved.

Mike

Perry

unread,
Apr 11, 2012, 7:19:13 AM4/11/12
to manchester-word...@googlegroups.com
Ah in that case I misunderstood the purpose of wp_insert_attachment() - I found a few references to using it make attachments to parents which obviously threw me off.

I'll keep digging.

Cheers Mike :)

On Wednesday, April 11, 2012 11:20:42 AM UTC+1, Mike Little wrote:
Reply all
Reply to author
Forward
0 new messages