Created
November 25, 2012 21:17
-
-
Save RalfAlbert/4145401 to your computer and use it in GitHub Desktop.
Switch theme on specific page
This file contains hidden or 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
<?php | |
/* | |
Plugin Name: Switch Theme | |
Plugin URI: | |
Description: | |
Version: | |
Author: | |
Author URI: | |
License: | |
*/ | |
if (is_admin()){ | |
/* load plugin functions */ | |
add_action('add_meta_boxes', 'myplugin_add_meta_box'); | |
add_action('save_post', 'myplugin_save_meta_box'); | |
} else { | |
add_action( 'init', 'my_template_hooks', 5, 0 ); | |
} | |
function my_template_hooks(){ | |
/* load page theme */ | |
add_filter( 'option_template', 'my_plugin_get_page_theme'); | |
add_filter( 'template', 'my_plugin_get_page_theme'); | |
add_filter( 'option_stylesheet', 'my_plugin_get_page_theme'); | |
} | |
/* add meta box to page editing */ | |
function myplugin_add_meta_box() { | |
add_meta_box( | |
'', | |
'Page Theme', | |
'myplugin_get_meta_box_contents', | |
'page', | |
'normal', | |
'default', | |
'' | |
); | |
} | |
/* meta box contents */ | |
function myplugin_get_meta_box_contents($post) { | |
$themes = wp_get_themes(); | |
$page_theme = get_post_meta($post->ID, 'page_theme', true); | |
echo '<p><label for=page_theme><b>Themes:</b></label></p>'; | |
echo '<select name=page_theme>'; | |
echo '<option>(ohne)</option>'; | |
foreach($themes as $key => $theme) : | |
$selected = ($themes[$key]["Template"] == $page_theme) ? 'selected' : ''; | |
echo '<option value="' . $themes[$key]['Template']. '"'. $selected .'>' . $themes[ $key ]['Name'] . ' </option>'; | |
endforeach; | |
echo '</select>'; | |
} | |
/* save theme options */ | |
function myplugin_save_meta_box($post_id) { | |
if( isset( $_POST['page_theme'] ) && ! empty( $_POST['page_theme'] ) ) | |
update_post_meta($post_id, 'page_theme', $_POST['page_theme']); | |
} | |
function my_plugin_get_page_theme() { | |
$page_id = get_the_ID(); | |
$page_theme = get_post_meta($page_id, 'page_theme', true); | |
return $page_theme; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment