admin管理员组文章数量:1414908
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!
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 edited Jun 7, 2012 at 17:59 Zach Lysobey asked Jun 3, 2012 at 18:30 Zach LysobeyZach Lysobey 8391 gold badge15 silver badges32 bronze badges 4 |4 Answers
Reset to default 3 +50You 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();
});
I think update_post_meta function will make the attachment featured.
update_post_meta($post_id, '_thumbnail_id', $attachment_id);
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.
This solution didn't exist at the time this question was asked, but since I was led here in my quest to find an answer, I figure it might help someone else.
I'm not sure how to capture the upload event as I'm using CMB2's event cmb_media_modal_select
, but setting the featured image is as simple as:
wp.media.featuredImage.set(id)
More on this portion of the JS API here.
For me, with CMB2, I am using the following:
$( document ).on( 'cmb_media_modal_select', function ( e, selection, media ) {
let currentMediaId = wp.media.featuredImage.get();
if( currentMediaId === -1 ) {
let newMediaId = selection.models[ 0 ].id;
console.log( newMediaId );
wp.media.featuredImage.set( newMediaId );
}
} );
Hope this helps someone!
本文标签: jquerySet Featured Image programmatically (in admin) with JavaScript
版权声明:本文标题:jquery - Set Featured Image programmatically (in admin) with JavaScript? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745158155a2645300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 Lysobey Commented Jun 8, 2012 at 13:45