CMS - Wordpress - Coding - PHP

WordPress Plugin Development, Lesson 2: The Admin Page

WordPress is an incredibly popular and widespread content management system, used by over 60 million websites and blogs. It has its downsides, like any software package, but one of its strengths is its use of plugins, pieces of additional code that add to the core code of WordPress without altering it. This is an intermediate lesson in WP plugin development.

This tutorial will expand on the concepts and code from the Introduction to WordPress Plugin Development article, so if you don’t have a basic foundation in WordPress and PHP, I’d strongly recommend doing that one first. It’s a pretty simple start, and it won’t take long.

Base Code and Setup

Rather than start from scratch, let’s take over right where we left off from the introductory lesson. If you are coming from that lesson, just open up your final readtime.php file. If not, here’s the simple plugin we developed in that introductory tutorial. Create a new file in your wp-content/plugins/ directory, and call it readtime.php:


 * @package WP Read Time Plugin
 * @version 1.0
Plugin Name: Read Time
Plugin URI: 
Description: Plugin Example for WordPress
Author: Your Name Here
Version: 1.0
Author URI: 

// Functions
function readtime ($content) {

  global $wpdb;
  global $post;

  $getpost = $wpdb->get_var($wpdb->prepare(
      "SELECT post_content FROM wp_posts WHERE ID = %d", $post->ID));

  $wordcount = str_word_count($getpost);
  $minutestoread = $wordcount / 250;

  $newcontent = "This post has ".$wordcount." words. 
      It will take the average reader ".$minutestoread." minutes.
      <br /><br />".$content;
  return $newcontent;

// Hooks
add_filter( 'the_content', 'readtime' );


This plugin attaches a section of text to the beginning of each post on any site where it is installed, telling the reader the word count and estimated time to read each article. That’s a useful, but pretty simple and inflexible, function. We’re going to make it do much more.

The Bare Bones Admin Screen

For simple plugins like our original ReadTime, a single file and function is all you need. But as your plugins get more advanced and you want administrators to have more control over their functions, you’re going to need to create an admin page. Luckily, WordPress makes this a pretty painless process.

First, put your readtime.php file into a new folder, called “ReadTime”, in your wp-content/plugins/ directory. Add a second file to that new folder, and call it rt_admin.php. Open it and paste in the following code:


  function rt_admin_page() {
    echo '<h2>WP ReadTime Options</h2><br />';

  function rt_admin_menu() {
    add_menu_page('WP_ReadTime', 'WP ReadTime', 'manage_options', 
      'rt_admin', 'rt_admin_page', '', 3);


All the first function does is create an H2 header. The second is a little more complicated. WordPress’ add_menu_page() function creates an admin page in the WordPress dashboard. There are seven arguments included in the function: the page title, menu title, the user capability required to access this page, a unique menu slug, the function that will populate the page, the icon that will be used next to the menu item (I’ve left this one blank), and the order in which it will appear in the menu. As you can see, it’s a complex function and an entire tutorial could be written specifically on it. Instead, if you have questions on add_menu_page(), you should check out its page on the WordPress Codex.

If you open up your WordPress dashboard now, you won’t see anything different just yet. We still have to integrate this new file into the main plugin. To do so, open up your readtime.php file again and add two pieces of code. First, at the top of the file, before the // Functions comment line, include the new file:


Then, at the bottom of the file, right before the closing ?> PHP tag, add this line to let WordPress know when to display the new admin menu item in your dashboard:

  add_action('admin_menu', 'rt_admin_menu');

If you open up your WordPress dashboard now, you should see something like this at the top of the left-side menu:


Adding Functionality

We now have a working, but empty, admin page. Let’s add some meat to our rt_admin_page() function. Open up your rt_admin.php file and replace the existing function with this more complete one:

  function rt_admin_page() {

    $textcolor = $_POST['rt_admin_color'];
    if ($textcolor) { 
      update_option('rt_admin_color', $textcolor);
      echo '<strong>Plugin text color updated.</strong> <br /> <br />';

    echo '<h2>WP ReadTime Options</h2><br />';
    echo '<form method="POST">
      Color of plugin text: <select name="rt_admin_color">
      <option value="#000">Black</option>
      <option value="#00F">Blue</option>
      <option value="#F00">Red</option>
      </select> <input type="submit" /> </form>';

If this is your first exposure to using PHP to process forms, check out my earlier post “PHP Basics: Forms”. What we’ve done here is create a simple form for changing the text color of the word count and estimated reading time text that the plugin adds to each of your site’s posts. In order to make this work now, all we need to do is make a quick alteration to readtime.php, so that the code checks whether a color has been specified before displaying the text. In readtime.php, replace the declaration of the $newcontent variable (lines 28-30) with this code:

  $textcolor = get_option('rt_admin_color');
  $newcontent = "This post has ".$wordcount." words. 
      It will take the average reader ".$minutestoread." minutes.

"; if ($textcolor) { $newcontent = '' .$newcontent.''.$content; }

Now, change the color in your dashboard admin page, and open up a post on your site to see the results.

This may seem like a hugely more complex tutorial than the introduction to WordPress plugin development, but we’re still just scratching the surface. We’ve created an admin page to control details of the plugin and one function as an example. Play around with the code and add more controls. As always, take what we’ve done here and experiment on your own. Happy coding!

About the author

Ian Rose is a web developer, blogger, and writer living in Portland, OR. In his day job, he develops WordPress plugins and custom PHP solutions, focusing on nonprofit clients. By night, he attempts to write both fiction and nonfiction. Ian's site is Seaworthy Web Solutions

Share this post

Leave a Comment

Subscribe To Our Newsletter