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 add_contextual_help( $this->page, '<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>' .
84 '<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>' .
85 '<p>' . __( 'You can also choose a background color. If you know the hexadecimal code for the color you want, enter it in the 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>' .
86 '<p>' . __( 'Don’t forget to click on the Save Changes button when you are finished.' ) . '</p>' .
87 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
88 '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Background_Screen" target="_blank">Documentation on Custom Background</a>' ) . '</p>' .
89 '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>' );
90 wp_enqueue_script('custom-background');
91 wp_enqueue_style('farbtastic');
95 * Execute custom background modification.
99 function take_action() {
104 if ( isset($_POST['reset-background']) ) {
105 check_admin_referer('custom-background-reset', '_wpnonce-custom-background-reset');
106 remove_theme_mod('background_image');
107 remove_theme_mod('background_image_thumb');
108 $this->updated = true;
112 if ( isset($_POST['remove-background']) ) {
113 // @TODO: Uploaded files are not removed here.
114 check_admin_referer('custom-background-remove', '_wpnonce-custom-background-remove');
115 set_theme_mod('background_image', '');
116 set_theme_mod('background_image_thumb', '');
117 $this->updated = true;
121 if ( isset($_POST['background-repeat']) ) {
122 check_admin_referer('custom-background');
123 if ( in_array($_POST['background-repeat'], array('repeat', 'no-repeat', 'repeat-x', 'repeat-y')) )
124 $repeat = $_POST['background-repeat'];
127 set_theme_mod('background_repeat', $repeat);
130 if ( isset($_POST['background-position-x']) ) {
131 check_admin_referer('custom-background');
132 if ( in_array($_POST['background-position-x'], array('center', 'right', 'left')) )
133 $position = $_POST['background-position-x'];
136 set_theme_mod('background_position_x', $position);
139 if ( isset($_POST['background-attachment']) ) {
140 check_admin_referer('custom-background');
141 if ( in_array($_POST['background-attachment'], array('fixed', 'scroll')) )
142 $attachment = $_POST['background-attachment'];
144 $attachment = 'fixed';
145 set_theme_mod('background_attachment', $attachment);
148 if ( isset($_POST['background-color']) ) {
149 check_admin_referer('custom-background');
150 $color = preg_replace('/[^0-9a-fA-F]/', '', $_POST['background-color']);
151 if ( strlen($color) == 6 || strlen($color) == 3 )
152 set_theme_mod('background_color', $color);
154 set_theme_mod('background_color', '');
157 $this->updated = true;
161 * Display the custom background page.
165 function admin_page() {
167 <div class="wrap" id="custom-background">
168 <?php screen_icon(); ?>
169 <h2><?php _e('Custom Background'); ?></h2>
170 <?php if ( !empty($this->updated) ) { ?>
171 <div id="message" class="updated">
172 <p><?php printf( __( 'Background updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
176 if ( $this->admin_image_div_callback ) {
177 call_user_func($this->admin_image_div_callback);
180 <h3><?php _e('Background Image'); ?></h3>
181 <table class="form-table">
184 <th scope="row"><?php _e('Preview'); ?></th>
187 $background_styles = '';
188 if ( $bgcolor = get_background_color() )
189 $background_styles .= 'background-color: #' . $bgcolor . ';';
191 if ( get_background_image() ) {
192 // background-image URL must be single quote, see below
193 $background_styles .= ' background-image: url(\'' . get_theme_mod('background_image_thumb', '') . '\');'
194 . ' background-repeat: ' . get_theme_mod('background_repeat', 'repeat') . ';'
195 . ' background-position: top ' . get_theme_mod('background_position_x', 'left');
198 <div id="custom-background-image" style="<?php echo $background_styles; ?>"><?php // must be double quote, see above ?>
199 <?php if ( get_background_image() ) { ?>
200 <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" /><br />
201 <img class="custom-background-image" src="<?php echo get_theme_mod('background_image_thumb', ''); ?>" style="visibility:hidden;" alt="" />
207 <?php if ( get_background_image() ) : ?>
209 <th scope="row"><?php _e('Remove Image'); ?></th>
211 <form method="post" action="">
212 <?php wp_nonce_field('custom-background-remove', '_wpnonce-custom-background-remove'); ?>
213 <?php submit_button( __( 'Remove Background Image' ), 'button', 'remove-background', false ); ?><br/>
214 <?php _e('This will remove the background image. You will not be able to restore any customizations.') ?>
220 <?php if ( defined( 'BACKGROUND_IMAGE' ) ) : // Show only if a default background image exists ?>
222 <th scope="row"><?php _e('Restore Original Image'); ?></th>
224 <form method="post" action="">
225 <?php wp_nonce_field('custom-background-reset', '_wpnonce-custom-background-reset'); ?>
226 <?php submit_button( __( 'Restore Original Image' ), 'button', 'reset-background', false ); ?><br/>
227 <?php _e('This will restore the original background image. You will not be able to restore any customizations.') ?>
234 <th scope="row"><?php _e('Upload Image'); ?></th>
235 <td><form enctype="multipart/form-data" id="upload-form" method="post" action="">
236 <label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
237 <input type="hidden" name="action" value="save" />
238 <?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
239 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
246 <h3><?php _e('Display Options') ?></h3>
247 <form method="post" action="">
248 <table class="form-table">
250 <?php if ( get_background_image() ) : ?>
252 <th scope="row"><?php _e( 'Position' ); ?></th>
253 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Position' ); ?></span></legend>
255 <input name="background-position-x" type="radio" value="left"<?php checked('left', get_theme_mod('background_position_x', 'left')); ?> />
259 <input name="background-position-x" type="radio" value="center"<?php checked('center', get_theme_mod('background_position_x', 'left')); ?> />
260 <?php _e('Center') ?>
263 <input name="background-position-x" type="radio" value="right"<?php checked('right', get_theme_mod('background_position_x', 'left')); ?> />
270 <th scope="row"><?php _e( 'Repeat' ); ?></th>
271 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Repeat' ); ?></span></legend>
272 <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>
273 <label><input type="radio" name="background-repeat" value="repeat"<?php checked('repeat', get_theme_mod('background_repeat', 'repeat')); ?> /> <?php _e('Tile'); ?></label>
274 <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>
275 <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>
280 <th scope="row"><?php _e( 'Attachment' ); ?></th>
281 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Attachment' ); ?></span></legend>
283 <input name="background-attachment" type="radio" value="scroll" <?php checked('scroll', get_theme_mod('background_attachment', 'scroll')); ?> />
284 <?php _e('Scroll') ?>
287 <input name="background-attachment" type="radio" value="fixed" <?php checked('fixed', get_theme_mod('background_attachment', 'scroll')); ?> />
292 <?php endif; // get_background_image() ?>
294 <th scope="row"><?php _e( 'Color' ); ?></th>
295 <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'Background Color' ); ?></span></legend>
296 <?php $show_clear = get_background_color() ? '' : ' style="display:none"'; ?>
297 <input type="text" name="background-color" id="background-color" value="#<?php echo esc_attr(get_background_color()) ?>" />
298 <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>
299 <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
305 <?php wp_nonce_field('custom-background'); ?>
306 <?php submit_button( null, 'primary', 'save-background-options' ); ?>
314 * Handle an Image upload for the background image.
318 function handle_upload() {
320 if ( empty($_FILES) )
323 check_admin_referer('custom-background-upload', '_wpnonce-custom-background-upload');
324 $overrides = array('test_form' => false);
325 $file = wp_handle_upload($_FILES['import'], $overrides);
327 if ( isset($file['error']) )
328 wp_die( $file['error'] );
331 $type = $file['type'];
332 $file = $file['file'];
333 $filename = basename($file);
335 // Construct the object array
337 'post_title' => $filename,
338 'post_content' => $url,
339 'post_mime_type' => $type,
341 'context' => 'custom-background'
345 $id = wp_insert_attachment($object, $file);
348 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
349 update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
351 set_theme_mod('background_image', esc_url($url));
353 $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
354 set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) );
356 do_action('wp_create_file_in_uploads', $file, $id); // For replication
357 $this->updated = true;