WordPress is built so that when you create a plugin, you can create a link to the plugin’s management page within the existing WordPress admin menu. The instructions for adding this link in Posts, Pages, Tools and Settings are clear. However, instructions for adding that link just under the Dashboard was hard to find. With some experimentation, I figured it out.
Here’s what the function needs to look like:
function modify_menu_for_display_date() {
add_dashboard_page(
'Set Drop-off Date', // Title on menu
'Set Drop-off Date', // title on page
1, // min. user level
dirname(__FILE__), // File to open
'display_date_options' // Function to call
);
}
For those of you who are interested, here’s the complete plugin. It’s an extremely simple one. This client has a “next drop-off date” that changes monthly and is displayed in four places on their site. I didn’t want them to have to remember to update the site in four places. They can update the date in once place via this plugin, and then use a shortcode [ahs_nextdate] to display that date anywhere on the site.
/*
Plugin Name: FtoF Next Dropoff Date
Description: This custom plugin allows you to keep your next Drop-Off Date current aross the whole site.
Version: 1.0
Author: April Hodge Silver
Author URI: http://springthistle.com
*/
function set_display_rate_options() {
add_option("ahs_nextdate","");
}
function unset_display_rate_options () {
delete_option("ahs_nextdate");
}
function modify_menu_for_display_date() {
add_dashboard_page(
'Set Drop-off Date',
'Set Drop-off Date',
1,
dirname(__FILE__),
'display_date_options'
);
}
function display_date_options () {
echo '
Set Drop-off Date
';
if ($_REQUEST['submit']) update_display_date_options();
print_display_date_form();
echo '
';
}
function update_display_date_options() {
$updated = false;
if ($_REQUEST['ahs_nextdate']) { update_option('ahs_nextdate', $_REQUEST['ahs_nextdate']); $updated = true; }
if ($updated) {
echo '
';
echo '
Dropoff date successfully updated!
';
echo '
';
} else {
echo '
';
echo '
Unable to update date!
';
echo '
';
}
}
function print_display_date_form () {
$val_ahs_nextdate = get_option('ahs_nextdate');
echo <<
This must be in this format: YYYY-MM-DD
EOF;
}
// shortcode for displaying the date in any page or post
function ahs_nextdate_handler($atts) {
$date = get_option('ahs_nextdate');
list($y,$m,$d) = split('-',$date);
$date = date('l, F j',mktime(0,0,0,$m,$d,$y));
return $date;
}
// function that displays the date in a special way for the homepage
// this function is avaialble to any template page, but only used on the homepage
function spit_datebox() {
$date = get_option('ahs_nextdate');
list($y,$m,$d) = split('-',$date);
echo '
'.date("M",mktime(0, 0, 0, $m, $d, $y)).'
'.abs($d).'
Next Drop-off Date:
'.date("F jS, Y",mktime(0, 0, 0, $m, $d, $y)).'
';
}
add_shortcode('ahs_nextdate', 'ahs_nextdate_handler');
add_action('admin_menu','modify_menu_for_display_date');
register_activation_hook(__FILE__,"set_display_date_options");
register_deactivation_hook(__FILE__,"unset_display_date_options");
Once the client’s new site is live, you’ll be able to get to it, and see this plugin in action, via the portfolio, though of course you can’t see the result in the WP Admin.