Press enter to see results or esc to cancel.

How to Create Your Own WordPress Plugin

wordpress pluginOne of the biggest selling points for WordPress is the support of plugins, utilities that greatly increase the function and usability of any WordPress site. From plugins that add directories and link libraries to those that greater admin function and automating of tasks, plugins are absolutely indispensable. But what do you do when you want to write something customized for your site, or maybe you have the next great idea and want to share it with the world (for free or as a premium for pay). It may seem like a huge task to undertake, but you don’t have to let it intimidate you. With an understanding of the basics and a little guidance, you too can write your own plugin.

  1. Get Everything in Place

Whenever possible, you should develop and write in a local environment. It saves time and avoids any mistakes that could end up online that you miss. You can run something the WAMP, MAMP, or XAMPP– just to name a few of the bigger ones. Next, you will have to download WordPress and install it to your local server’s file directory. Be sure to create a subdirectory for your new site where you will copy the WordPress files. Finally, create a new folder in your wp-content/plugins/ folder and name it whatever simplified version of the name for your plugin.

  1. Create Your Plugin Header

Once everything is in place, create a new file that is again a simplified version of the name of the plugin; it’s just easier that way. You will then create the header for your plugin at the top of this page. This is how WordPress knows that it is a plugin, and gives it the info WP needs to display all of the pertinent data on the plugins page. Here is one sample:

<?php

    /*

    Plugin Name: Your Full Name for the Plugin

    Plugin URI: URL For the Plugin

    Description: Description that will show up on the plugin page

    Author: Your Name

    Version: Version Number

    Author URI: Your Personal or Business URL

    */

?>

  1. Adding Your Plugin to the Page

Plugin functions are called on a page by the use of an “action hook”. This is what allows the function to operate. Adding it is really quite simple. For instance, if you need something to be called when the footer is called you would simply create a hook that calls your plugin’s footer function.

<php add_action(‘wp_footer’, ‘plugin_footer’); ?>

Using these hooks is how we will make the admin page for the plugin.

  1. Configuring the Admin Page

In order to use your plugin, you will need an admin page to configure the settings. To do that, you will need to configure a few parameters. First, your database settings. You will need your database host address, database name, database username, and database password. You set all of these up when you installed your WordPress set up. If you were setting up an eCommerce page you would also want to include the URL for your store and a folder director for any images.

In order to create the sub menu for your plugin, you will need to implement an action hook. To do that you will add something similar to this:

function oscimp_admin_actions() {

    add_options_page(“Full Plugin Name”, “Plugin Name”, 1, “Plugin Name”, “pluginname_admin”);

}

 add_action(‘admin_menu’, pluginname_admin_actions’);

You will have to give it something to pull up as a page for that function. So create a new file called pluginname_import_admin.php and add the following code above the code we just added.

function pluginname_admin() {

    include(pluginname_import_admin.php’);

}

If you click on that option in the menu you will just pull up a blank page. But that’s good because the file is empty- we just want to make sure everything is functioning basically.

From here on out, you will be adding the nuts and bolts of your page, setting up the forms and options for your plugin. If you are familiar with regular HTML, this shouldn’t be difficult. We won’t go into everything here, but you can find more information and WordPress coding standards, as well as a lot of documentation here. You can also get a better look at how some plugins were built, as well as a place to upload your plugin at the WordPress repository.

Comments

Comments are disabled for this post