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 want to build a very simple toggle for taking my site down into a maintenance mode. To do that, I want to add a admin area to define a template that is the maintenance page, and allow that page to override the database defined template when maintenance mode is switched on.

How can I change the theme template called for each page, WITHOUT affecting the database?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

you can use template_redirect action hook to php include your maintenance mode template file using a simple option in options database.

When you turn maintenance mode on add an option for example:

add_option('maintenance_mode_on');

then with this code you check if that option is set and if so you redirect to your desired template file:

function custom_maintenance_mode_template_redirect() {
    global $wp;
    if(get_option('maintenance_mode_on')){
        status_header(200); // a 404 code will not be returned in the HTTP headers if the page does not exists

        include(TEMPLATEPATH . "/Custom_template.php"); // include the corresponding template
        die();
    }
}
add_action( 'template_redirect', 'custom_maintenance_mode_template_redirect' );

Then when you turn maintenance mode off delete that option :

delete_option('maintenance_mode_on');

Update

If you want to effect the body_class() you can use body_class filter hook:

function custom_body_class($classes){
    if(get_option('maintenance_mode_on')){
            $n_classes[] = "maintenance";
        return $n_classes;
    } else {
        return $classes;
    }
}

add_filter('body_class', 'custom_body_class');

This will change the body_class() to output maintenance when maintenance mode is turned on.

share|improve this answer
    
Only problem here is wordpress still thinks it is loading the old page template, and my CSS is dependent on the body_class() within the header. –  Mild Fuzz Jun 1 '11 at 11:52
    
using this code correctly makes wordpress load /Custom_template.php only when maintenance_mode_on is on and set correctly –  Bainternet Jun 1 '11 at 12:05
    
It loads the template just fine, but the header is still loaded (wanted behaviour) but the header behaves as if Wordpress is loading the normal template (single.php) not the actual template (maintenance.php). This is shows by the class names body_class() adds to the body element. This function would be perfect if it could affect that! –  Mild Fuzz Jun 1 '11 at 13:34
    
i see what you mean , i updated my answer –  Bainternet Jun 1 '11 at 15:08
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.