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;
59 * Set up the hooks for the Custom Background admin page.
64 if ( ! current_user_can('edit_theme_options') )
67 $this->page = $page = add_theme_page(__('Background'), __('Background'), 'edit_theme_options', 'custom-background', array(&$this, 'admin_page'));
69 add_action("load-$page", array(&$this, 'admin_load'));
70 add_action("load-$page", array(&$this, 'take_action'), 49);
71 add_action("load-$page", array(&$this, 'handle_upload'), 49);
73 if ( $this->admin_header_callback )
74 add_action("admin_head-$page", $this->admin_header_callback, 51);
78 * Set up the enqueue for the CSS & JavaScript files.
82 function admin_load() {
83 get_current_screen()->add_help_tab( array(
85 'title' => __('Overview'),
87 '<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>' .
88 '<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>' .
89 '<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>' .
90 '<p>' . __( 'Don’t forget to click on the Save Changes button when you are finished.' ) . '</p>'
93 get_current_screen()->set_help_sidebar(
94 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
95 '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Background_Screen" target="_blank">Documentation on Custom Background</a>' ) . '</p>' .
96 '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
99 wp_enqueue_script('custom-background');
100 wp_enqueue_style('farbtastic');
104 * Execute custom background modification.
108 function take_action() {
113 if ( isset($_POST['reset-background']) ) {
114 check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
115 remove_theme_mod('background_image');
116 remove_theme_mod('background_image_thumb');
117 $this->updated = true;
121 if ( isset($_POST['remove-background']) ) {
122 // @TODO: Uploaded files are not removed here.
123 check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
124 set_theme_mod('background_image', '');
125 set_theme_mod('background_image_thumb', '');
126 $this->updated = true;
130 if ( isset($_POST['background-repeat']) ) {
131 check_admin_referer('custom-background');
132 if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) )
133 $repeat = $_POST['background-repeat'];
136 set_theme_mod('background_repeat', $repeat);
139 if ( isset($_POST['background-position-x']) ) {
140 check_admin_referer('custom-background');
141 if ( in_array($_POST['background-position-x'], array('center', 'right', 'left')) )
142 $position = $_POST['background-position-x'];
145 set_theme_mod('background_position_x', $position);
148 if ( isset($_POST['background-attachment']) ) {
149 check_admin_referer('custom-background');
150 if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
151 $attachment = $_POST['background-attachment'];
153 $attachment = 'fixed';
154 set_theme_mod('background_attachment', $attachment);
157 if ( isset($_POST['background-color']) ) {
158 check_admin_referer('custom-background');
159 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
160 if ( strlen($color) == 6 || strlen($color) == 3 )
161 set_theme_mod('background_color', $color);
163 set_theme_mod('background_color', '');
166 $this->updated = true;
170 * Display the custom background page.
174 function admin_page() {
176 <div class="wrap" id="custom-background">
177 <?php screen_icon(); ?>
178 <h2><?php _e('Custom Background'); ?></h2>
179 <?php if ( !empty($this->updated) ) { ?>
180 <div id="message" class="updated">
181 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
185 if ( $this->admin_image_div_callback ) {
186 call_user_func($this->admin_image_div_callback);
189 <h3><?php _e('Background Image'); ?></h3>
190 <table class="form-table">
193 <th scope="row"><?php _e('Preview'); ?></th>
196 $background_styles = '';
197 if ( $bgcolor = get_background_color() )
198 $background_styles .= 'background-color: #' . $bgcolor . ';';
200 if ( get_background_image() ) {
201 // background-image URL must be single quote, see below
202 $background_styles .= ' background-image: url(\'' . get_theme_mod('background_image_thumb', '') . '\');'
203 . ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
204 . ' background-position: top ' . get_theme_mod('background_position_x', 'left');
207 <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
208 <?php if ( get_background_image() ) { ?>
209 <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" /><br />
210 <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" />
216 <?php if ( get_background_image() ) : ?>
218 <th scope="row"><?php _e('Remove Image'); ?></th>
220 <form method="post" action="">
221 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
222 <?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/>
223 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
229 <?php if ( defined( 'BACKGROUND_IMAGE' ) ) : // Show only if a default background image exists ?>
231 <th scope="row"><?php _e('Restore Original Image'); ?></th>
233 <form method="post" action="">
234 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
235 <?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/>
236 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
243 <th scope="row"><?php _e('Upload Image'); ?></th>
244 <td><form enctype="multipart/form-data" id="upload-form" method="post" action="">
245 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
246 <input type="hidden" name="action" value="save" />
247 <?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
248 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
255 <h3><?php _e('Display Options') ?></h3>
256 <form method="post" action="">
257 <table class="form-table">
259 <?php if ( get_background_image() ) : ?>
261 <th scope="row"><?php _e( 'Position' ); ?></th>
262 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
264 <input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
268 <input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
269 <?php _e('Center') ?>
272 <input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
279 <th scope="row"><?php _e( 'Repeat' ); ?></th>
280 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
281 <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>
282 <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label>
283 <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>
284 <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>
289 <th scope="row"><?php _e( 'Attachment' ); ?></th>
290 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
292 <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
293 <?php _e('Scroll') ?>
296 <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
301 <?php endif; // get_background_image() ?>
303 <th scope="row"><?php _e( 'Background Color' ); ?></th>
304 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
305 <?php $show_clear = get_background_color() ? '' : ' style="display:none"'; ?>
306 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" />
307 <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 _e( 'Clear' ); ?></a>)</span>
308 <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
314 <?php wp_nonce_field('custom-background'); ?>
315 <?php submit_button( null, 'primary', 'save-background-options' ); ?>
323 * Handle an Image upload for the background image.
327 function handle_upload() {
329 if ( empty($_FILES) )
332 check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
333 $overrides = array('test_form' => false);
334 $file = wp_handle_upload($_FILES['import'], $overrides);
336 if ( isset($file['error']) )
337 wp_die( $file['error'] );
340 $type = $file['type'];
341 $file = $file['file'];
342 $filename = basename($file);
344 // Construct the object array
346 'post_title' => $filename,
347 'post_content' => $url,
348 'post_mime_type' => $type,
350 'context' => 'custom-background'
354 $id = wp_insert_attachment($object, $file);
357 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
358 update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
360 set_theme_mod('background_image', esc_url($url));
362 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
363 set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) );
365 do_action('wp_create_file_in_uploads', $file, $id); // For replication
366 $this->updated = true;