Class for registering multiple default backgrounds in WrodPress themes.

  • Download Gist
customize-control-background-image.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<?php
/**
* Extends the WordPress background image customize control class, which allows a theme to register
* multiple default backgrounds for the user to choose from. To use this, the theme author
* should remove the 'background_image' control and add this control in its place.
*
* @since 0.1.0
* @author Justin Tadlock <justin@justintadlock.com>
* @copyright Copyright (c) 2013, Justin Tadlock
* @link http://justintadlock.com/archives/2013/10/13/registering-multiple-default-backgrounds
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
 
class JT_Customize_Control_Background_Image extends WP_Customize_Background_Image_Control {
 
/**
* Array of default backgrounds.
*
* @since 0.1.0
* @access public
* @var array
*/
public $jt_default_backgrounds = array();
 
/**
* Set up our control.
*
* @since 0.1.0
* @access public
* @param object $manager
* @return void
*/
public function __construct( $manager ) {
 
/* Let WP handle this. */
parent::__construct( $manager );
 
/* Allow themes to register custom backgrounds. */
$this->jt_default_backgrounds = apply_filters( 'jt_default_backgrounds', $this->jt_default_backgrounds );
 
/* WordPress will only output the 'default' tab if there's a default image. Make sure it gets added. */
if ( !$this->setting->default && !empty( $this->jt_default_backgrounds ) )
$this->add_tab( 'default', __( 'Default', 'jt' ), array( $this, 'tab_default_background' ) );
}
 
/**
* Displays the 'default' tab for selecting a background image. This method plays nicely with the
* 'default-image' argument for 'custom-background' as well as our custom backgrounds.
*
* @since 0.1.0
* @access public
* @return void
*/
public function tab_default_background() {
 
/* If the theme added a 'default-image', make sure to output it. */
if ( $this->setting->default )
$this->print_tab_image( $this->setting->default );
 
/* Check if the theme added an array of default backgrounds. */
if ( !empty( $this->jt_default_backgrounds ) ) {
 
/* Get the template and stylesheet directory URIs. */
$template = get_template_directory_uri();
$stylesheet = get_stylesheet_directory_uri();
 
/* Loop through the backgrounds and print them. */
foreach ( $this->jt_default_backgrounds as $background ) {
 
/* If no thumbnail was given, use the original. */
if ( !isset( $background['thumbnail_url'] ) )
$background['thumbnail_url'] = $background['url'];
 
/* Use '%s' for parent themes and '%2$s' for child themes. */
$url = sprintf( $background['url'], $template, $stylesheet );
$thumb_url = sprintf( $background['thumbnail_url'], $template, $stylesheet );
 
/* Print the image. */
$this->print_tab_image( $url, $thumb_url );
}
}
}
}
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.