Guest User

Untitled

a guest
Sep 27th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. /* PDF Tear Sheet on Product Pages */
  2.  
  3. //Add Metabox
  4. add_action('add_meta_boxes', 'add_upload_file_metaboxes');
  5.  
  6. function add_upload_file_metaboxes() {
  7. add_meta_box('swp_file_upload', 'File Upload', 'swp_file_upload', 'product', 'normal', 'default');
  8. }
  9.  
  10.  
  11. function swp_file_upload() {
  12. global $post;
  13. // Noncename needed to verify where the data originated
  14. echo '<input type="hidden" name="tearsheetmeta_noncename" id="tearsheetmeta_noncename" value="'.
  15. wp_create_nonce(plugin_basename(__FILE__)).
  16. '" />';
  17. global $wpdb;
  18. $strFile = get_post_meta($post -> ID, $key = 'tearsheet_file', true);
  19. $media_file = get_post_meta($post -> ID, $key = '_wp_attached_file', true);
  20. if (!empty($media_file)) {
  21. $strFile = $media_file;
  22. } ?>
  23.  
  24.  
  25. <script type = "text/javascript">
  26.  
  27. // Uploading files
  28. var file_frame;
  29. jQuery('#upload_image_button').live('click', function(product) {
  30.  
  31. podcast.preventDefault();
  32.  
  33. // If the media frame already exists, reopen it.
  34. if (file_frame) {
  35. file_frame.open();
  36. return;
  37. }
  38.  
  39. // Create the media frame.
  40. file_frame = wp.media.frames.file_frame = wp.media({
  41. title: jQuery(this).data('uploader_title'),
  42. button: {
  43. text: jQuery(this).data('uploader_button_text'),
  44. },
  45. multiple: false // Set to true to allow multiple files to be selected
  46. });
  47.  
  48. // When a file is selected, run a callback.
  49. file_frame.on('select', function(){
  50. // We set multiple to false so only get one image from the uploader
  51. attachment = file_frame.state().get('selection').first().toJSON();
  52.  
  53. // here are some of the variables you could use for the attachment;
  54. //var all = JSON.stringify( attachment );
  55. //var id = attachment.id;
  56. //var title = attachment.title;
  57. //var filename = attachment.filename;
  58. var url = attachment.url;
  59. //var link = attachment.link;
  60. //var alt = attachment.alt;
  61. //var author = attachment.author;
  62. //var description = attachment.description;
  63. //var caption = attachment.caption;
  64. //var name = attachment.name;
  65. //var status = attachment.status;
  66. //var uploadedTo = attachment.uploadedTo;
  67. //var date = attachment.date;
  68. //var modified = attachment.modified;
  69. //var type = attachment.type;
  70. //var subtype = attachment.subtype;
  71. //var icon = attachment.icon;
  72. //var dateFormatted = attachment.dateFormatted;
  73. //var editLink = attachment.editLink;
  74. //var fileLength = attachment.fileLength;
  75.  
  76. var field = document.getElementById("tearsheet_file");
  77.  
  78. field.value = url; //set which variable you want the field to have
  79. });
  80.  
  81. // Finally, open the modal
  82. file_frame.open();
  83. });
  84.  
  85. </script>
  86.  
  87.  
  88.  
  89. <div>
  90.  
  91. <table>
  92. <tr valign = "top">
  93. <td>
  94. <input type = "text"
  95. name = "tearsheet_file"
  96. id = "tearsheet_file"
  97. size = "70"
  98. value = "<?php echo $strFile; ?>" />
  99. <input id = "upload_image_button"
  100. type = "button"
  101. value = "Upload">
  102. </td> </tr> </table> <input type = "hidden"
  103. name = "img_txt_id"
  104. id = "img_txt_id"
  105. value = "" />
  106. </div> <?php
  107. function admin_scripts() {
  108. wp_enqueue_script('media-upload');
  109. wp_enqueue_script('thickbox');
  110. }
  111.  
  112. function admin_styles() {
  113. wp_enqueue_style('thickbox');
  114. }
  115. add_action('admin_print_scripts', 'admin_scripts');
  116. add_action('admin_print_styles', 'admin_styles');
  117. }
  118.  
  119.  
  120. //Saving the file
  121. function save_tearsheet_meta($post_id, $post) {
  122. // verify this came from the our screen and with proper authorization,
  123. // because save_post can be triggered at other times
  124. if (!wp_verify_nonce($_POST['tearsheetmeta_noncename'], plugin_basename(__FILE__))) {
  125. return $post -> ID;
  126. }
  127. // Is the user allowed to edit the post?
  128. if (!current_user_can('edit_post', $post -> ID))
  129. return $post -> ID;
  130. // We need to find and save the data
  131. // We'll put it into an array to make it easier to loop though.
  132. $tearsheet_meta['tearsheet_file'] = $_POST['tearsheet_file'];
  133. // Add values of $tearsheet_meta as custom fields
  134.  
  135. foreach($tearsheet_meta as $key => $value) {
  136. if ($post -> post_type == 'revision') return;
  137. $value = implode(',', (array) $value);
  138. if (get_post_meta($post -> ID, $key, FALSE)) { // If the custom field already has a value it will update
  139. update_post_meta($post -> ID, $key, $value);
  140. } else { // If the custom field doesn't have a value it will add
  141. add_post_meta($post -> ID, $key, $value);
  142. }
  143. if (!$value) delete_post_meta($post -> ID, $key); // Delete if blank value
  144. }
  145. }
  146. add_action('save_post', 'save_tearsheet_meta', 1, 2); // save the custom fields
Add Comment
Please, Sign In to add comment