You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

84 lines
2.1 KiB

  1. /* global ajaxurl */
  2. (function($) {
  3. $(document).ready(function() {
  4. var frame,
  5. bgImage = $( '#custom-background-image' );
  6. $('#background-color').wpColorPicker({
  7. change: function( event, ui ) {
  8. bgImage.css('background-color', ui.color.toString());
  9. },
  10. clear: function() {
  11. bgImage.css('background-color', '');
  12. }
  13. });
  14. $( 'select[name="background-size"]' ).change( function() {
  15. bgImage.css( 'background-size', $( this ).val() );
  16. });
  17. $( 'input[name="background-position"]' ).change( function() {
  18. bgImage.css( 'background-position', $( this ).val() );
  19. });
  20. $( 'input[name="background-repeat"]' ).change( function() {
  21. bgImage.css( 'background-repeat', $( this ).is( ':checked' ) ? 'repeat' : 'no-repeat' );
  22. });
  23. $( 'input[name="background-attachment"]' ).change( function() {
  24. bgImage.css( 'background-attachment', $( this ).is( ':checked' ) ? 'scroll' : 'fixed' );
  25. });
  26. $('#choose-from-library-link').click( function( event ) {
  27. var $el = $(this);
  28. event.preventDefault();
  29. // If the media frame already exists, reopen it.
  30. if ( frame ) {
  31. frame.open();
  32. return;
  33. }
  34. // Create the media frame.
  35. frame = wp.media.frames.customBackground = wp.media({
  36. // Set the title of the modal.
  37. title: $el.data('choose'),
  38. // Tell the modal to show only images.
  39. library: {
  40. type: 'image'
  41. },
  42. // Customize the submit button.
  43. button: {
  44. // Set the text of the button.
  45. text: $el.data('update'),
  46. // Tell the button not to close the modal, since we're
  47. // going to refresh the page when the image is selected.
  48. close: false
  49. }
  50. });
  51. // When an image is selected, run a callback.
  52. frame.on( 'select', function() {
  53. // Grab the selected attachment.
  54. var attachment = frame.state().get('selection').first();
  55. // Run an AJAX request to set the background image.
  56. $.post( ajaxurl, {
  57. action: 'set-background-image',
  58. attachment_id: attachment.id,
  59. size: 'full'
  60. }).done( function() {
  61. // When the request completes, reload the window.
  62. window.location.reload();
  63. });
  64. });
  65. // Finally, open the modal.
  66. frame.open();
  67. });
  68. });
  69. })(jQuery);