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.
24 public $admin_header_callback;
27 * Callback for header div.
32 public $admin_image_div_callback;
35 * Used to trigger a success message when settings updated and set to true.
44 * Constructor - Register administration header callback.
47 * @param callable $admin_header_callback
48 * @param callable $admin_image_div_callback Optional custom image div output callback.
50 public function __construct($admin_header_callback = '', $admin_image_div_callback = '') {
51 $this->admin_header_callback = $admin_header_callback;
52 $this->admin_image_div_callback = $admin_image_div_callback;
54 add_action( 'admin_menu', array( $this, 'init' ) );
56 add_action( 'wp_ajax_custom-background-add', array( $this, 'ajax_background_add' ) );
58 // Unused since 3.5.0.
59 add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
63 * Set up the hooks for the Custom Background admin page.
67 public function init() {
68 $page = add_theme_page( __( 'Background' ), __( 'Background' ), 'edit_theme_options', 'custom-background', array( $this, 'admin_page' ) );
73 add_action( "load-$page", array( $this, 'admin_load' ) );
74 add_action( "load-$page", array( $this, 'take_action' ), 49 );
75 add_action( "load-$page", array( $this, 'handle_upload' ), 49 );
77 if ( $this->admin_header_callback ) {
78 add_action( "admin_head-$page", $this->admin_header_callback, 51 );
83 * Set up the enqueue for the CSS & JavaScript files.
87 public function admin_load() {
88 get_current_screen()->add_help_tab( array(
90 'title' => __('Overview'),
92 '<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>' .
93 '<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>' .
94 '<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>' .
95 '<p>' . __( 'Don’t forget to click on the Save Changes button when you are finished.' ) . '</p>'
98 get_current_screen()->set_help_sidebar(
99 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
100 '<p>' . __( '<a href="https://codex.wordpress.org/Appearance_Background_Screen">Documentation on Custom Background</a>' ) . '</p>' .
101 '<p>' . __( '<a href="https://wordpress.org/support/">Support Forums</a>' ) . '</p>'
105 wp_enqueue_script('custom-background');
106 wp_enqueue_style('wp-color-picker');
110 * Execute custom background modification.
114 public function take_action() {
118 if ( isset($_POST['reset-background']) ) {
119 check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
120 remove_theme_mod('background_image');
121 remove_theme_mod('background_image_thumb');
122 $this->updated = true;
126 if ( isset($_POST['remove-background']) ) {
127 // @TODO: Uploaded files are not removed here.
128 check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
129 set_theme_mod('background_image', '');
130 set_theme_mod('background_image_thumb', '');
131 $this->updated = true;
132 wp_safe_redirect( $_POST['_wp_http_referer'] );
136 if ( isset( $_POST['background-preset'] ) ) {
137 check_admin_referer( 'custom-background' );
139 if ( in_array( $_POST['background-preset'], array( 'default', 'fill', 'fit', 'repeat', 'custom' ), true ) ) {
140 $preset = $_POST['background-preset'];
145 set_theme_mod( 'background_preset', $preset );
148 if ( isset( $_POST['background-position'] ) ) {
149 check_admin_referer( 'custom-background' );
151 $position = explode( ' ', $_POST['background-position'] );
153 if ( in_array( $position[0], array( 'left', 'center', 'right' ), true ) ) {
154 $position_x = $position[0];
156 $position_x = 'left';
159 if ( in_array( $position[1], array( 'top', 'center', 'bottom' ), true ) ) {
160 $position_y = $position[1];
165 set_theme_mod( 'background_position_x', $position_x );
166 set_theme_mod( 'background_position_y', $position_y );
169 if ( isset( $_POST['background-size'] ) ) {
170 check_admin_referer( 'custom-background' );
172 if ( in_array( $_POST['background-size'], array( 'auto', 'contain', 'cover' ), true ) ) {
173 $size = $_POST['background-size'];
178 set_theme_mod( 'background_size', $size );
181 if ( isset( $_POST['background-repeat'] ) ) {
182 check_admin_referer( 'custom-background' );
184 $repeat = $_POST['background-repeat'];
186 if ( 'no-repeat' !== $repeat ) {
190 set_theme_mod( 'background_repeat', $repeat );
193 if ( isset( $_POST['background-attachment'] ) ) {
194 check_admin_referer( 'custom-background' );
196 $attachment = $_POST['background-attachment'];
198 if ( 'fixed' !== $attachment ) {
199 $attachment = 'scroll';
202 set_theme_mod( 'background_attachment', $attachment );
205 if ( isset($_POST['background-color']) ) {
206 check_admin_referer('custom-background');
207 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
208 if ( strlen($color) == 6 || strlen($color) == 3 )
209 set_theme_mod('background_color', $color);
211 set_theme_mod('background_color', '');
214 $this->updated = true;
218 * Display the custom background page.
222 public function admin_page() {
224 <div class="wrap" id="custom-background">
225 <h1><?php _e( 'Custom Background' ); ?></h1>
227 <?php if ( current_user_can( 'customize' ) ) { ?>
228 <div class="notice notice-info hide-if-no-customize">
232 __( 'You can now manage and live-preview Custom Backgrounds in the <a href="%1$s">Customizer</a>.' ),
233 admin_url( 'customize.php?autofocus[control]=background_image' )
240 <?php if ( ! empty( $this->updated ) ) { ?>
241 <div id="message" class="updated">
242 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
246 <h3><?php _e( 'Background Image' ); ?></h3>
248 <table class="form-table">
251 <th scope="row"><?php _e( 'Preview' ); ?></th>
254 if ( $this->admin_image_div_callback ) {
255 call_user_func( $this->admin_image_div_callback );
257 $background_styles = '';
258 if ( $bgcolor = get_background_color() )
259 $background_styles .= 'background-color: #' . $bgcolor . ';';
261 $background_image_thumb = get_background_image();
262 if ( $background_image_thumb ) {
263 $background_image_thumb = esc_url( set_url_scheme( get_theme_mod( 'background_image_thumb', str_replace( '%', '%%', $background_image_thumb ) ) ) );
264 $background_position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
265 $background_position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) );
266 $background_size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) );
267 $background_repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
268 $background_attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
270 // Background-image URL must be single quote, see below.
271 $background_styles .= " background-image: url('$background_image_thumb');"
272 . " background-size: $background_size;"
273 . " background-position: $background_position_x $background_position_y;"
274 . " background-repeat: $background_repeat;"
275 . " background-attachment: $background_attachment;";
278 <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
279 <?php if ( $background_image_thumb ) { ?>
280 <img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" /><br />
281 <img class="custom-background-image" src="<?php echo $background_image_thumb; ?>" style="visibility:hidden;" alt="" />
288 <?php if ( get_background_image() ) : ?>
290 <th scope="row"><?php _e('Remove Image'); ?></th>
293 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
294 <?php submit_button( __( 'Remove Background Image' ), '', 'remove-background', false ); ?><br/>
295 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
301 <?php $default_image = get_theme_support( 'custom-background', 'default-image' ); ?>
302 <?php if ( $default_image && get_background_image() != $default_image ) : ?>
304 <th scope="row"><?php _e('Restore Original Image'); ?></th>
307 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
308 <?php submit_button( __( 'Restore Original Image' ), '', 'reset-background', false ); ?><br/>
309 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
315 <?php if ( current_user_can( 'upload_files' ) ): ?>
317 <th scope="row"><?php _e('Select Image'); ?></th>
318 <td><form enctype="multipart/form-data" id="upload-form" class="wp-upload-form" method="post">
320 <label for="upload"><?php _e( 'Choose an image from your computer:' ); ?></label><br />
321 <input type="file" id="upload" name="import" />
322 <input type="hidden" name="action" value="save" />
323 <?php wp_nonce_field( 'custom-background-upload', '_wpnonce-custom-background-upload' ); ?>
324 <?php submit_button( __( 'Upload' ), '', 'submit', false ); ?>
327 <label for="choose-from-library-link"><?php _e( 'Or choose an image from your media library:' ); ?></label><br />
328 <button id="choose-from-library-link" class="button"
329 data-choose="<?php esc_attr_e( 'Choose a Background Image' ); ?>"
330 data-update="<?php esc_attr_e( 'Set as background' ); ?>"><?php _e( 'Choose Image' ); ?></button>
339 <h3><?php _e( 'Display Options' ); ?></h3>
341 <table class="form-table">
343 <?php if ( get_background_image() ) : ?>
344 <input name="background-preset" type="hidden" value="custom">
347 $background_position = sprintf(
349 get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) ),
350 get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) )
353 $background_position_options = array(
355 'left top' => array( 'label' => __( 'Top Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
356 'center top' => array( 'label' => __( 'Top' ), 'icon' => 'dashicons dashicons-arrow-up-alt' ),
357 'right top' => array( 'label' => __( 'Top Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
360 'left center' => array( 'label' => __( 'Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
361 'center center' => array( 'label' => __( 'Center' ), 'icon' => 'background-position-center-icon' ),
362 'right center' => array( 'label' => __( 'Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
365 'left bottom' => array( 'label' => __( 'Bottom Left' ), 'icon' => 'dashicons dashicons-arrow-left-alt' ),
366 'center bottom' => array( 'label' => __( 'Bottom' ), 'icon' => 'dashicons dashicons-arrow-down-alt' ),
367 'right bottom' => array( 'label' => __( 'Bottom Right' ), 'icon' => 'dashicons dashicons-arrow-right-alt' ),
372 <th scope="row"><?php _e( 'Image Position' ); ?></th>
373 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Position' ); ?></span></legend>
374 <div class="background-position-control">
375 <?php foreach ( $background_position_options as $group ) : ?>
376 <div class="button-group">
377 <?php foreach ( $group as $value => $input ) : ?>
379 <input class="screen-reader-text" name="background-position" type="radio" value="<?php echo esc_attr( $value ); ?>"<?php checked( $value, $background_position ); ?>>
380 <span class="button display-options position"><span class="<?php echo esc_attr( $input['icon'] ); ?>" aria-hidden="true"></span></span>
381 <span class="screen-reader-text"><?php echo $input['label']; ?></span>
391 <th scope="row"><label for="background-size"><?php _e( 'Image Size' ); ?></label></th>
392 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Image Size' ); ?></span></legend>
393 <select id="background-size" name="background-size">
394 <option value="auto"<?php selected( 'auto', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _ex( 'Original', 'Original Size' ); ?></option>
395 <option value="contain"<?php selected( 'contain', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fit to Screen' ); ?></option>
396 <option value="cover"<?php selected( 'cover', get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ) ); ?>><?php _e( 'Fill Screen' ); ?></option>
402 <th scope="row"><?php _ex( 'Repeat', 'Background Repeat' ); ?></th>
403 <td><fieldset><legend class="screen-reader-text"><span><?php _ex( 'Repeat', 'Background Repeat' ); ?></span></legend>
404 <input name="background-repeat" type="hidden" value="no-repeat">
405 <label><input type="checkbox" name="background-repeat" value="repeat"<?php checked( 'repeat', get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) ) ); ?>> <?php _e( 'Repeat Background Image' ); ?></label>
410 <th scope="row"><?php _ex( 'Scroll', 'Background Scroll' ); ?></th>
411 <td><fieldset><legend class="screen-reader-text"><span><?php _ex( 'Scroll', 'Background Scroll' ); ?></span></legend>
412 <input name="background-attachment" type="hidden" value="fixed">
413 <label><input name="background-attachment" type="checkbox" value="scroll" <?php checked( 'scroll', get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) ) ); ?>> <?php _e( 'Scroll with Page' ); ?></label>
416 <?php endif; // get_background_image() ?>
418 <th scope="row"><?php _e( 'Background Color' ); ?></th>
419 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
422 if ( current_theme_supports( 'custom-background', 'default-color' ) )
423 $default_color = ' data-default-color="#' . esc_attr( get_theme_support( 'custom-background', 'default-color' ) ) . '"';
425 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr( get_background_color() ); ?>"<?php echo $default_color ?>>
431 <?php wp_nonce_field('custom-background'); ?>
432 <?php submit_button( null, 'primary', 'save-background-options' ); ?>
440 * Handle an Image upload for the background image.
444 public function handle_upload() {
445 if ( empty($_FILES) )
448 check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
449 $overrides = array('test_form' => false);
451 $uploaded_file = $_FILES['import'];
452 $wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
453 if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
454 wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
456 $file = wp_handle_upload($uploaded_file, $overrides);
458 if ( isset($file['error']) )
459 wp_die( $file['error'] );
462 $type = $file['type'];
463 $file = $file['file'];
464 $filename = basename($file);
466 // Construct the object array
468 'post_title' => $filename,
469 'post_content' => $url,
470 'post_mime_type' => $type,
472 'context' => 'custom-background'
476 $id = wp_insert_attachment($object, $file);
479 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
480 update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
482 set_theme_mod('background_image', esc_url_raw($url));
484 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
485 set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
487 /** This action is documented in wp-admin/custom-header.php */
488 do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
489 $this->updated = true;
493 * Ajax handler for adding custom background context to an attachment.
495 * Triggered when the user adds a new background image from the
500 public function ajax_background_add() {
501 check_ajax_referer( 'background-add', 'nonce' );
503 if ( ! current_user_can( 'edit_theme_options' ) ) {
504 wp_send_json_error();
507 $attachment_id = absint( $_POST['attachment_id'] );
508 if ( $attachment_id < 1 ) {
509 wp_send_json_error();
512 update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_stylesheet() );
514 wp_send_json_success();
522 * @param array $form_fields
523 * @return array $form_fields
525 public function attachment_fields_to_edit( $form_fields ) {
535 * @return array $tabs
537 public function filter_upload_tabs( $tabs ) {
546 public function wp_set_background_image() {
547 if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
548 $attachment_id = absint($_POST['attachment_id']);
549 /** This filter is documented in wp-admin/includes/media.php */
550 $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
552 if ( in_array( $_POST['size'], $sizes ) )
553 $size = esc_attr( $_POST['size'] );
555 update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
556 $url = wp_get_attachment_image_src( $attachment_id, $size );
557 $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
558 set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
559 set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );