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.
[code]<?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/
*/[/code]
Opening PHP tag - duh? <img src=\'http://www.killanet.net/forum3/public/s ... >/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.
[code]function add_plugin_page()
{
add_menu_page('My First Plugin', 'Plugin', '8', __FILE__, 'plugin_content');
}[/code]
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.
[code]function plugin_content() {
print "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque quam.";
}[/code]
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."
[code]add_action('admin_menu', 'add_plugin_page');[/code]
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.
[code]<?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]
Now for the code with some comments, but less than the full writeup provided here.
[code]<?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');
?>[/code]
Wordpress Plugin
Moderators: Moderator, Global Moderator
Wordpress Plugin
Love is all a matter of timing.
It's no good meeting the right person too soon or too late.
If I'd live in another time or place...
...my story might have had a very different ending.
It's no good meeting the right person too soon or too late.
If I'd live in another time or place...
...my story might have had a very different ending.
Wordpress Plugin
That simple, huh? Hmm... Ok so not as hard as I thought. lol
[align=center]

“If you stop learning, you stop living.” ~Tami Quiring
“It's the rare man who understands the value of a single perfect rose.”
[/align]

“If you stop learning, you stop living.” ~Tami Quiring
“It's the rare man who understands the value of a single perfect rose.”
[/align]
Wordpress Plugin
Since I was asked for them, these are the two resources I used to write this up...and, I'm guessing in the future that I will forget how to do this and need to relearn it. <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />
[url=\"http://markjaquith.wordpress.com/2006/0 ... wp-plugin/\"]http://markjaquith.wordpress.com/2006/0 ... wp-plugin/[/url]
[url=\"http://codex.wordpress.org/Main_Page\"]http://codex.wordpress.org/Main_Page[/url]
[url=\"http://markjaquith.wordpress.com/2006/0 ... wp-plugin/\"]http://markjaquith.wordpress.com/2006/0 ... wp-plugin/[/url]
[url=\"http://codex.wordpress.org/Main_Page\"]http://codex.wordpress.org/Main_Page[/url]
Last edited by Jim on Wed Aug 20, 2008 4:42 am, edited 1 time in total.
Love is all a matter of timing.
It's no good meeting the right person too soon or too late.
If I'd live in another time or place...
...my story might have had a very different ending.
It's no good meeting the right person too soon or too late.
If I'd live in another time or place...
...my story might have had a very different ending.
Wordpress Plugin
The codex just confuses me... Just like when the mIRC manual confused me, and the PHP manual confused me... Maybe I'll grasp on to it some day <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />
[align=center]

“If you stop learning, you stop living.” ~Tami Quiring
“It's the rare man who understands the value of a single perfect rose.”
[/align]

“If you stop learning, you stop living.” ~Tami Quiring
“It's the rare man who understands the value of a single perfect rose.”
[/align]
Wordpress Plugin
Okay, maybe I missed something but for some reason this plugin does not add the "My First Plugin" to the title bar...anyone know why?
Love is all a matter of timing.
It's no good meeting the right person too soon or too late.
If I'd live in another time or place...
...my story might have had a very different ending.
It's no good meeting the right person too soon or too late.
If I'd live in another time or place...
...my story might have had a very different ending.
Wordpress Plugin
[quote name=\'Jim\' post=\'44503\' date=\'Aug 21 2008, 08:30 PM\']Okay, maybe I missed something but for some reason this plugin does not add the "My First Plugin" to the title bar...anyone know why?[/quote]
Looks like an actual bug. <img src=\'http://www.killanet.net/forum3/public/s ... #>/dry.gif\' class=\'bbc_emoticon\' alt=\'<_<\' />
To be honest pretty much all plugins use a submenu in either the settings or plugins tab. The function is similar - add_submenu_page - and the first parameter is the parent page, something along the lines of:
[code]function add_plugin_page()
{
add_submenu_page('plugins.php', 'My First Plugin', 'Plugin', '8', __FILE__, 'plugin_content');
}[/code]
<img src=\'http://www.killanet.net/forum3/public/s ... /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />
Looks like an actual bug. <img src=\'http://www.killanet.net/forum3/public/s ... #>/dry.gif\' class=\'bbc_emoticon\' alt=\'<_<\' />
To be honest pretty much all plugins use a submenu in either the settings or plugins tab. The function is similar - add_submenu_page - and the first parameter is the parent page, something along the lines of:
[code]function add_plugin_page()
{
add_submenu_page('plugins.php', 'My First Plugin', 'Plugin', '8', __FILE__, 'plugin_content');
}[/code]
<img src=\'http://www.killanet.net/forum3/public/s ... /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />
Wordpress Plugin
I'll see if I can't fix it, shouldn't be too hard...right?
edit: here's the code for the add_menu_page() funtion, from the /wp-admin/includes/plugin.php file
[code]function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
global $menu, $admin_page_hooks;
$file = plugin_basename( $file );
$menu[] = array ( $menu_title, $access_level, $file, $page_title );
$admin_page_hooks[$file] = sanitize_title( $menu_title );
$hookname = get_plugin_page_hookname( $file, '' );
if (!empty ( $function ) && !empty ( $hookname ))
add_action( $hookname, $function );
return $hookname;
}[/code]
edit: here's the code for the add_menu_page() funtion, from the /wp-admin/includes/plugin.php file
[code]function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
global $menu, $admin_page_hooks;
$file = plugin_basename( $file );
$menu[] = array ( $menu_title, $access_level, $file, $page_title );
$admin_page_hooks[$file] = sanitize_title( $menu_title );
$hookname = get_plugin_page_hookname( $file, '' );
if (!empty ( $function ) && !empty ( $hookname ))
add_action( $hookname, $function );
return $hookname;
}[/code]
Last edited by Jim on Thu Aug 21, 2008 10:38 pm, edited 1 time in total.
Love is all a matter of timing.
It's no good meeting the right person too soon or too late.
If I'd live in another time or place...
...my story might have had a very different ending.
It's no good meeting the right person too soon or too late.
If I'd live in another time or place...
...my story might have had a very different ending.
Wordpress Plugin
Feel free but in general plugins shouldn't use the top level menu like that. If they were to do so there should be submenu items which would mean the page title would never be seen anyway.
Last edited by Scott on Fri Aug 22, 2008 10:47 am, edited 1 time in total.

