Created
July 6, 2012 12:51
-
-
Save codehooligans/3060004 to your computer and use it in GitHub Desktop.
theme_switcher_markup function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function theme_switcher_markup( $style = 'text', $instance = array() ) { | |
if ( ! $theme_data = wp_cache_get('themes-data', 'advanced-theme-switcher') ) { | |
$themes = (array) get_themes(); | |
if (!$themes) return; | |
if ( is_multisite() ) { | |
//$allowed_themes = (array) get_site_option( 'allowedthemes' ); | |
$allowed_themes = WP_Theme::get_allowed(); | |
foreach( $themes as $name => $theme ) { | |
if ( !isset( $allowed_themes[ esc_html( $theme['Stylesheet'] ) ] ) ) { | |
unset( $themes[$name] ); | |
} | |
} | |
} | |
$theme_names = array_keys( (array) $themes ); | |
foreach ( $theme_names as $theme_name ) { | |
/* Skip unpublished themes. */ | |
if ( empty( $theme_name ) || isset( $themes[$theme_name]['Status'] ) && $themes[$theme_name]['Status'] != 'publish' ) | |
continue; | |
$theme_data[$theme_name] = add_query_arg( 'theme-preview', urlencode( $theme_name ), get_option('home') ); | |
} | |
wp_cache_set('themes-data', $theme_data, 'advanced-theme-switcher'); | |
} | |
ksort( $theme_data ); | |
$ts = '<ul class="advanced-theme-switcher-container">'."\n"; | |
if ( $style == 'dropdown' ) | |
$ts .= '<li>'."\n\t" . '<select class="advanced-theme-switcher-themes" onchange="location.href=this.options[this.selectedIndex].value;">'."\n"; | |
$default_theme = get_current_theme(); | |
$current_theme = $this->get_current_theme_name(); | |
foreach ( $theme_data as $theme_name => $url ) { | |
if ( !empty( $current_theme ) && $current_theme == $theme_name || empty( $current_theme ) && ( $theme_name == $default_theme ) ) { | |
if ($default_theme == $theme_name) | |
$pattern = ( 'dropdown' == $style ) ? '<option value="%1$s" selected="selected">%2$s *</option>' : '<li>%2$s *</li>'; | |
else | |
$pattern = ( 'dropdown' == $style ) ? '<option value="%1$s" selected="selected">%2$s</option>' : '<li>%2$s</li>'; | |
} else if ($default_theme == $theme_name) { | |
$pattern = ( 'dropdown' == $style ) ? '<option value="%1$s">%2$s *</option>' : '<li><a href="%1$s">%2$s</a> *</li>'; | |
} else { | |
$pattern = ( 'dropdown' == $style ) ? '<option value="%1$s">%2$s</option>' : '<li><a href="%1$s">%2$s</a></li>'; | |
} | |
$ts .= sprintf( $pattern, esc_attr( $url ), esc_html( $theme_name ) ); | |
} | |
if ( 'dropdown' == $style ) { | |
$ts .= '</select>' . "\n" . '</li>' . "\n"; | |
} | |
$ts .= '</ul>'; | |
//$ts .= "<p>* = ". __('Current theme', 'advanced-theme-switcher') ."</p>"; | |
return $ts; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment