Skip to content

Instantly share code, notes, and snippets.

@justintadlock
Created October 14, 2013 01:23
Show Gist options
  • Save justintadlock/6969375 to your computer and use it in GitHub Desktop.
Save justintadlock/6969375 to your computer and use it in GitHub Desktop.
Class for registering multiple default backgrounds in WrodPress themes.
<?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 );
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment