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 ( $this->admin_header_callback )
77 add_action("admin_head-$page", $this->admin_header_callback, 51);
81 * Set up the enqueue for the CSS & JavaScript files.
85 function admin_load() {
86 get_current_screen()->add_help_tab( array(
88 'title' => __('Overview'),
90 '<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>' .
91 '<p>' . __( 'To use a background image, simply upload it or choose an image that has already been uploaded to your Media Library by clicking the “Choose Image” button. 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>' .
92 '<p>' . __( 'You can also choose a background color by clicking the Select Color button and either typing in a legitimate HTML hex value, e.g. “#ff0000” for red, or by choosing a color using the color picker.' ) . '</p>' .
93 '<p>' . __( 'Don’t forget to click on the Save Changes button when you are finished.' ) . '</p>'
96 get_current_screen()->set_help_sidebar(
97 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
98 '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Background_Screen" target="_blank">Documentation on Custom Background</a>' ) . '</p>' .
99 '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
103 wp_enqueue_script('custom-background');
104 wp_enqueue_style('wp-color-picker');
108 * Execute custom background modification.
112 function take_action() {
117 if ( isset($_POST['reset-background']) ) {
118 check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
119 remove_theme_mod('background_image');
120 remove_theme_mod('background_image_thumb');
121 $this->updated = true;
125 if ( isset($_POST['remove-background']) ) {
126 // @TODO: Uploaded files are not removed here.
127 check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
128 set_theme_mod('background_image', '');
129 set_theme_mod('background_image_thumb', '');
130 $this->updated = true;
131 wp_safe_redirect( $_POST['_wp_http_referer'] );
135 if ( isset($_POST['background-repeat']) ) {
136 check_admin_referer('custom-background');
137 if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) )
138 $repeat = $_POST['background-repeat'];
141 set_theme_mod('background_repeat', $repeat);
144 if ( isset($_POST['background-position-x']) ) {
145 check_admin_referer('custom-background');
146 if ( in_array($_POST['background-position-x'], array('center', 'right', 'left')) )
147 $position = $_POST['background-position-x'];
150 set_theme_mod('background_position_x', $position);
153 if ( isset($_POST['background-attachment']) ) {
154 check_admin_referer('custom-background');
155 if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
156 $attachment = $_POST['background-attachment'];
158 $attachment = 'fixed';
159 set_theme_mod('background_attachment', $attachment);
162 if ( isset($_POST['background-color']) ) {
163 check_admin_referer('custom-background');
164 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
165 if ( strlen($color) == 6 || strlen($color) == 3 )
166 set_theme_mod('background_color', $color);
168 set_theme_mod('background_color', '');
171 $this->updated = true;
175 * Display the custom background page.
179 function admin_page() {
181 <div class="wrap" id="custom-background">
182 <?php screen_icon(); ?>
183 <h2><?php _e('Custom Background'); ?></h2>
184 <?php if ( !empty($this->updated) ) { ?>
185 <div id="message" class="updated">
186 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
190 if ( $this->admin_image_div_callback ) {
191 call_user_func($this->admin_image_div_callback);
194 <h3><?php _e('Background Image'); ?></h3>
195 <table class="form-table">
198 <th scope="row"><?php _e('Preview'); ?></th>
201 $background_styles = '';
202 if ( $bgcolor = get_background_color() )
203 $background_styles .= 'background-color: #' . $bgcolor . ';';
205 if ( get_background_image() ) {
206 $background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', get_background_image() ) ) ) );
207 // background-image URL must be single quote, see below
208 $background_styles .= ' background-image: url(\'' . $background_image_thumb . '\');'
209 . ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
210 . ' background-position: top ' . get_theme_mod('background_position_x', 'left');
213 <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
214 <?php if ( get_background_image() ) { ?>
215 <img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" /><br />
216 <img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" />
222 <?php if ( get_background_image() ) : ?>
224 <th scope="row"><?php _e('Remove Image'); ?></th>
226 <form method="post" action="">
227 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
228 <?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/>
229 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
235 <?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
236 <?php if ( $default_image && get_background_image() != $default_image ) : ?>
238 <th scope="row"><?php _e('Restore Original Image'); ?></th>
240 <form method="post" action="">
241 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
242 <?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/>
243 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
250 <th scope="row"><?php _e('Select Image'); ?></th>
251 <td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post" action="">
253 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
254 <input type="file" id="upload" name="import" />
255 <input type="hidden" name="action" value="save" />
256 <?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
257 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
260 <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
261 <a id="choose-from-library-link" class="button"
262 data-choose="<?php esc_attr_e( 'Choose a Background Image' ); ?>"
263 data-update="<?php esc_attr_e( 'Set as background' ); ?>"><?php _e( 'Choose Image' ); ?></a>
271 <h3><?php _e('Display Options') ?></h3>
272 <form method="post" action="">
273 <table class="form-table">
275 <?php if ( get_background_image() ) : ?>
277 <th scope="row"><?php _e( 'Position' ); ?></th>
278 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
280 <input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
284 <input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
285 <?php _e('Center') ?>
288 <input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
295 <th scope="row"><?php _e( 'Repeat' ); ?></th>
296 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
297 <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>
298 <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label>
299 <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>
300 <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>
305 <th scope="row"><?php _ex( 'Attachment', 'Background Attachment' ); ?></th>
306 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
308 <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
309 <?php _e('Scroll') ?>
312 <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
317 <?php endif; // get_background_image() ?>
319 <th scope="row"><?php _e( 'Background Color' ); ?></th>
320 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
323 if ( current_theme_supports( 'custom-background', 'default-color' ) )
324 $default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
326 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color ?> />
332 <?php wp_nonce_field('custom-background'); ?>
333 <?php submit_button( null, 'primary', 'save-background-options' ); ?>
341 * Handle an Image upload for the background image.
345 function handle_upload() {
347 if ( empty($_FILES) )
350 check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
351 $overrides = array('test_form' => false);
353 $uploaded_file = $_FILES['import'];
354 $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'], false );
355 if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
356 wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
358 $file = wp_handle_upload($uploaded_file, $overrides);
360 if ( isset($file['error']) )
361 wp_die( $file['error'] );
364 $type = $file['type'];
365 $file = $file['file'];
366 $filename = basename($file);
368 // Construct the object array
370 'post_title' => $filename,
371 'post_content' => $url,
372 'post_mime_type' => $type,
374 'context' => 'custom-background'
378 $id = wp_insert_attachment($object, $file);
381 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
382 update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
384 set_theme_mod('background_image', esc_url_raw($url));
386 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
387 set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
389 do_action('wp_create_file_in_uploads', $file, $id); // For replication
390 $this->updated = true;
394 * Unused since 3.5.0.
398 function attachment_fields_to_edit( $form_fields ) {
403 * Unused since 3.5.0.
407 function filter_upload_tabs( $tabs ) {
411 public function wp_set_background_image() {
412 if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
413 $attachment_id = absint($_POST['attachment_id']);
414 $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
416 if ( in_array( $_POST['size'], $sizes ) )
417 $size = esc_attr( $_POST['size'] );
419 update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
420 $url = wp_get_attachment_image_src( $attachment_id, $size );
421 $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
422 set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
423 set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );