3 * The custom background script.
6 * @subpackage Administration
10 * The custom background class.
14 * @subpackage Administration
16 class Custom_Background {
19 * Callback for administration header.
25 var $admin_header_callback;
28 * Callback for header div.
34 var $admin_image_div_callback;
37 * Holds the page menu hook.
46 * Constructor - Register administration header callback.
49 * @param callback $admin_header_callback
50 * @param callback $admin_image_div_callback Optional custom image div output callback.
51 * @return Custom_Background
53 function __construct($admin_header_callback = '', $admin_image_div_callback = '') {
54 $this->admin_header_callback = $admin_header_callback;
55 $this->admin_image_div_callback = $admin_image_div_callback;
57 add_action( 'admin_menu', array( $this, 'init' ) );
58 add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
62 * Set up the hooks for the Custom Background admin page.
67 if ( ! current_user_can('edit_theme_options') )
70 $this->page = $page = add_theme_page(__('Background'), __('Background'), 'edit_theme_options', 'custom-background', array(&$this, 'admin_page'));
72 add_action("load-$page", array(&$this, 'admin_load'));
73 add_action("load-$page", array(&$this, 'take_action'), 49);
74 add_action("load-$page", array(&$this, 'handle_upload'), 49);
76 if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) {
77 add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
78 add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
79 add_filter( 'media_upload_mime_type_links', '__return_empty_array' );
82 if ( $this->admin_header_callback )
83 add_action("admin_head-$page", $this->admin_header_callback, 51);
87 * Set up the enqueue for the CSS & JavaScript files.
91 function admin_load() {
92 get_current_screen()->add_help_tab( array(
94 'title' => __('Overview'),
96 '<p>' . __( 'You can customize the look of your site without touching any of your theme’s code by using a custom background. Your background can be an image or a color.' ) . '</p>' .
97 '<p>' . __( 'To use a background image, simply upload it, then choose your display options below. You can display a single instance of your image, or tile it to fill the screen. You can have your background fixed in place, so your site content moves on top of it, or you can have it scroll with your site.' ) . '</p>' .
98 '<p>' . __( 'You can also choose a background color. If you know the hexadecimal code for the color you want, enter it in the Background Color field. If not, click on the Select a Color link, and a color picker will allow you to choose the exact shade you want.' ) . '</p>' .
99 '<p>' . __( 'Don’t forget to click on the Save Changes button when you are finished.' ) . '</p>'
102 get_current_screen()->set_help_sidebar(
103 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
104 '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Background_Screen" target="_blank">Documentation on Custom Background</a>' ) . '</p>' .
105 '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
109 wp_enqueue_script('media-upload');
110 wp_enqueue_script('custom-background');
111 wp_enqueue_style('farbtastic');
115 * Execute custom background modification.
119 function take_action() {
124 if ( isset($_POST['reset-background']) ) {
125 check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
126 remove_theme_mod('background_image');
127 remove_theme_mod('background_image_thumb');
128 $this->updated = true;
132 if ( isset($_POST['remove-background']) ) {
133 // @TODO: Uploaded files are not removed here.
134 check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
135 set_theme_mod('background_image', '');
136 set_theme_mod('background_image_thumb', '');
137 $this->updated = true;
138 wp_safe_redirect( $_POST['_wp_http_referer'] );
142 if ( isset($_POST['background-repeat']) ) {
143 check_admin_referer('custom-background');
144 if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) )
145 $repeat = $_POST['background-repeat'];
148 set_theme_mod('background_repeat', $repeat);
151 if ( isset($_POST['background-position-x']) ) {
152 check_admin_referer('custom-background');
153 if ( in_array($_POST['background-position-x'], array('center', 'right', 'left')) )
154 $position = $_POST['background-position-x'];
157 set_theme_mod('background_position_x', $position);
160 if ( isset($_POST['background-attachment']) ) {
161 check_admin_referer('custom-background');
162 if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
163 $attachment = $_POST['background-attachment'];
165 $attachment = 'fixed';
166 set_theme_mod('background_attachment', $attachment);
169 if ( isset($_POST['background-color']) ) {
170 check_admin_referer('custom-background');
171 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
172 if ( strlen($color) == 6 || strlen($color) == 3 )
173 set_theme_mod('background_color', $color);
175 set_theme_mod('background_color', '');
178 $this->updated = true;
182 * Display the custom background page.
186 function admin_page() {
188 <div class="wrap" id="custom-background">
189 <?php screen_icon(); ?>
190 <h2><?php _e('Custom Background'); ?></h2>
191 <?php if ( !empty($this->updated) ) { ?>
192 <div id="message" class="updated">
193 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
197 if ( $this->admin_image_div_callback ) {
198 call_user_func($this->admin_image_div_callback);
201 <h3><?php _e('Background Image'); ?></h3>
202 <table class="form-table">
205 <th scope="row"><?php _e('Preview'); ?></th>
208 $background_styles = '';
209 if ( $bgcolor = get_background_color() )
210 $background_styles .= 'background-color: #' . $bgcolor . ';';
212 if ( get_background_image() ) {
213 // background-image URL must be single quote, see below
214 $background_styles .= ' background-image: url(\'' . set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ) . '\');'
215 . ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
216 . ' background-position: top ' . get_theme_mod('background_position_x', 'left');
219 <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
220 <?php if ( get_background_image() ) { ?>
221 <img class="custom-background-image" src="<?php echo set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ); ?>" style="visibility:hidden;" alt="" /><br />
222 <img class="custom-background-image" src="<?php echo set_url_scheme( get_theme_mod( 'background_image_thumb', get_background_image() ) ); ?>" style="visibility:hidden;" alt="" />
228 <?php if ( get_background_image() ) : ?>
230 <th scope="row"><?php _e('Remove Image'); ?></th>
232 <form method="post" action="">
233 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
234 <?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/>
235 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
241 <?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
242 <?php if ( $default_image && get_background_image() != $default_image ) : ?>
244 <th scope="row"><?php _e('Restore Original Image'); ?></th>
246 <form method="post" action="">
247 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
248 <?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/>
249 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
256 <th scope="row"><?php _e('Select Image'); ?></th>
257 <td><form enctype="multipart/form-data" id="upload-form" method="post" action="">
259 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
260 <input type="file" id="upload" name="import" />
261 <input type="hidden" name="action" value="save" />
262 <?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
263 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
266 $image_library_url = get_upload_iframe_src( 'image', null, 'library' );
267 $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
268 $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
271 <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
272 <a id="choose-from-library-link" class="button thickbox" href="<?php echo esc_url( $image_library_url ); ?>"><?php _e( 'Choose Image' ); ?></a>
280 <h3><?php _e('Display Options') ?></h3>
281 <form method="post" action="">
282 <table class="form-table">
284 <?php if ( get_background_image() ) : ?>
286 <th scope="row"><?php _e( 'Position' ); ?></th>
287 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
289 <input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
293 <input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
294 <?php _e('Center') ?>
297 <input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
304 <th scope="row"><?php _e( 'Repeat' ); ?></th>
305 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
306 <label><input type="radio" name="background-repeat" value="no-repeat"<?php checked('no-repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('No Repeat'); ?></label>
307 <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label>
308 <label><input type="radio" name="background-repeat" value="repeat-x"<?php checked('repeat-x', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile Horizontally'); ?></label>
309 <label><input type="radio" name="background-repeat" value="repeat-y"<?php checked('repeat-y', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile Vertically'); ?></label>
314 <th scope="row"><?php _e( 'Attachment' ); ?></th>
315 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
317 <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
318 <?php _e('Scroll') ?>
321 <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
326 <?php endif; // get_background_image() ?>
328 <th scope="row"><?php _e( 'Background Color' ); ?></th>
329 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
330 <?php $show_clear = get_theme_mod('background_color') ? '' : ' style="display:none"'; ?>
331 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" />
332 <a class="hide-if-no-js" href="#" id="pickcolor"><?php _e('Select a Color'); ?></a> <span<?php echo $show_clear; ?> class="hide-if-no-js" id="clearcolor"> (<a href="#"><?php current_theme_supports( 'custom-background', 'default-color' ) ? _e( 'Default' ) : _e( 'Clear' ); ?></a>)</span>
333 <input type="hidden" id="defaultcolor" value="<?php if ( current_theme_supports( 'custom-background', 'default-color' ) ) echo '#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ); ?>" />
334 <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
340 <?php wp_nonce_field('custom-background'); ?>
341 <?php submit_button( null, 'primary', 'save-background-options' ); ?>
349 * Handle an Image upload for the background image.
353 function handle_upload() {
355 if ( empty($_FILES) )
358 check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
359 $overrides = array('test_form' => false);
360 $file = wp_handle_upload($_FILES['import'], $overrides);
362 if ( isset($file['error']) )
363 wp_die( $file['error'] );
366 $type = $file['type'];
367 $file = $file['file'];
368 $filename = basename($file);
370 // Construct the object array
372 'post_title' => $filename,
373 'post_content' => $url,
374 'post_mime_type' => $type,
376 'context' => 'custom-background'
380 $id = wp_insert_attachment($object, $file);
383 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
384 update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
386 set_theme_mod('background_image', esc_url_raw($url));
388 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
389 set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
391 do_action('wp_create_file_in_uploads', $file, $id); // For replication
392 $this->updated = true;
396 * Replace default attachment actions with "Set as background" link.
400 function attachment_fields_to_edit( $form_fields, $post ) {
401 $form_fields = array( 'image-size' => $form_fields['image-size'] );
402 $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td><a data-attachment-id="' . $post->ID . '" class="wp-set-background">' . __( 'Set as background' ) . '</a></td></tr>' );
403 $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
409 * Leave only "Media Library" tab in the uploader window.
413 function filter_upload_tabs() {
414 return array( 'library' => __('Media Library') );
417 public function wp_set_background_image() {
418 if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
419 $attachment_id = absint($_POST['attachment_id']);
420 $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
422 if ( in_array( $_POST['size'], $sizes ) )
423 $size = esc_attr( $_POST['size'] );
425 update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
426 $url = wp_get_attachment_image_src( $attachment_id, $size );
427 $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
428 set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
429 set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );