Wordpress Plugin
Posted: Thu Aug 07, 2008 4:23 pm
Creating plugins for WordPress can be a bit confusing at first; especially ones with top level menus in the admin panel. Here is the basic layout with an explanation for each part of the code. I will put the full code at the end, with no comments in it and with comments in it.
Opening PHP tag - duh? /blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />
The next part is a comment block required by WordPress to let WordPress know what your plugin is and to create that nifty activate/deactivate row in the plugin management table. The only "required" field is the plugin name, however, these are the most basic fields and placing all of them in the comment block is helpful to other people. If not to yourself at a later date when you wonder what a plugin does or give it to someone else and they need to contact you.
This part is a bit more tricky but I'll try to explain it.
"function add_plugin_page()" is a function who's sole purpose is to call the "add_menu_page" function. The first field in the "add_menu_page()" function is the text that WordPress will append to the title in the title bar. Next is the text that will appear in the actual menu on the admin page; that you click to view your plugin. The third field is the user level which is able to use your plugin, with "0" being the lowest and "10" being the WordPress Blog Admins. Fourth is the file name which contains the function which WordPress should run when it initiates the script, in this case we use PHP's "__FILE__" to signify that the current file should be use. Lastly we have the name of the function that WordPress needs to call in order to display the content of your plugin. This last field is not necessary and if you do not include it, WordPress simply assumes that by including the file, the content will be either echoed or printed out.
If you remember from the last little snippet of code, when WordPress loads the plugin, it will call the "plugin_content" function. This function is where you would place any and all of the code that you want to appear on your page. Variables, session_start();, MySQL calls and the display of data. Since this is just an outline, we will simply print out "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque quam."
Of all the code that I have put up here, this is probably some of the most important. The "add_action" function will add the "add_plugin"page" sink into the plugin hook list for the "admin_menu"; or in English, the "add_action" function adds the "add_plugin_page" to the "admin_menu" so that the "add_menu_page" function you just created will be called by WordPress when you go to the Admin page and the menu loads.
As promised, here is the full, uncommented code (minus of course, the WordPress required comments. that you can use as a skeleton or for anything you wish.
Now for the code with some comments, but less than the full writeup provided here.
Code: Select all
<?php
/*
Plugin Name: My First Plugin
Plugin URI: http://jim.killanet.net/blog/
Version: 0.1
Description: This is the first plugin that I have made for WordPress
Author: Jim Anderson
Author URI: http://jim.killanet.net/blog/
*/The next part is a comment block required by WordPress to let WordPress know what your plugin is and to create that nifty activate/deactivate row in the plugin management table. The only "required" field is the plugin name, however, these are the most basic fields and placing all of them in the comment block is helpful to other people. If not to yourself at a later date when you wonder what a plugin does or give it to someone else and they need to contact you.
Code: Select all
function add_plugin_page()
{
add_menu_page('My First Plugin', 'Plugin', '8', __FILE__, 'plugin_content');
}"function add_plugin_page()" is a function who's sole purpose is to call the "add_menu_page" function. The first field in the "add_menu_page()" function is the text that WordPress will append to the title in the title bar. Next is the text that will appear in the actual menu on the admin page; that you click to view your plugin. The third field is the user level which is able to use your plugin, with "0" being the lowest and "10" being the WordPress Blog Admins. Fourth is the file name which contains the function which WordPress should run when it initiates the script, in this case we use PHP's "__FILE__" to signify that the current file should be use. Lastly we have the name of the function that WordPress needs to call in order to display the content of your plugin. This last field is not necessary and if you do not include it, WordPress simply assumes that by including the file, the content will be either echoed or printed out.
Code: Select all
function plugin_content() {
print "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque quam.";
}Code: Select all
add_action('admin_menu', 'add_plugin_page');As promised, here is the full, uncommented code (minus of course, the WordPress required comments. that you can use as a skeleton or for anything you wish.
Code: Select all
<?php
/*
Plugin Name: My First Plugin
Plugin URI: http://jim.killanet.net/blog/
Version: 0.1
Description: This is the first plugin that I have made for WordPress
Author: Jim Anderson
Author URI: http://jim.killanet.net/blog/
*/
function add_plugin_page()
{
add_menu_page('My First Plugin', 'Plugin', '8', __FILE__, 'plugin_content');
}
function plugin_content() {
print "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque quam.";
}
add_action('admin_menu', 'add_plugin_page');
?>Code: Select all
<?php
/*
Plugin Name: My First Plugin
Plugin URI: http://jim.killanet.net/blog/
Version: 0.1
Description: This is the first plugin that I have made for WordPress
Author: Jim Anderson
Author URI: http://jim.killanet.net/blog/
*/
/*
Function for adding a new page to the menu.
*/
function add_plugin_page()
{
add_menu_page('My First Plugin', 'Plugin', '8', __FILE__, 'plugin_content');
}
/*
Function that displays any and all of the content that is to be displayed in our plugin.
*/
function plugin_content() {
print "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque quam.";
}
/*
Function for adding the "add_plugin_page" sink to the admin menu hook
*/
add_action('admin_menu', 'add_plugin_page');
?>