Home > Wordpress > A Minimal Plugin for WordPress

A Minimal Plugin for WordPress

September 22nd, 2007

I recently found a need to access data held in a WordPress database. Although I could have written code that accessed the database directly, I decided to write a plugin so that I could use the in-built WordPress functions.

Writing a WordPress plugin is fairly straightforward and I would recommend reading information provided at wordpress.org for writing plugins

http://codex.wordpress.org/Writing_a_Plugin
http://codex.wordpress.org/Function_Reference

However, if it’s of any interest, here are my efforts at creating a minimal plugin. A minimal plugin is presented which can be re-used, essentially as a starting point, for creating various different plugins, and is the minimum structure required when creating a plugin for WordPress.

I created a file, called minimal_plugin.php, containing the following code and placed it in my wordpress plugins directory. (wordpress/wp-content/plugins). The plugin, called Minimal Plugin is then available in the list of plugins:

Minimal Plugin

When this plugin is activated, a menu item Minimal Plugin is added to the WordPress Dashboard:

Dashboard

The code consists of 2 functions

function minimal_addMenu() is used to add a menu item and is called by

  1. add_action(‘admin_menu’, ‘minimal_addMenu’);

which adds the menu item to the dashboard admin page.

function minimal_adminMenu() defines what is displayed on the page under the Minimal Plugin tab. This can also used to call available internal WordPress functions that allow you to access details available in the database.

In addition, information is used from the comments section

  1. /*
  2. Plugin Name: Minimal Plugin
  3. Plugin URI: http://somewhere/
  4. Description: Minimal Plugin
  5. Author: Minimal
  6. Version: 0.0.1
  7. Author URI: http://somewhere/
  8. */

Here is the complete code listing

  1. <?php
  2. /*
  3. Plugin Name: Minimal Plugin
  4. Plugin URI: http://somewhere/
  5. Description: Minimal Plugin
  6. Author: Minimal
  7. Version: 0.0.1
  8. Author URI: http://somewhere/
  9.  
  10. Last update: 09/20/07 (09/20/07)
  11.  
  12. Functions:
  13. minimal_adminMenu: where all the work is done
  14.  
  15. Copyright 2006-2007 Minimal
  16.  
  17.     This program is free software; you can redistribute it and/or modify
  18.     it under the terms of the GNU General Public License as published by
  19.     the Free Software Foundation; either version 2 of the License, or
  20.     (at your option) any later version.
  21.  
  22.     This program is distributed in the hope that it will be useful,
  23.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25.     GNU General Public License for more details.
  26.  
  27.     You should have received a copy of the GNU General Public License
  28.     along with this program; if not, write to the Free Software
  29.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  30. */
  31. $minimal_version = ‘v0.0.1′; // the current version
  32.  
  33. // more details at …
  34. // http://codex.wordpress.org/Writing_a_Plugin
  35. // http://codex.wordpress.org/Function_Reference
  36.  
  37.  
  38. function minimal_addMenu()
  39. {
  40.         if ( function_exists(‘add_submenu_page’) )
  41.         {
  42.                 add_submenu_page(‘index.php’, ‘Minimal Plugin Template’, ‘Minimal Plugin’, 1, basename(__FILE__),‘minimal_adminMenu’);
  43.         }
  44. }
  45.  
  46. function minimal_adminMenu()
  47. {
  48. global $minimal_version;
  49.         echo(‘<div class="wrap">’);
  50.         echo("<h2>Minimal Plugin – version $minimal_version</h2><br /><br />");
  51.         echo(‘</div>’);
  52. }
  53.  
  54. add_action(‘admin_menu’, ‘minimal_addMenu’);
  55.  
  56. ?>

Related Posts:

  1. Category Cloud
  2. Viewing 3D Models in IE7
  3. Blender 2.37 is released
  4. Free Download Manager
  5. PicLens
  6. Adding a code snippet
  7. Blender 2.41
  8. Maxwell and Paintshop
  9. Blender 2.42

Categories: Wordpress Tags:
  1. August 15th, 2008 at 06:54 | #1

    Nice article. thanks

Comments are closed.