February 4, 2008

Quick N Dirty

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
?>

Offline Notification

}

//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
?>

>


Offline

Is offline at the moment for some essential maintenence.

In the meantime you might like to subscribe to my feed:



//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.



Wordpress feature image page?
24 comments on page 1065

Addsettingsfield?
3 comments on page 793

Shorter link?
4 comments on page 1190

Shorter link?
4 comments on page 1190

Cssimageforlogin?
no comment on page 128

Cssimageforlogin?
no comment on page 128

Get featured image url wordpress?
24 comments on page 1065

How to clear wpcache?
9 comments on page 1310

Photo gallry code html5?
6 comments on page 1305

Wordpress get image size?
24 comments on page 1065

Sitecouk wptheme?
2 comments on page 1376

Wordpress chat disqus?
no comment on page 1308

Howtocreategoodsizelayout?
4 comments on page 1026

Friend connect profile?
6 comments on page 1364

Get medium image from post?
24 comments on page 1065

Tabbed widgets wp?
no comment on page 333

Wordpress pictue cache lschen?
9 comments on page 1310

Wordpress plugin post separator?
no comment on page 1371

Display photos html 5?
6 comments on page 1305

Fun wordpress themes?
2 comments on page 1376

Wp featured image without size?
24 comments on page 1065

Wordpress get url global?
4 comments on page 1190

Wp unlimited?
3 comments on page 1141

Html5 image gallery?
6 comments on page 1305

Wordpress exif plugin?
12 comments on page 230

Css login screen?
no comment on page 128

Htlm 5 photo gallery?
6 comments on page 1305

Css login screen?
no comment on page 128

Comments by intensedebate?
7 comments on page 1175

Image gallery html 5?
6 comments on page 1305

Wordpress andrew rickmann?
2 comments on page 1376

Wordpress medium large?
24 comments on page 1065

Fun with uninstallation wordpress?
53 comments on page 100

How to clear wp cache?
9 comments on page 1310

Photoshop frameworks?
3 comments on page 296

Html5 gallery?
6 comments on page 1305

Html 5 photo gallery?
6 comments on page 1305

Login screen css template?
no comment on page 128

Html5 photo gallery code?
6 comments on page 1305

Wordpress post multiple images?
24 comments on page 1065
  every 1995s, 1s ago, in 0.03s.
Post a comment?

0s