Skip to content

Instantly share code, notes, and snippets.

@marteinn
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save marteinn/8729903 to your computer and use it in GitHub Desktop.

Select an option

Save marteinn/8729903 to your computer and use it in GitHub Desktop.
An example on how to register your own attachment as feature image from the Wordpress media lightbox.
// Pick up data from service.php and send returned data to wp ajax admin.
var win = window.dialogArguments || opener || parent || top
$.ajax({
url: ajaxurl,
type: "POST",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data: postData,
success: function(str){
if ( str === '0' ) {
alert(setPostThumbnailL10n.error);
} else {
jQuery('a.wp-post-thumbnail').show();
win.WPSetThumbnailID(postId);
win.WPSetThumbnailHTML(str);
win.tb_remove();
}
}
});
<?php
// Return this data to js, so we can perform a wp ajax request.
// Can be used in conjunktion with my save image to wordpress gist (https://gist.github.com/marteinn/6174127).
$data = array(
"post_id" => $post_id,
"thumbnail_id" => $attachment_id,
"_wpnonce" => wp_create_nonce("set_post_thumbnail-".$post_id),
"action" => "set-post-thumbnail"
);
echo json_encode($data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment