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.

Is it possible to change the template loaded for a page, as it loads, without having to change the record in the database?

I suspect it can be done with the template_redirect action, and the will probably have something to do with the template-loader.php file in the WP core, but I can't figure it out.

Any pointers would be grand!!

share|improve this question

1 Answer 1

here's a simple template_redirect example that checks if a particular page is being requested and loads a template specifically for it:

function my_template_redirect() {
    if ( is_page('my-special-page') ) :
        include (TEMPLATEPATH . '/my-page-template.php');
        exit;
    endif;
}
add_action( 'template_redirect', 'my_template_redirect' );

an example that checks for the existence of a GET var:

function my_template_redirect() {
    if ( isset($_GET['somevar']) ) :
        include (TEMPLATEPATH . '/special.php');
        exit;
    endif;
}
add_action( 'template_redirect', 'my_template_redirect' );
share|improve this answer
    
I want it to respond to get variable –  Mild Fuzz Aug 6 '11 at 16:30
1  
see edit, not sure that's what you're after. –  Milo Aug 6 '11 at 16:37
1  
If we use this method template_redirect + exit; then the page won't have any html code inside <head> included. Can I somehow only replace <body> with my code from a plug-in? –  Radek Aug 17 '13 at 7:17

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.