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 set the post thumbnail (featured image) with javascript from an existing image in the media gallery. I would consider a php-based option too I suppose (one that would require clicking update before the images show up).

Some Background

I'm building a site for a music venue with a custom "event" post-type. Most of the time these events are unique, but there are some recurring ideas (for example, open mic night most tuesdays).

I was thinking I'd like to put in a dropdown with some "presets" to populate the add new event form fields in the backend. It's a cinch (I think) to do this with the the tinymce, and my custom meta fields. The tough part is how to to put an (already uploaded to media library) image into the featured image box programmaticly.

I know I can do make something work on the template end of things, but it would be nice to see that thumbnail pop up in the add new/edit view.

Thanks!

share|improve this question
    
So you are asking how to display an image chosen from a combobox in the TinyMCE preview? –  fdsa Jun 7 '12 at 17:45
    
Nope :) I'm trying to set the post thumbnail (featured image) from an existing image in the media gallery with javascript. I'll try to update the question to make it more clear. –  Zach L Jun 7 '12 at 17:55
    
I'm still confused about what you're asking...have you looked at making a [tinymce plugin] (tinymce.com/wiki.php/Creating_a_plugin)? –  fdsa Jun 8 '12 at 2:08
    
Nope. I just want a custom meta box with a drop down on each event post type. When the option is changed, it will change certain other values on the page: the title, the contents of the tinymce (just text), a "date" meta field, and (hopefully) the featured Image. I'm pretty sure I can change all these values, except I've been stuck on the featured image. –  Zach L Jun 8 '12 at 13:45

3 Answers 3

up vote 2 down vote accepted
+50

You need to do an ajax call with action: 'set-post-thumbnail'

Check in admin-ajax.php (line 1477 in 3.3.2) for the expected values and nonce, but in general you need to send post_id, attachment_id and nonce.

The nonce should come from: wp_create_nonce( "set_post_thumbnail-$post_id" );

The admin does something like:

 uploader.bind('FileUploaded', function(up, file, response) {
            jQuery.post(ajaxurl, {
                    action:"set-post-thumbnail", post_id: post_id, thumbnail_id: response.response, _ajax_nonce: '<?php echo $ajax_nonce;?>' , cookie: encodeURIComponent(document.cookie)
                }, function(str){
                    var win = window.dialogArguments || opener || parent || top;
                    if ( str == '0' ) {
                        alert( setPostThumbnailL10n.error );
                    } else {
                         jQuery('#postimagediv .inside').html(str);
                         jQuery('#postimagediv .inside #plupload-upload-ui').hide();

                    }
                }
                );
                jQuery("#postimagediv .inside h2.uploading_message").remove();

        });
share|improve this answer

I think update_post_meta function will make the attachment featured.

update_post_meta($post_id, '_thumbnail_id', $attachment_id);
share|improve this answer
    
aww jeez - this was in fdsa's answer before he edited it, and I thought it was incorrect, but after research - this can do what I want (wordpress.org/support/topic/…) –  Zach L Jun 8 '12 at 13:41
    
I have no idea who to give the bounty to, but I'll wait to see if fdsa udpates his answer or if someone can furnish a javascript solution that doesn't require clicking Update. –  Zach L Jun 8 '12 at 13:43

You could display the images in a jquery image combobox, have the user click 'update,' and then use set_post_meta or update_post_meta to put the image in the post.

If you don't want to add an extra button you could use the solution to this question to save the meta data for your custom post type.

share|improve this answer
    
I may be confused, but those functions don't look like they are associated with the post thumbnail (featured image). I have no problem adding metadata to the post - I just want to see if I can do it this way instead. –  Zach L Jun 7 '12 at 18:17
    
I edited it...is that what you're looking for? –  fdsa Jun 7 '12 at 18:25
    
unfortunately no. the_post_thumbnail is used to display the featured image in a template - not to set it on the edit screen. –  Zach L Jun 7 '12 at 18:37
    
You were actually on track with your first (unedited) answer. My mistake. see Albin Joesphs answer... –  Zach L Jun 8 '12 at 13:46
    
Put old answer back up –  fdsa Jun 9 '12 at 2:11

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.