888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

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.

Using Your Own Url Shortener
Short url?
4 comments
page 1190
Why I Ditched Disqus
Styling disqus widgets?
5 comments
page 1175
Wordpress 25 Exif Fields
Wordpress exif?
12 comments
page 230
Html 5 Gallery
Html 5 gallery?
6 comments
page 1305
Dont Mess With My Toot Toot
Fun with toots?
16 comments
page 599
Dont Mess With My Toot Toot
Fun with toots?
16 comments
page 599
Converting Wordpress Themes To Habari
Habari timthumb?
one comment
page 694
Wordpress 25 Exif Fields
Habari timthumb?
12 comments
page 230
Fun With Sidebar Tabs Styling
Tabs with html css on same page javascript?
2 comments
page 336
Using Your Own Url Shortener
How to have own url short?
4 comments
page 1190
Post Image The Easy Peasy Way
Insert conditional image php wp?
26 comments
page 1065
Using Your Own Url Shortener
Run short url using htaccess?
4 comments
page 1190
How To Add Sidebars To A Theme
Wordpress sidebar above main sidebars?
11 comments
page 1053
Dont Mess With My Toot Toot
New posttype not displayed wordpress?
16 comments
page 599
Quick N Dirty Replacement Text
Wpupdatepost object?
no comment
page 122
Html 5 Gallery
Html5 simple wordpress theme?
6 comments
page 1305
Updating Code Snippets Here
Fun wordpress plugin?
one comment
page 1338
How To Add Sidebars To A Theme
Wp register sidebars?
11 comments
page 1053
Html 5 Gallery
Html5 wordpress theme?
6 comments
page 1305
Post Image The Easy Peasy Way
Get post images?
26 comments
page 1065
Updating Code Snippets Here
Wordpress fun plugins?
one comment
page 1338
Post Image The Easy Peasy Way
Wordpress get first image large?
26 comments
page 1065
Using Wordpress As A Php Framework
Wordpress create your own framework?
2 comments
page 335
Improve Your Typography With Plugins
Wordpress typography plugin?
one comment
page 721
Quick N Dirty Replacement Text
Dirty replacement?
no comment
page 122
How To Add Sidebars To A Theme
How to add pages in footer wordpress?
11 comments
page 1053
Post Image The Easy Peasy Way
How to post all gallery images in one post wordpress?
26 comments
page 1065
Post Image The Easy Peasy Way
Get attachment by post?
26 comments
page 1065
Html 5 Gallery
Html 5 tab?
6 comments
page 1305
Wordpress 25 Exif Fields
Exif info display wordpress?
12 comments
page 230
Using Your Own Url Shortener
Tiny urls htaccess?
4 comments
page 1190
Six Million Ways To Die Choose One
6 million ways to die so i chose?
14 comments
page 1128
How To Add Sidebars To A Theme
Wp register sidebars?
11 comments
page 1053
  1 query every 1455 seconds, updated 1 seconds ago.
Post a comment?