Multiple Themes - Version 3.0

Version Description

  • Add Support for Categories and Archives when no Permalinks exist (support already existed Categories and Archives with Permalinks)
  • Resolve several minor bugs
Download this release

Release Info

Developer dgewirtz
Plugin Icon 128x128 Multiple Themes
Version 3.0
Comparing to
See all releases

Version 3.0

includes/admin.php ADDED
@@ -0,0 +1,350 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // Admin Page
3
+
4
+ add_action( 'admin_menu', 'jr_mt_admin_hook' );
5
+
6
+ /**
7
+ * Add Admin Menu item for plugin
8
+ *
9
+ * Plugin needs its own Page in the Settings section of the Admin menu.
10
+ *
11
+ */
12
+ function jr_mt_admin_hook() {
13
+ // Add Settings Page for this Plugin
14
+ add_theme_page( 'jonradio Multiple Themes', 'Multiple Themes plugin', 'manage_options', 'jr_mt_settings', 'jr_mt_settings_page' );
15
+ add_options_page( 'jonradio Multiple Themes', 'Multiple Themes plugin', 'manage_options', 'jr_mt_settings', 'jr_mt_settings_page' );
16
+ }
17
+
18
+ /**
19
+ * Settings page for plugin
20
+ *
21
+ * Display and Process Settings page for this plugin.
22
+ *
23
+ */
24
+ function jr_mt_settings_page() {
25
+ echo '<div class="wrap">';
26
+ screen_icon( 'plugins' );
27
+ echo '<h2>jonradio Multiple Themes</h2>';
28
+
29
+ // Required because it is only called automatically for Admin Pages in the Settings section
30
+ settings_errors( 'jr_mt_settings' );
31
+
32
+ $theme = wp_get_theme()->Name;
33
+ global $jr_mt_options_cache;
34
+ if ( $jr_mt_options_cache['template'] == $jr_mt_options_cache['stylesheet'] ) {
35
+ ?>
36
+ <p>This plugin allows you to selectively change the Theme you have selected as your <b>Current Theme</b> in <b>Appearance-Themes</b> on the Admin panels.
37
+ You can choose from any of the <b>Available Themes</b> listed on the Appearance-Themes Admin panel for:
38
+ <ul>
39
+ <li> &raquo; All Pages</li>
40
+ <li> &raquo; All Posts</li>
41
+ <li> &raquo; The Site Home</li>
42
+ <li> &raquo; A Specific Page</li>
43
+ <li> &raquo; A Specific Post</li>
44
+ <li> &raquo; Any other non-Admin page that has its own Permalink; for example, a specific Archive or Category page</li>
45
+ </ul>
46
+ <?php
47
+ if ( function_exists('is_multisite') && is_multisite() ) {
48
+ echo "In a WordPress Network (AKA Multisite), Themes must be <b>Network Enabled</b> before they will appear as Available Themes on individual sites' Appearance-Themes panel.";
49
+ }
50
+ echo '</p>';
51
+ echo '<p>';
52
+ echo "The Current Theme is <b>$theme</b>. You will not normally need to specify it in any of the Settings on this page. The only exception would be if you specify a different Theme for All Pages or All Posts and wish to use the Current Theme for a specific Page, Post or other non-Admin page.";
53
+ echo '</p>';
54
+ echo '<form action="options.php" method="POST">';
55
+
56
+ // Plugin Settings are displayed and entered here:
57
+ settings_fields( 'jr_mt_settings' );
58
+ do_settings_sections( 'jr_mt_settings_page' );
59
+ echo '<p><input name="save" type="submit" value="Save Changes" class="button-primary" /></p></form>';
60
+ } else {
61
+ echo '<p>Please report this problem to the Plugin Author:<br />';
62
+ echo "Stylesheet and Template names do not match for Theme $theme: " . $jr_mt_options_cache['stylesheet'] . ' v.s. ' . $jr_mt_options_cache['template'];
63
+ global $jr_mt_plugin_data;
64
+ echo '</p><p><a href="' . $jr_mt_plugin_data['AuthorURI'] . '"' . ">Click here</a> to get to The Plugin Author's page where you can click Contact Us in the menu bar.</p>";
65
+ }
66
+ }
67
+
68
+ add_action( 'admin_init', 'jr_mt_admin_init' );
69
+
70
+ /**
71
+ * Register and define the settings
72
+ *
73
+ * Everything to be stored and/or can be set by the user
74
+ *
75
+ */
76
+ function jr_mt_admin_init() {
77
+ register_setting( 'jr_mt_settings', 'jr_mt_settings', 'jr_mt_validate_settings' );
78
+ add_settings_section( 'jr_mt_all_settings_section',
79
+ 'For All Pages, All Posts and/or Site Home',
80
+ 'jr_mt_all_settings_expl',
81
+ 'jr_mt_settings_page'
82
+ );
83
+ $suffix = array(
84
+ 'Pages' => '<br />(Pages created with Add Page)',
85
+ 'Posts' => ''
86
+ );
87
+ foreach ( array( 'Pages', 'Posts' ) as $thing ) {
88
+ add_settings_field( 'all_' . strtolower( $thing ), "Select Theme for All $thing" . $suffix[$thing], 'jr_mt_echo_all_things', 'jr_mt_settings_page', 'jr_mt_all_settings_section',
89
+ array( 'thing' => $thing ) );
90
+ }
91
+ add_settings_field( 'site_home',
92
+ 'Select Theme for Site Home<br />(' . get_home_url() . ')',
93
+ 'jr_mt_echo_site_home',
94
+ 'jr_mt_settings_page',
95
+ 'jr_mt_all_settings_section'
96
+ );
97
+ $settings = get_option( 'jr_mt_settings' );
98
+ $ids = $settings['ids'];
99
+ if ( !empty( $ids) ) {
100
+ add_settings_section( 'jr_mt_delete_settings_section',
101
+ 'To Display or Delete Theme Selections for Individual Pages or Posts',
102
+ 'jr_mt_delete_settings_expl',
103
+ 'jr_mt_settings_page'
104
+ );
105
+ add_settings_field( 'del_entry', 'Entries:', 'jr_mt_echo_delete_entry', 'jr_mt_settings_page', 'jr_mt_delete_settings_section' );
106
+ }
107
+ add_settings_section( 'jr_mt_single_settings_section',
108
+ 'For An Individual Page, Post or other non-Admin page',
109
+ 'jr_mt_single_settings_expl',
110
+ 'jr_mt_settings_page'
111
+ );
112
+ add_settings_field( 'add_theme', 'Theme', 'jr_mt_echo_add_theme', 'jr_mt_settings_page', 'jr_mt_single_settings_section' );
113
+ add_settings_field( 'add_path_id', 'URL of Page or Post', 'jr_mt_echo_add_path_id', 'jr_mt_settings_page', 'jr_mt_single_settings_section' );
114
+ }
115
+
116
+ /**
117
+ * Section text for Section1
118
+ *
119
+ * Display an explanation of this Section
120
+ *
121
+ */
122
+ function jr_mt_all_settings_expl() {
123
+ ?>
124
+ <p>
125
+ In this section, you can select a different Theme for All Pages, All Posts and/or Site Home.
126
+ To remove a previously selected Theme, select the blank entry from the drop-down list.
127
+ </p>
128
+ <p>
129
+ In the <i>next</i> section, you will be able to select a Theme, including the Current Theme, to override any choice you make here, for individual Pages, Posts or
130
+ any other non-Admin pages that have their own Permalink; for example, specific Archive or Category pages.
131
+ </p>
132
+ <?php
133
+ }
134
+
135
+ function jr_mt_echo_all_things( $thing ) {
136
+ $settings = get_option( 'jr_mt_settings' );
137
+ $field = 'all_' . strtolower( $thing['thing'] );
138
+ jr_mt_themes_field( $field, $settings[$field], 'jr_mt_settings', TRUE );
139
+ }
140
+
141
+ function jr_mt_echo_site_home() {
142
+ $settings = get_option( 'jr_mt_settings' );
143
+ jr_mt_themes_field( 'site_home', $settings['site_home'], 'jr_mt_settings', FALSE );
144
+ }
145
+
146
+ /**
147
+ * Section text for Section2
148
+ *
149
+ * Display an explanation of this Section
150
+ *
151
+ */
152
+ function jr_mt_delete_settings_expl() {
153
+ ?>
154
+ <p>
155
+ In this section, all entries are displayed for Themes selected for individual Pages, Posts
156
+ and any other non-Admin pages that have their own Permalink; for example, specific Archive or Category pages.
157
+ </p>
158
+ <p>
159
+ You can delete any of these entries by filling in the check box beside each one.
160
+ To change the Theme for an entry, add the same entry with a different Theme in the section below this one.</p>
161
+ <?php
162
+ }
163
+
164
+ function jr_mt_echo_delete_entry() {
165
+ $entry_num = 0;
166
+ $settings = get_option( 'jr_mt_settings' );
167
+ foreach ( $settings['ids'] as $path_id => $opt_array ) {
168
+ ++$entry_num;
169
+ echo "Delete <input type='checkbox' id='del_entry' name='jr_mt_settings[del_entry][]' value='$path_id' /> &nbsp; Theme="
170
+ . wp_get_theme( $opt_array['theme'] )->Name . '; ';
171
+ if ( $path_id == '' ) {
172
+ echo 'Site=<a href="' . get_home_url() . '" target="_blank">Home</a>';
173
+ } else {
174
+ if ( $opt_array['type'] == 'cat' ) {
175
+ echo 'Category=<a href="' . get_home_url() . '/?cat=' . $opt_array['id'] . '" target="_blank">' . get_cat_name( $opt_array['id'] ) . '</a>';
176
+ } else {
177
+ if ( $opt_array['type'] == 'archive' ) {
178
+ echo 'Archive=<a href="' . get_home_url() . '/?m=' . $opt_array['id'] . '" target="_blank">' . $opt_array['id'] . '</a>';
179
+ } else {
180
+ $p_array = get_posts( array( 'post_type' => 'any', 'include' => array( $path_id ) ) );
181
+ if ( empty( $p_array ) ) {
182
+ if ( $opt_array['type'] == 'admin' ) {
183
+ echo 'Admin=<a href="' . get_home_url() . '/' . $opt_array['rel_url'] . '" target="_blank">' . "$path_id</a>";
184
+ } else {
185
+ echo 'Path=<a href="' . get_home_url() . "/$path_id" . '" target="_blank">' . "$path_id</a>";
186
+ }
187
+ } else {
188
+ echo ucfirst( $p_array[0]->post_type ) . '=<a href="' . get_permalink( $path_id ) . '" target="_blank">' . $p_array[0]->post_title . '</a>';
189
+ }
190
+ }
191
+ }
192
+ }
193
+ echo '<br />';
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Section text for Section3
199
+ *
200
+ * Display an explanation of this Section
201
+ *
202
+ */
203
+ function jr_mt_single_settings_expl() {
204
+ ?>
205
+ <p>
206
+ Select a Theme for an individual Page, Post or
207
+ any other non-Admin pages that has its own Permalink; for example, a specific Archive or Category page.
208
+ <p>
209
+ </p>
210
+ Then cut and paste the URL of the desired Page, Post or other non-Admin page.
211
+ And click the <b>Save Changes</b> button to add the entry.
212
+ </p>
213
+ <?php
214
+ }
215
+
216
+ function jr_mt_echo_add_theme() {
217
+ jr_mt_themes_field( 'add_theme', '', 'jr_mt_settings', FALSE );
218
+ }
219
+
220
+ function jr_mt_echo_add_path_id() {
221
+ ?>
222
+ <input id="add_path_id" name="jr_mt_settings[add_path_id]" type="text" size="100" maxlength="256" value="" />
223
+ <br />
224
+ (cut and paste URL of Page or Post here)
225
+ <br />
226
+ URL must begin with
227
+ <?php
228
+ echo trim( get_home_url(), '\ /' ) . '/';
229
+ }
230
+
231
+ function jr_mt_validate_settings( $input ) {
232
+ $valid = array();
233
+ foreach ( array( 'all_pages', 'all_posts', 'site_home' ) as $thing ) {
234
+ $valid[$thing] = $input[$thing];
235
+ }
236
+
237
+ $settings = get_option( 'jr_mt_settings' );
238
+ $ids = $settings['ids'];
239
+ if ( isset ( $input['del_entry'] ) ) {
240
+ foreach ( $input['del_entry'] as $del_entry ) {
241
+ unset( $ids[$del_entry] );
242
+ }
243
+ }
244
+
245
+ $url = trim( $input['add_path_id'] );
246
+ if ( ( empty( $input['add_theme'] ) && !empty( $url ) ) || ( !empty( $input['add_theme'] ) && empty( $url ) ) ) {
247
+ add_settings_error(
248
+ 'jr_mt_settings',
249
+ 'jr_mt_emptyerror',
250
+ 'Both URL and Theme must be specified to add an Individual entry',
251
+ 'error'
252
+ );
253
+ } else {
254
+ if ( !empty( $url ) ) {
255
+ $validate_url = jr_mt_site_url( $url );
256
+ if ( $validate_url === TRUE ) {
257
+ extract( jr_mt_url_to_id( $url ) );
258
+ if ( $home ) {
259
+ add_settings_error(
260
+ 'jr_mt_settings',
261
+ 'jr_mt_homeerror',
262
+ 'Please use "Select Theme for Site Home" field instead of specifying Site Home URL as an individual entry.',
263
+ 'error'
264
+ );
265
+ } else {
266
+ if ( $type == 'admin' ) {
267
+ add_settings_error(
268
+ 'jr_mt_settings',
269
+ 'jr_mt_adminerror',
270
+ 'Admin Page URLs are not allowed because no known Themes alter the appearance of Admin pages.',
271
+ 'error'
272
+ );
273
+ } else {
274
+ if ( $id === FALSE ) {
275
+ $key = $page_url;
276
+ } else {
277
+ $key = $id;
278
+ }
279
+ $ids[$key] = array(
280
+ 'theme' => $input['add_theme'],
281
+ 'type' => $type,
282
+ 'id' => $id,
283
+ 'page_url' => $page_url,
284
+ 'rel_url' => $rel_url,
285
+ 'url' => $url
286
+ );
287
+ }
288
+ }
289
+ } else {
290
+ add_settings_error(
291
+ 'jr_mt_settings',
292
+ 'jr_mt_urlerror',
293
+ "Invalid URL specified for Individual page/post: '$url'. $validate_url",
294
+ 'error'
295
+ );
296
+ }
297
+ }
298
+ }
299
+ $valid['ids'] = $ids;
300
+ return $valid;
301
+ }
302
+
303
+ // Add Link to the plugin's entry on the Admin "Plugins" Page, for easy access
304
+ global $jr_mt_plugin_basename;
305
+ add_filter( "plugin_action_links_$jr_mt_plugin_basename", 'jr_mt_plugin_action_links', 10, 1 );
306
+
307
+ /**
308
+ * Creates Settings entry right on the Plugins Page entry.
309
+ *
310
+ * Helps the user understand where to go immediately upon Activation of the Plugin
311
+ * by creating entries on the Plugins page, right beside Deactivate and Edit.
312
+ *
313
+ * @param array $links Existing links for our Plugin, supplied by WordPress
314
+ * @param string $file Name of Plugin currently being processed
315
+ * @return string $links Updated set of links for our Plugin
316
+ */
317
+ function jr_mt_plugin_action_links( $links ) {
318
+ // The "page=" query string value must be equal to the slug
319
+ // of the Settings admin page.
320
+ array_push( $links, '<a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=jr_mt_settings' . '">Settings</a>' );
321
+ return $links;
322
+ }
323
+
324
+ function jr_mt_themes_field( $field_name, $theme_name, $setting, $excl_current_theme ) {
325
+ echo "<select id='$field_name' name='$setting" . "[$field_name]' size='1'>";
326
+ if ( empty( $theme_name ) ) {
327
+ $selected = 'selected="selected"';
328
+ } else {
329
+ $selected = '';
330
+ }
331
+ echo "<option value='' $selected></option>";
332
+ foreach ( wp_get_themes() as $folder => $theme_obj ) {
333
+ if ( $excl_current_theme ) {
334
+ if ( jr_mt_current_theme() == $folder ) {
335
+ // Skip the Current Theme
336
+ continue;
337
+ }
338
+ }
339
+ if ( $theme_name == $folder ) {
340
+ $selected = 'selected="selected"';
341
+ } else {
342
+ $selected = '';
343
+ }
344
+ $name = $theme_obj->Name;
345
+ echo "<option value='$folder' $selected>$name</option>";
346
+ }
347
+ echo '</select>' . PHP_EOL;
348
+ }
349
+
350
+ ?>
includes/debug/debug.php ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /* require_once( jr_mt_path() . 'includes/debug/debug.php' );
3
+ jr_dump( 'jr_mt_validate_settings $input', $input );
4
+ */
5
+
6
+ function jr_dump( $comment, $dump_var ) {
7
+ $file_name = 'jonradio-dump.txt';
8
+
9
+ $header = '***' . current_time('mysql') . ': ' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
10
+ ob_start();;
11
+ echo "$comment: ";
12
+ var_dump( $dump_var );
13
+ $output = ob_get_clean() . jr_dump_env();
14
+ if ( function_exists('is_multisite') && is_multisite() ) {
15
+ global $site_id, $blog_id;
16
+ $file_name = $site_id . '-' . $blog_id . '-' . $file_name;
17
+ }
18
+ $file = fopen( plugin_dir_path( __FILE__ ) . $file_name, 'at' );
19
+ fwrite( $file, $header . PHP_EOL . $output );
20
+ fclose( $file );
21
+
22
+ return;
23
+ }
24
+
25
+ function jr_dump_env() {
26
+ global $jr_dump_env_first;
27
+ if ( isset( $jr_dump_env_first ) ) {
28
+ $output = '';
29
+ } else {
30
+ $jr_dump_env_first = FALSE;
31
+ $output = PHP_EOL;
32
+
33
+ }
34
+ return $output;
35
+ }
36
+ ?>
includes/functions.php ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Return WordPress Current Theme, as defined in Appearance Admin panels
4
+ *
5
+ * Obtains Folder Name of Current Theme, from 'template' option of wp_load_alloptions().
6
+ *
7
+ * @param string $option optional parameter that is not currently used,
8
+ * but may in the future select current template or stylesheet
9
+ * @return string type Folder Name of Current Theme
10
+ */
11
+ function jr_mt_current_theme( $option='stylesheet' ) {
12
+ global $jr_mt_options_cache;
13
+ return $jr_mt_options_cache[$option];
14
+ }
15
+
16
+ /**
17
+ * Given URL, return post or page ID, if possible, and relative path if not
18
+ *
19
+ * Calls jr_mt_query_keywords.
20
+ *
21
+ * @param string $url full URL of WordPress page, post, admin, etc.
22
+ * @return array array with keys of "type", "id" and "page_url":
23
+ * string type "pages", "posts" or "admin"
24
+ * string id Page ID or Post ID or FALSE
25
+ * string page_url relative URL WordPress page, post, admin, etc. or FALSE
26
+ * string rel_url URL relative to WordPress home
27
+ * bool home is URL Site Home?
28
+ */
29
+ function jr_mt_url_to_id( $url_orig ) {
30
+ // Some hosts, likely IIS, insert an erroneous "/index.php" into the middle of the Permalink in $_SERVER['REQUEST_URI']
31
+ $url = str_replace( '/index.php', '', $url_orig );
32
+
33
+ $trim = '\ /'; // remove leading and trailing backslashes, blanks and forward slashes
34
+
35
+ $is_home = FALSE;
36
+
37
+ // get_home_url() returns "https://subdomain.domain.com/wp" - the full URL of the home page of the site
38
+ $home = trim( parse_url( get_home_url(), PHP_URL_PATH ), $trim ); // "wp"
39
+
40
+ $admin_home = trim( parse_url( admin_url(), PHP_URL_PATH ), $trim );
41
+ $page_url = trim( parse_url( $url, PHP_URL_PATH ), $trim ); // "wp/fruit/apples"
42
+ $is_admin = ( $admin_home == substr( $page_url, 0, strlen( $admin_home ) ) );
43
+ if ( !empty( $home ) ) { // Only if WordPress is installed in a subfolder, NOT in the Root
44
+ $page_url = trim( substr( $page_url, stripos( $page_url, $home ) + strlen( $home ) ), $trim ); // "fruit/apples"
45
+ }
46
+ $rel_url = $page_url;
47
+
48
+ $type = FALSE;
49
+
50
+ $id = jr_mt_query_keywords( parse_url( $url, PHP_URL_QUERY ) );
51
+ if ( $id === NULL ) {
52
+ if ( $is_admin ) {
53
+ $id = FALSE;
54
+ $type = 'admin';
55
+ } else {
56
+ // Check for home page (get_page_by_path() does not work for home page)
57
+ if ( empty( $page_url ) ) {
58
+ $is_home = TRUE;
59
+ $id = get_option('page_on_front');
60
+ if ( $id == 0 ) {
61
+ // There is no home Page; posts are displayed instead on the home page
62
+ $page_url = '';
63
+ $id = FALSE;
64
+ } else {
65
+ $type = 'pages';
66
+ }
67
+ } else {
68
+ $page = get_page_by_path( $page_url );
69
+ if ( $page === NULL ) {
70
+ // get_page_by_path() returns NULL for Posts, Home Page, Admin, etc.
71
+ // So, check for Posts:
72
+ $post = get_posts( array( 'name' => $page_url ) );
73
+ if ( empty( $post ) ) {
74
+ $id = FALSE;
75
+ } else {
76
+ $id = $post[0]->ID;
77
+ $type = 'posts';
78
+ }
79
+ } else {
80
+ $id = $page->ID;
81
+ $type = 'pages';
82
+ }
83
+ }
84
+ }
85
+ } else {
86
+ // id in query of URL (?keyword=value&keyword=value)
87
+ $type = key( $id );
88
+ $id = $id[$type];
89
+ $page_url = FALSE;
90
+ }
91
+ return array( 'type' => $type, 'id' => $id, 'page_url' => $page_url, 'rel_url' => $rel_url, 'home' => $is_home );
92
+ }
93
+
94
+ /**
95
+ * Return page_id= or p= (post ID) or page= (admin page) value from a URL
96
+ *
97
+ * Calls parse_str function in its own variable space because it could create virtually any variable name!
98
+ * Only looks at page_id=, p= and page= now, but could be expanded to other query keywords.
99
+ *
100
+ * @param string $url_query Query portion (after the ?) in a URL
101
+ * @return var array with key of "pages", "posts" or "admin" and value of page_id=, p= or page=, respectively; or NULL if none are present
102
+ */
103
+ function jr_mt_query_keywords( $url_query ) {
104
+ if ( $url_query === NULL ) {
105
+ return NULL;
106
+ } else {
107
+ parse_str( $url_query );
108
+ if ( isset( $page_id ) ) {
109
+ return array( 'pages' => $page_id );
110
+ } else {
111
+ if ( isset( $p ) ) {
112
+ return array( 'posts' => $p );
113
+ } else {
114
+ if ( isset( $page ) ) {
115
+ return array( 'admin' => $page );
116
+ } else {
117
+ if ( isset( $cat ) ) {
118
+ return array( 'cat' => $cat );
119
+ } else {
120
+ if ( isset( $m ) ) {
121
+ return array( 'archive' => $m );
122
+ } else {
123
+ return NULL;
124
+ }
125
+ }
126
+ }
127
+ }
128
+ }
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Is the URL on the current WordPress web site?
134
+ *
135
+ * Checks if URL begins with Site Home URL.
136
+ *
137
+ * @param string $url URL to be checked to be sure it is "on" the current WordPress web site
138
+ * @return var bool TRUE if URL on current WordPress web site; string error message otherwise
139
+ */
140
+ function jr_mt_site_url( $url ) {
141
+ $check_url = trim( $url );
142
+ if ( strcasecmp( 'http', substr( $check_url, 0, 4 ) ) != 0 ) {
143
+ return 'URL does not begin with http://';
144
+ }
145
+ $site_home = get_home_url();
146
+ if ( strcasecmp( $site_home, substr( $check_url, 0, strlen( $site_home ) ) ) != 0 ) {
147
+ return "URL specified is not part of current WordPress web site. URL must begin with '$site_home'";
148
+ }
149
+ return TRUE;
150
+ }
151
+ ?>
includes/select-theme.php ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // Select the relevant Theme
3
+
4
+ add_filter( 'pre_option_stylesheet', 'jr_mt_stylesheet' );
5
+ add_filter( 'pre_option_template', 'jr_mt_template' );
6
+
7
+ function jr_mt_stylesheet() {
8
+ return jr_mt_theme( 'stylesheet' );
9
+ }
10
+
11
+ function jr_mt_template() {
12
+ return jr_mt_theme( 'template' );
13
+ }
14
+
15
+ function jr_mt_theme( $option ) {
16
+ /* The hooks that (indirectly) call this function are called repeatedly by WordPress,
17
+ so do the checking once and store the values in a global array.
18
+ */
19
+ global $jr_mt_theme;
20
+ if ( !isset( $jr_mt_theme ) ) {
21
+ $jr_mt_theme = array();
22
+ }
23
+ if ( !isset( $jr_mt_theme[$option] ) ) {
24
+ $theme = jr_mt_chosen();
25
+ if ( $theme === FALSE ) {
26
+ // $jr_mt_theme[$option] = jr_mt_current_theme( $option ); if stylesheet and template are ever different
27
+ $jr_mt_theme['template'] = jr_mt_current_theme();
28
+ $jr_mt_theme['stylesheet'] = jr_mt_current_theme();
29
+ } else {
30
+ $jr_mt_theme[$option] = $theme;
31
+ }
32
+ }
33
+ return $jr_mt_theme[$option];
34
+ }
35
+
36
+ function jr_mt_chosen() {
37
+ extract( jr_mt_url_to_id( 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] ) );
38
+ $settings = get_option( 'jr_mt_settings' );
39
+ if ( $home ) {
40
+ if ( trim( $settings['site_home'] ) != '' ) {
41
+ return $settings['site_home'];
42
+ }
43
+ }
44
+ $ids = $settings['ids'];
45
+ if ( $id === FALSE ) {
46
+ if ( isset( $ids[$page_url] ) ) {
47
+ $theme = $ids[$page_url]['theme'];
48
+ } else {
49
+ $theme = jr_mt_check_all( $type );
50
+ }
51
+ } else {
52
+ if ( isset( $ids[$id] ) ) {
53
+ $theme = $ids[$id]['theme'];
54
+ } else {
55
+ $theme = jr_mt_check_all( $type );
56
+ }
57
+ }
58
+ return $theme;
59
+ }
60
+
61
+ function jr_mt_check_all( $type ) {
62
+ if ( $type === FALSE ) {
63
+ $theme = FALSE;
64
+ } else {
65
+ $settings = get_option( 'jr_mt_settings' );
66
+ if ( isset( $settings["all_$type"] ) ) {
67
+ $theme = $settings["all_$type"];
68
+ } else {
69
+ $theme = '';
70
+ }
71
+ if ( empty( $theme ) ) {
72
+ $theme = FALSE;
73
+ }
74
+ }
75
+ return $theme;
76
+ }
77
+
78
+ ?>
jonradio-multiple-themes.php ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: jonradio Multiple Themes
4
+ Plugin URI: http://zatzlabs.com/plugins/
5
+ Description: Select different Themes for one or more, or all WordPress Pages, Posts or other non-Admin pages. Or Site Home.
6
+ Version: 3.0
7
+ Author: David Gewirtz
8
+ Author URI: http://zatzlabs.com/plugins/
9
+ License: GPLv2
10
+ */
11
+
12
+ /* Copyright 2012 jonradio (email : info@zatz.com)
13
+
14
+ This program is free software; you can redistribute it and/or modify
15
+ it under the terms of the GNU General Public License as published by
16
+ the Free Software Foundation; either version 2 of the License, or
17
+ (at your option) any later version.
18
+
19
+ This program is distributed in the hope that it will be useful,
20
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ GNU General Public License for more details.
23
+
24
+ You should have received a copy of the GNU General Public License
25
+ along with this program; if not, write to the Free Software
26
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
+ */
28
+
29
+ // Limitation: This plugin does not currently support Theme usage that involves the stylesheet and template names not being the same.
30
+
31
+ global $jr_mt_plugin_folder;
32
+ $jr_mt_plugin_folder = basename( dirname( __FILE__ ) );
33
+
34
+ function jr_mt_plugin_folder() {
35
+ global $jr_mt_plugin_folder;
36
+ return $jr_mt_plugin_folder;
37
+ }
38
+
39
+ global $jr_mt_path;
40
+ $jr_mt_path = plugin_dir_path( __FILE__ );
41
+ function jr_mt_path() {
42
+ global $jr_mt_path;
43
+ return $jr_mt_path;
44
+ }
45
+
46
+ global $jr_mt_plugin_basename;
47
+ $jr_mt_plugin_basename = plugin_basename( __FILE__ );
48
+
49
+ if ( !function_exists( 'get_plugin_data' ) ) {
50
+ require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
51
+ }
52
+
53
+ global $jr_mt_plugin_data;
54
+ $jr_mt_plugin_data = get_plugin_data( __FILE__ );
55
+
56
+ global $jr_mt_options_cache;
57
+ $all_options = wp_load_alloptions();
58
+ $jr_mt_options_cache['stylesheet'] = $all_options['stylesheet'];
59
+ $jr_mt_options_cache['template'] = $all_options['template'];
60
+
61
+ register_activation_hook( __FILE__, 'jr_mt_activate' );
62
+ register_deactivation_hook( __FILE__, 'jr_mt_deactivate' );
63
+
64
+ function jr_mt_activate( $network_wide ) {
65
+ if ( $network_wide ) {
66
+ global $wpdb, $site_id;
67
+ $blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = $site_id" );
68
+ foreach ( $blogs as $blog_obj ) {
69
+ if ( switch_to_blog( $blog_obj->blog_id ) ) {
70
+ // We know the Site actually exists
71
+ jr_mt_activate1();
72
+ }
73
+ }
74
+ restore_current_blog();
75
+ } else {
76
+ jr_mt_activate1();
77
+ }
78
+ }
79
+
80
+ function jr_mt_activate1() {
81
+ $settings = array(
82
+ 'all_pages' => '',
83
+ 'all_posts' => '',
84
+ 'site_home' => '',
85
+ 'ids' => array()
86
+ );
87
+ // Nothing happens if Settings already exist
88
+ add_option( 'jr_mt_settings', $settings );
89
+
90
+ global $jr_mt_plugin_data;
91
+ $internal_settings = array(
92
+ 'version' => $jr_mt_plugin_data['Version']
93
+ ); // Only records you version when plugin installed, not current version
94
+ add_option( 'jr_mt_internal_settings', $internal_settings ); // Nothing happens if Settings already exist
95
+ }
96
+
97
+ add_action( 'wpmu_new_blog', 'jr_mt_new_site', 10, 6 );
98
+
99
+ function jr_mt_new_site( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
100
+ if ( is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
101
+ switch_to_blog( $blog_id );
102
+ jr_mt_activate1();
103
+ restore_current_blog();
104
+ }
105
+ }
106
+
107
+ function jr_mt_deactivate() {
108
+ // Nothing (yet)
109
+ }
110
+
111
+ jr_mt_version_check();
112
+
113
+ function jr_mt_version_check() {
114
+ // Check for Plugin Version update (Deactivate and Activate Hooks not fired)
115
+ $internal_settings = get_option( 'jr_mt_internal_settings' );
116
+ if ( $internal_settings ) { // Just in case Activation has not occurred yet
117
+ global $jr_mt_plugin_data;
118
+ if ( version_compare( $internal_settings['version'], $jr_mt_plugin_data['Version'], '<' ) ) {
119
+ $settings = get_option( 'jr_mt_settings' );
120
+ if ( isset( $settings['ids'] ) ) {
121
+ $ids = $settings['ids'];
122
+ } else {
123
+ $ids = array();
124
+ }
125
+ if ( version_compare( $internal_settings['version'], '2.1', '<' ) ) {
126
+ unset( $settings['all_admin'] );
127
+ // Check for Site Home entry, remove it and set Site Home field
128
+ // And remove all Admin entries (no longer supported)
129
+ if ( isset( $ids[''] ) ) {
130
+ $settings['site_home'] = $ids['']['theme'];
131
+ unset( $ids[''] );
132
+ } else {
133
+ $settings['site_home'] = '';
134
+ }
135
+ foreach ( $ids as $key => $arr ) {
136
+ if ( $arr['type'] == 'admin' ) {
137
+ unset( $ids[$key] );
138
+ }
139
+ }
140
+ }
141
+ if ( version_compare( $internal_settings['version'], '3.0', '<' ) ) {
142
+ foreach ( $ids as $key => $arr ) {
143
+ if ( strcasecmp( 'http', substr( $arr['rel_url'], 0, 4 ) ) == 0 ) {
144
+ unset( $ids[$key] );
145
+ }
146
+ }
147
+ }
148
+ $settings['ids'] = $ids;
149
+ update_option( 'jr_mt_settings', $settings );
150
+ $internal_settings['version'] = $jr_mt_plugin_data['Version'];
151
+ update_option( 'jr_mt_internal_settings', $internal_settings );
152
+ }
153
+ }
154
+ }
155
+
156
+ require_once( jr_mt_path() . 'includes/functions.php' );
157
+
158
+ if ( is_admin() ) {
159
+ // Admin panel
160
+ require_once( jr_mt_path() . 'includes/admin.php' );
161
+ } else {
162
+ // Do not try and select a Theme for Admin Pages
163
+ // Check that template and stylesheet have the same value for the Current Theme, as Plugin expects this to be true.
164
+ global $jr_mt_options_cache;
165
+ if ( $jr_mt_options_cache['template'] == $jr_mt_options_cache['stylesheet'] ) {
166
+ require_once( jr_mt_path() . 'includes/select-theme.php' );
167
+ }
168
+ }
169
+
170
+ /*
171
+ Research Notes:
172
+ The first time one of these Filter Hooks fires, pre_option_stylesheet and pre_option_template, only the following functions can be used to help determine "where" you are in the site:
173
+ - is_admin()
174
+ - is_user_logged_in()
175
+ - get_option("page_on_front") - ID of home page; zero if Reading Settings NOT set to a Static Page of a WordPress Page
176
+ */
177
+
178
+ ?>
readme.txt ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === jonradio Multiple Themes ===
2
+ Contributors: dgewirtz
3
+ Donate link: http://zatzlabs.com/plugins/
4
+ Tags: themes, theme
5
+ Requires at least: 3.4.1
6
+ Tested up to: 3.4.2
7
+ Stable tag: 3.0
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+
11
+ Select different Themes for one or more, or all WordPress Pages, Posts or other non-Admin pages. Or Site Home.
12
+
13
+ == Description ==
14
+
15
+ Allows the Administrator to specify which Theme will be used on specific Pages, Posts, other non-Admin pages (such as Category or Archive pages) or Site Home. Also allows a Theme to be specified for All Pages or All Posts. In turn, even when a Theme is specified for All Pages or All Posts, a different Theme can still be specified for specific Pages or Posts.
16
+
17
+ If a Theme is not specified for a Page, Post, Site Home or other WordPress-displayed web page, such as Category or Archive display, the **Current Theme** specified in **Appearance-Themes** on the Admin panels will be used.
18
+
19
+ When selecting a Theme for Site Home or all or specific Pages, Posts or other non-Admin pages, the Theme must be shown in the list of Available Themes on the Appearance-Themes Admin panel. If necessary, install the Theme. In a WordPress Network (AKA Multisite), Themes must be **Network Enabled** before they will appear as Available Themes on individual sites' Appearance-Themes panel.
20
+
21
+ I hesitate to use the term **Theme Switcher** to describe this plugin, because the term has so many meanings. This plugin does **not** alter the standard WordPress options that define what Theme is used on your WordPress site. Instead, it dynamically (and selectively) overrides that choice. Technical details aside, what this means is that deactivating or deleting the plugin will instantly revert to the WordPress Current Theme that you have defined through the standard WordPress Appearance-Themes Admin panel.
22
+
23
+ == Installation ==
24
+
25
+ This section describes how to install the plugin and get it working.
26
+
27
+ 1. Use "Add Plugin" within the WordPress Admin panel to download and install this plugin from the WordPress.org plugin repository (preferred method). Or download and unzip this plugin, then upload the `/jonradio-multiple-themes/` directory to your WordPress web site's `/wp-content/plugins/` directory.
28
+ 1. Activate the plugin through the 'Plugins' menu in WordPress. If you have a WordPress Network ("Multisite"), you can either Network Activate this plugin, or Activate it individually on the sites where you wish to use it. Activating on individual sites within a Network avoids some of the confusion created by WordPress' hiding of Network Activated plugins on the Plugin menu of individual sites.
29
+ 1. Be sure that all Themes you plan to use have been installed and are listed under Available Themes on the WordPress Appearance-Themes Admin panel. In a WordPress Network (AKA Multisite), Themes must be **Network Enabled** before they will appear as Available Themes on individual sites' Appearance-Themes panel.
30
+ 1. Select Themes to be used on the Plugin's "Multiple Themes plugin" page in the WordPress Admin panels, which is found in both the **Appearance** and **Settings** sections. You can also get to this page by clicking on the **Settings** link for this plugin on the **Installed Plugins** page.
31
+
32
+ == Frequently Asked Questions ==
33
+
34
+ = What happens when I change Permalinks? =
35
+
36
+ For entries you have created for individual Pages and Posts with this plugin, you can subsequently change your Permalink structure or you can change the Permalink of a Page or Post, without having to change the plugin's entry. However, for other pages, such as Categories or Archives, you will have to delete your old entries and create new ones with this plugin.
37
+
38
+ = I added a new entry but why doesn't it appear in the list of entries? =
39
+
40
+ If you add an entry that already exists, it merely replaces the previous entry.
41
+
42
+ = How can I change the Theme for an entry? =
43
+
44
+ Simply add the entry again, with the new Theme. It will replace the previous entry.
45
+
46
+ == Screenshots ==
47
+
48
+ 1. Plugin's Admin Page when first installed (Version 2.0)
49
+ 2. Plugin's Admin Page with entries added (Version 2.0)
50
+
51
+ == Changelog ==
52
+
53
+ = 3.0 =
54
+ * Add Support for Categories and Archives when no Permalinks exist (support already existed Categories and Archives with Permalinks)
55
+ * Resolve several minor bugs
56
+
57
+ = 2.9 =
58
+ * Rewrite much of the Settings page and Plugin Directory documentation
59
+ * Add Support for IIS which returns incorrect values in $_SERVER['REQUEST_URI']
60
+ * Make it easier to select the Theme for the Site Home by providing a new Settings field
61
+ * Remove ability to set Theme for Admin pages since no known Theme provides Admin templates, and because the previous implementation sometimes displayed the incorrect Current Theme in Admin; this feature may be re-added in a future release, and could even be used to change Settings of Themes that are not currently the Current Theme
62
+ * Add version upgrade detection to add, remove and update Settings fields
63
+ * Move Settings link on Plugins page from beginning to end of links
64
+
65
+ = 2.0 =
66
+ * Address pecularities of wp_make_link_relative() related to root-based WordPress sites using Permalinks
67
+
68
+ = 1.1 =
69
+ * Fix foreach failing on some systems, based on PHP warning level
70
+
71
+ = 1.0 =
72
+ * Make plugin conform to WordPress plugin repository standards.
73
+ * Beta testing completed.
74
+
75
+ == Upgrade Notice ==
76
+
77
+ = 3.0 =
78
+ Improve support for Categories and Archives, and eliminate all known bugs.
79
+
80
+ = 2.9 =
81
+ Improve Settings fields, correct display of wrong Current Theme in Appearance-Themes Admin panel, and add IIS Support.
82
+
83
+ = 2.0 =
84
+ Selecting Individual Pages and Posts on a WordPress site installed in the root and using Permalinks now works correctly.
85
+
86
+ = 1.1 =
87
+ Eliminate possibility of foreach error message if PHP warning level is set at a high level
88
+
89
+ = 1.0 =
90
+ Beta version 0.9 had not been tested when installed from the WordPress Plugin Repository
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file
uninstall.php ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // Ensure call comes from WordPress, not a hacker.
3
+ if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
4
+ exit ();
5
+
6
+ /* Remove any tables, options, and such created by this Plugin */
7
+ if ( function_exists('is_multisite') && is_multisite() ) {
8
+ global $wpdb, $site_id;
9
+ $blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = $site_id" );
10
+ foreach ($blogs as $blog_obj) {
11
+ delete_blog_option( $blog_obj->blog_id, 'jr_mt_settings' );
12
+ delete_blog_option( $blog_obj->blog_id, 'jr_mt_internal_settings' );
13
+ }
14
+ } else {
15
+ delete_option( 'jr_mt_settings' );
16
+ delete_option( 'jr_mt_internal_settings' );
17
+ }
18
+ ?>