root/theme-switcher/trunk/theme-switcher.php

Revision 4001, 3.7 kB (checked in by ryan, 3 years ago)

Exit after sending the redirect header.

Line 
1 <?php
2
3 /*
4 Plugin Name: Theme Switcher
5 Plugin URI: http://wordpress.org/
6 Description: Allow your readers to switch themes.
7 Version: 0.5
8 Author: Ryan Boren
9 Author URI: http://boren.nu/
10
11 Adapted from Alex King's style switcher.
12 http://www.alexking.org/software/wordpress/
13
14 To use, add the following to your sidebar menu:
15
16   <li>Themes:
17     <?php wp_theme_switcher(); ?>
18   </li>
19
20 This will create a list of themes for your readers to select.
21
22 If you would like a dropdown box rather than a list, add this:
23
24   <li>Themes:
25     <?php wp_theme_switcher('dropdown'); ?>
26   </li>
27
28 */
29
30 function ts_set_theme_cookie() {
31     $expire = time() + 30000000;
32     if (!empty($_GET["wptheme"])) {
33         setcookie("wptheme" . COOKIEHASH,
34                             stripslashes($_GET["wptheme"]),
35                             $expire,
36                             COOKIEPATH
37                             );
38
39         $redirect = get_settings('home').'/';
40
41         if (function_exists('wp_redirect'))
42             wp_redirect($redirect);
43         else
44             header("Location: ". $redirect);
45
46         exit;
47     }
48 }
49
50 function ts_get_theme() {
51     if (!empty($_COOKIE["wptheme" . COOKIEHASH])) {
52         return $_COOKIE["wptheme" . COOKIEHASH];
53     }    else {
54         return '';
55     }
56 }
57
58 function ts_get_template($template) {
59     $theme = ts_get_theme();
60
61     if (empty($theme)) {
62         return $template;
63     }
64
65     $theme = get_theme($theme);
66     
67     if (empty($theme)) {
68         return $template;
69     }
70
71     // Don't let people peek at unpublished themes.
72     if (isset($theme['Status']) && $theme['Status'] != 'publish')
73         return $template;       
74
75     return $theme['Template'];
76 }
77
78 function ts_get_stylesheet($stylesheet) {
79     $theme = ts_get_theme();
80
81     if (empty($theme)) {
82         return $stylesheet;
83     }
84
85     $theme = get_theme($theme);
86
87     // Don't let people peek at unpublished themes.
88     if (isset($theme['Status']) && $theme['Status'] != 'publish')
89         return $template;       
90     
91     if (empty($theme)) {
92         return $stylesheet;
93     }
94
95     return $theme['Stylesheet'];
96 }
97
98 function wp_theme_switcher($style = "text") {
99     $themes = get_themes();
100
101     $default_theme = get_current_theme();
102
103     if (count($themes) > 1) {
104         $theme_names = array_keys($themes);
105         natcasesort($theme_names);
106
107         $ts = '<ul id="themeswitcher">'."\n";       
108
109         if ($style == 'dropdown') {
110             $ts .= '<li>'."\n"
111                 . '    <select name="themeswitcher" onchange="location.href=\''.get_settings('home').'/index.php?wptheme=\' + this.options[this.selectedIndex].value;">'."\n"    ;
112
113             foreach ($theme_names as $theme_name) {
114                 // Skip unpublished themes.
115                 if (isset($themes[$theme_name]['Status']) && $themes[$theme_name]['Status'] != 'publish')
116                     continue;
117                     
118                 if ((!empty($_COOKIE["wptheme" . COOKIEHASH]) && $_COOKIE["wptheme" . COOKIEHASH] == $theme_name)
119                         || (empty($_COOKIE["wptheme" . COOKIEHASH]) && ($theme_name == $default_theme))) {
120                     $ts .= '        <option value="'.$theme_name.'" selected="selected">'
121                         . htmlspecialchars($theme_name)
122                         . '</option>'."\n"
123                         ;
124                 }    else {
125                     $ts .= '        <option value="'.$theme_name.'">'
126                         . htmlspecialchars($theme_name)
127                         . '</option>'."\n"
128                         ;
129                 }               
130             }
131             $ts .= '    </select>'."\n"
132                 . '</li>'."\n"
133                 ;
134         }    else {
135             foreach ($theme_names as $theme_name) {
136                 // Skip unpublished themes.
137                 if (isset($themes[$theme_name]['Status']) && $themes[$theme_name]['Status'] != 'publish')
138                     continue;
139
140                 $display = htmlspecialchars($theme_name);
141                 
142                 if ((!empty($_COOKIE["wptheme" . COOKIEHASH]) && $_COOKIE["wptheme" . COOKIEHASH] == $theme_name)
143                         || (empty($_COOKIE["wptheme" . COOKIEHASH]) && ($theme_name == $default_theme))) {
144                     $ts .= '    <li>'.$display.'</li>'."\n";
145                 }    else {
146                     $ts .= '    <li><a href="'
147                         .get_settings('home').'/'. 'index.php'
148                         .'?wptheme='.urlencode($theme_name).'">'
149                         .$display.'</a></li>'."\n";
150                 }
151             }
152         }
153         $ts .= '</ul>';
154     }
155
156     echo $ts;
157 }
158
159 ts_set_theme_cookie();
160
161 add_filter('template', 'ts_get_template');
162 add_filter('stylesheet', 'ts_get_stylesheet');
163
164 ?>
165
Note: See TracBrowser for help on using the browser.