Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. <?php
  2. /* Add WP Media Uploader in Plugin */
  3. add_action('admin_enqueue_scripts', 'custom_admin_media_scripts');
  4.  
  5. function custom_admin_media_scripts() {
  6. if (isset($_GET['page']) && $_GET['page'] == 'custom-icons-settings-page') {
  7. wp_enqueue_media();
  8. wp_register_script('custom-media-uploader-js', WP_PLUGIN_URL.'/custom-theme-settings-plugin/js/media.js', array('jquery'));
  9. wp_enqueue_script('custom-media-uploader-js');
  10. }
  11. }
  12. /* Add WP Media Uploader in Plugin End */
  13. ?>
  14.  
  15. <!-- Add JS code in media.js file of plugin -->
  16. <script>
  17. jQuery(document).ready(function($){
  18.  
  19.  
  20. var custom_uploader;
  21.  
  22.  
  23. $('#upload_image_button').click(function(e) {
  24.  
  25. e.preventDefault();
  26.  
  27. //If the uploader object has already been created, reopen the dialog
  28. if (custom_uploader) {
  29. custom_uploader.open();
  30. return;
  31. }
  32.  
  33. //Extend the wp.media object
  34. custom_uploader = wp.media.frames.file_frame = wp.media({
  35. title: 'Choose Image',
  36. button: {
  37. text: 'Choose Image'
  38. },
  39. multiple: false
  40. });
  41.  
  42. //When a file is selected, grab the URL and set it as the text field's value
  43. custom_uploader.on('select', function() {
  44. attachment = custom_uploader.state().get('selection').first().toJSON();
  45. $('#upload_image').val(attachment.url);
  46. });
  47.  
  48. //Open the uploader dialog
  49. custom_uploader.open();
  50.  
  51. });
  52.  
  53.  
  54. });
  55. </script>
  56. <!-- Add JS code in media.js file of plugin End -->
  57.  
  58. <!-- Add button code in Template file -->
  59. <label for="upload_image">
  60. <input id="add_icon_image" type="text" name="add_icon_image" value="" />
  61. <input id="upload_image_button" class="button" type="button" value="Upload Image" />
  62. <br />Enter a URL or upload an image
  63. </label>
  64. <!-- Add button code in Template file -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement