3 $(document).ready(function() {
5 bgImage = $( '#custom-background-image' );
7 $('#background-color').wpColorPicker({
8 change: function( event, ui ) {
9 bgImage.css('background-color', ui.color.toString());
12 bgImage.css('background-color', '');
16 $( 'select[name="background-size"]' ).change( function() {
17 bgImage.css( 'background-size', $( this ).val() );
20 $( 'input[name="background-position"]' ).change( function() {
21 bgImage.css( 'background-position', $( this ).val() );
24 $( 'input[name="background-repeat"]' ).change( function() {
25 bgImage.css( 'background-repeat', $( this ).is( ':checked' ) ? 'repeat' : 'no-repeat' );
28 $( 'input[name="background-attachment"]' ).change( function() {
29 bgImage.css( 'background-attachment', $( this ).is( ':checked' ) ? 'scroll' : 'fixed' );
32 $('#choose-from-library-link').click( function( event ) {
35 event.preventDefault();
37 // If the media frame already exists, reopen it.
43 // Create the media frame.
44 frame = wp.media.frames.customBackground = wp.media({
45 // Set the title of the modal.
46 title: $el.data('choose'),
48 // Tell the modal to show only images.
53 // Customize the submit button.
55 // Set the text of the button.
56 text: $el.data('update'),
57 // Tell the button not to close the modal, since we're
58 // going to refresh the page when the image is selected.
63 // When an image is selected, run a callback.
64 frame.on( 'select', function() {
65 // Grab the selected attachment.
66 var attachment = frame.state().get('selection').first();
68 // Run an AJAX request to set the background image.
70 action: 'set-background-image',
71 attachment_id: attachment.id,
74 // When the request completes, reload the window.
75 window.location.reload();
79 // Finally, open the modal.