Last active
August 29, 2015 13:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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(); | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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