Quick n’ Dirty Offline StatusFiled Under: Snippets, plugins

This is the last post in my, Quick N Dirty plugins series and so I have decided to do something a little, although not a lot, more involved. This plugin will let you display a notification page that your blog is offline.
I’m sure you’ve seen by now the excellent pre-launch theme called LaunchPad by Ian Stewart. This has inspired me to focus my last plugin on what to do if you want to turn your blog off for a little while while you tinker with it.
This plugin uses two hooks that I have used before in this series, admin_menu, and template_redirect.
The action admin_menu triggers just in time to make modifications to the admin menu.
This link to the admin menu is created by the function in this plugin called ‘qnd_create_offline_notice_admin_toggle_menu’. This function calls the ‘add_submenu_page’ function that is part of WordPress which is what adds the actual link on the menu.
The link itself will point to another function ‘qnd_offline_notice_admin_toggle’ and it is this function that contains the admin page functionality.
It can seem a little complex at first so I have commented the plugin quite thoroughly.
The admin page function is split into two areas: at the top is the code that processes the commands, and the page content is beneath that. The page is fairly simple. It contains a label that explains what will happen if you press the button. When the button is pressed if the value of the option is 0, it gets changed to 1, and vice versa.
An important function that I am using is WordPress’s built in options handling that saves and retrieves information from the database.
get_option( ‘qnd_offline_notice’ ) This retrieves the data we have inserted (or false if there isn’t any yet)
update_option( ‘qnd_offline_notice’ , 1 ) This saves the value (in this case it is 1 ) against the name in the database so we can retrieve it later with get_option. WordPress takes care of the rest of the database interaction.
The second hook: template_redirect is a little simpler. It triggers before any pages within the template are chosen, so this plugin uses it to create a new page entirely and prevent any other pages being accessed.
The first thing the function does is get the option from the database, then it checks to make sure that a feed is not being requested, that the user is not logged in, and that we have set the option to 1, i.e. the offline notice is switched on.
The second of those checks is important. If you are logged on then you will see the blog as normal. This means you can tweak it however you want without exposing the content or the design to any outside users.
The plugin is shown below:
<?php /* Plugin Name: Quick n' Dirty Offline Notice Plugin URI: http://www.wp-fun.co.uk/2008/02/04/quick-n-dirty-offline-notice/ Description: Replaces user facing pages with a coming soon message Author: Andrew Rickmann Version: 1 Author URI: http://www.wp-fun.co.uk */ //This is the line that adds the action into the list. // 'admin_menu' and 'template_redirect' are the names of the actions // the second argument is the function that is called at that time add_filter( 'admin_menu' , 'qnd_create_offline_notice_admin_toggle_menu' ); add_action( 'template_redirect' , 'qnd_template_redirect_interception' ); function qnd_create_offline_notice_admin_toggle_menu(){ //this function only calls one function, the function that creates the submenu page add_submenu_page( 'options-general.php' , //the parent page that the submenu will be below 'Quick n\' Dirty Offline Notice' , //the page title 'Offline Notice' , //the title displayed on the menu 10 , //the security level 0 = lowest 10 = highest __FILE__ , //the file where the page can be found (this one) 'qnd_offline_notice_admin_toggle' ); // the function that outputs the page (below) } //this function will ouput the admin page function qnd_offline_notice_admin_toggle() { //get the option value from the database $off_line = get_option('qnd_offline_notice'); //check to see if the button was pressed if ( isset( $_POST['qnd_offline_notice_toggle'] ) ) { //if the value is 1 then we need to make it 0 if ( $off_line == 1 ) { update_option( 'qnd_offline_notice' , 0 ); //do the update $off_line = 0; //update this as we will used it later on the page } else { //otherwise update it to 1 update_option( 'qnd_offline_notice' , 1 ); //do the update $off_line = 1; //update this for later use } } //the actual page content can go below here ?> <div class="wrap"> <h2>Offline Notification</h2> <form method="post"> <p> <label for="qnd_offline_notice_toggle"><?php //choose the text to go in the label for the button if ( $off_line == 0 ) { //if the notice is turned off the button will turn it on echo 'Turn on the Offline Notice.'; } else { //if it is on then explain it will be turned off echo 'Turn off the Offline Notice.'; } ?></label><br /> <input type="submit" name="qnd_offline_notice_toggle" value="Do It!!!!" /> </p> </form> </div> <?php } //this function actually displays the page function qnd_template_redirect_interception(){ //get the option from the database $off_line = get_option('qnd_offline_notice'); //make sure a feed is not being requested //make sure the user is not logged in //make sure that the notice is turned on if ( !is_feed() && !is_user_logged_in() && $off_line == 1 ) { //if all that is true then we need to create a page to explain that the blog is offline ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>> <head profile="http://gmpg.org/xfn/11"> <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" /> <title><?php bloginfo('name'); ?></title> <style type="text/css"> body { margin:0; padding:0; background-color:#303030; color:#ccc; font-family:Arial, Helvetica, sans-serif; font-size:85%; line-height:1.5; } h1 { font-family:Georgia, "Times New Roman", Times, serif; font-weight:normal; font-size:500%; color:white; position:absolute; top:50%; margin-top:-150px; width:100%; text-align:center; } #strip { position:absolute; top:50%; height:100px; margin-top:-50px; width:100%; background-color:#0066CC; text-align:center; } </style> </head> <body> <h1>Offline</h1> <div id="strip"> <p><?php bloginfo('name'); ?> Is offline at the moment for some essential maintenence.</p> <p>In the meantime you might like to subscribe to my feed:</p> </div> </body> </html> <?php //stop any other template pages being loaded. exit; } } ?>
It isn’t as simple as the other plugins; however, it looks much more complicated than it really is. Most of the length is made up of two web pages so don’t let that put you off.
Note: If you copy the content of this plugin you will need to replace all the quote marks as WordPress replaces them with fancy ones.
- Permalink
- Andrew Rickmann
- 4 Feb 2008 7:23 PM
- Comments (0)