How to Build a WordPress Plugin in 10 Minutes

Description:

A tutorial demonstrating how to create a basic WordPress plugin, including a Graphical User Interface (GUI) in the WordPress admin area.

Who this Tutorial is For:

Programmers who have at least a little experience with WordPress and a solid grasp of PHP.

WordPress is a wonderful web site and application platform that has become a real standard, thanks to the dedicated support given by the WordPress community. With thousands of plugins available that allow developers to add functionality, WordPress is ideal for many web solutions.

If you are a programmer and you have some knowledge of PHP, why not try creating your first WordPress plugin? It’s easy and this tutorial will show you how to get started.

First: Decide on the name of your plugin and create a plugin folder.

If you are planning on distributing your plugin to the public, you will want a unique name, so that your plugin files will not clash with other plugins that might share the same name. So, you may want to make it a combination of your business or developer / design-firm name or ‘handle’, along with a description of the functionality. In our example, I will make a plugin called “php-heaven-first-plugin”.

So I will now make a folder called “php-heaven-first-plugin”. This directory will go into the wp-content/plugins/ directory.

Second: Make an index.php file within your folder and add the plugin description code

The index.php file will be the ‘main’ execution point of your plugin script, and inside of this, you can do all the fancy functionality that you plan to include in your mini-application. But first you have to get it recognized by WordPress. Here is a sample of the minimum code you need, at the very top of your index.php file, just after the <?php

<?php
/*
Plugin name: PHP Heaven First Plugin
Version: 1.0
Description: Basic plugin setup, and some GUI.
Author: PHP Heaven
Author URI: http://php.heaveninteractive.com
Plugin URI: http://php.heaveninteractive.com
*/
?>

Of course, you will want to substitute your own values for each configuration item.

What this Code Does

The above code tells WordPress the name, version, and other values, which will then be displayed in the WordPress admin area. If you are going to support your plugin, you should put your support URL in the plugin URI section. This will help the users find the exact support they need immediately.

That’s it. Congratulations, you have a plugin. You can now click ‘activate’, and WordPress will set your plugin to active in its database.

What to do next? Well there are many, many types of plugins, some that have a GUI (graphical user interface), some that just work with template output, some that just hook onto admin functions. Let’s consider a plugin that’s going to have a GUI that only admin users can access. Let’s create this admin page next.

Third: Create an Admin Page

There are different kinds of admin menus, and you can find a detailed explanation of them here. For our plugins, we like to make them have their own section in the left-hand menu in the WP admin, because then we can have sub-menus, and our plugin area can be collapsed when not in use.

Add the following code, below your configuration area:

add_action('admin_menu', 'basicPluginMenu');
function basicPluginMenu() {
$appName = 'Basic Plugin';
$appID = 'basic-plugin';
add_menu_page($appName, $appName, 'administrator', $appID, 'pluginAdminScreen');
}
function pluginAdminScreen() {
echo "<h1>The Basic Plugin Admin Area</h1>";
echo "<p>Here is all the plugin GUI goodness</p>";
}

What this Code Does

The first function call, add_action(), tells WP to setup an administration menu, and that to setup this plugin, it should use the function basicPluginMenu().

We then create the basicPluginMenu() function, and inside of it, we call the add_menu_page(), passing it several arguments (page_title, menu_title,capability,slug,function). The function that will actually render the menu is the pluginAdminScreen() function, which we have passed the name of in the last argument.

The pluginAdminScreen() function just uses echo statements to send some HTML to the screen, and of course we can use whatever HTML we want here.

Conclusion: Get Writing!

So there you go, a basic tutorial on setting up a plugin, with a GUI in the admin. As you can see, it’s not difficult to get a plugin going, and once you get the hang of it, you’ll be off and running, creating the next great and successful WP plugin, used by millions of websites!

Additional Resources

If you are really serious about plugin development, we recommend that you get some good reference and study guides to help you. Although there is documentation on-line for developing plug-ins, you will need a deep understanding of the WP ecosystem, including template tags, and normal (non-template-based) functionality, constants, hooks, and so forth, not to mention query variables, posts, and loops.

Web Pages

The function reference in the WordPress codex is a great place to start, including the informative page on administration menus.

Reading is Fundamental

One good book that this author read and personally recommends, is the WordPress Plugin Development Beginner’s Guide. A plugin is basically an application that you create and build, and learning to do so necessitates some focus and concentration. This book gives you a ton of knowledge on the fundamentals, including widgets and using AJAX, which becomes very important when you need to make custom plugin functionality for your development clients or your own business use.  Long story short, books can be better than websites when you really want to learn how to make applications!

Happy plugin-making! :)

http://php.heaveninteractive.com/php-tutorials/wordpress-php-tutorials/create-your-first-wordpress-plugin-in-10-minutes/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s