Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to write a little plugin which allows me to add a different theme to each page. well.. still trying.. ;)

code: http://pastebin.com/dP1GH43E

When I put return 'mytesttheme'; at the end it works just perfect! when I add an ID ($page_theme = get_post_meta('66', [...]) it also works..

when I write echo $page_theme.', '; before the return, it outputs: , , , mytesttheme, mytesttheme, mytesttheme, mytesttheme,

The empty spaces are the problem I think.

in wp-backend it shows "mytesttheme" as post meta content in wp-core post meta content box.. so the saving works as it should..

Any ideas how to solve the problem? :S

share|improve this question
    
this: global $wp_query; $postid = $wp_query->post->ID; if ($postid == '') : echo 'NO-ID, '; else : echo get_post_meta($postid, 'page_theme', true) .', '; endif; returns: NO-ID, NO-ID, NO-ID, NO-ID, NO-ID, NO-ID, NO-ID, cuttlefish, cuttlefish, –  Jan Apr 22 '12 at 15:46
add comment

2 Answers 2

There are several issues with your code. Let's start at the beginning...

Adding hooks

All your hooks will be added at the backend. Even those, which are outside the if((is_admin)) branch. Add your hooks explicit where they are should work:

if (is_admin()){
  // do this on backend (admin side)
    /* load plugin functions */
    add_action('add_meta_boxes', 'myplugin_add_meta_box');
    add_action('save_post', 'myplugin_save_meta_box');

} else {
  // if we are NOT on backend (admin side) we have to be on frontend, because there is only frontend and backend.
    /* 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');

}

In the loop or not?

get_the_ID() must be within the loop. Where are your hooks ('option_template', 'template' and 'option_stylesheet') are done? Before the query is done or after (means: inside the loop or outside) ? Two lines of code give us the answer:

global $post;
var_dump( $post );

If $post is empty (null), we areoutside the loop. We hooked to early. We need a hook after the query is done. The ´init´ hook is a ood one. Let's alter the code above a bit and get the plugin working:

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');

    }

Now our hooks are inside the loop and the plugin works fine.

Deprecated function

On line 28 of your plugin code you use $themes = get_themes();. get_themes() is deprecated and should be replaced with ´wp_get_themes()´.

Development

Please enable WP-debugging (set WP_DEBUG to TRUE in your wp-config.php). The debug messages will help you much.

You can find the complete modified code on Github

share|improve this answer
add comment

As far I know, to use post->ID outside of loop, wp_query should be called first. I mean something like that the code bellow which is already suggested by @Jan

global $wp_query; 
$postid = $wp_query->post->ID;

Optionally, get_post_id() can work for you, check codex for more.

share|improve this answer
    
thank 4 thinking about it, but "Jan" is just me ;) when i do it that way the id is not always transfered.. :S –  Jan Apr 22 '12 at 17:39
    
Ops, I missed the name of querist. Anyway, did you try get_the_ID()? –  Enthusiast Apr 23 '12 at 6:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.