script_backend.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var $ = jQuery;
  2. $(document).ready(function() {
  3. //$("***").ttt();
  4. console.log("OKOKOKO K plugin");
  5. var mediaUploader;
  6. $('#upload_image_button').click(function(e) {
  7. e.preventDefault();
  8. if (mediaUploader) {
  9. mediaUploader.open();
  10. return;
  11. }
  12. mediaUploader = wp.media.frames.file_frame = wp.media({
  13. title: 'Choose Image',
  14. button: {
  15. text: 'Choose Image'
  16. },
  17. multiple: false
  18. });
  19. mediaUploader.on('select', function() {
  20. var attachment = mediaUploader.state().get('selection').first().toJSON();
  21. console.log("LLLL",attachment);
  22. $('#background_image').attr("src",attachment.url);
  23. });
  24. mediaUploader.open();
  25. });
  26. /*
  27. $('#upload_image_button').click(function() {
  28. console.log("UPLLOADDD");
  29. formfield = $('#link_image').attr('name');
  30. tb_show('', 'media-upload.php?type=image&TB_iframe=true');
  31. return false;
  32. });
  33. window.send_to_editor = function(html) {
  34. imgurl = $('img',html).attr('src');
  35. imgsrc = '<img src="'+imgurl+'">';
  36. $('#link_image').val(imgurl); // this populates a text field with the image url
  37. //$('#my-link-img').html(imgsrc); // this prints the image into a div
  38. tb_remove();
  39. }
  40. */
  41. $('.custom_media').each(function() {
  42. var self = $(this);
  43. $('.custom_media_upload', self).click(function() {
  44. var send_attachment_bkp = wp.media.editor.send.attachment;
  45. wp.media.editor.send.attachment = function(props, attachment) {
  46. $('.custom_media_image', self).attr('src', attachment.url);
  47. var path = "/" + attachment.url.split("/").splice(3).join("/");
  48. $('.custom_media_url', self).val(path);
  49. $('.custom_media_id', self).val(attachment.id);
  50. wp.media.editor.send.attachment = send_attachment_bkp;
  51. }
  52. wp.media.editor.open();
  53. return false;
  54. });
  55. });
  56. });