How to write a WordPress Plugin?

Before we move on coding a plugin, please make sure you remember the following coding practices.

1. Always you chose a unique name to your plugin so that it doesnt collide with names used in other plugins.

2. Make sure you comment wherever and whenever necessary in the code.

3. You will to test the plugin in your localhost (using xampp) along with latest version of wordpress.
Plugin Files & Names

Assigning unique names, documenting and organizing the plugin files is very important part of plugin creation.

Although wordpress allows you to place the plugin php file directly into the wp-content/plugins folder, for a good plugin developer you will need to create a folder named hello-world and within place readme.txt and hello-world.php.

The readme.txt contains information about your plugin and can come in handy when you submit your plugin wordpress SVN plugin repository. See the readme sample.

Go ahead and create these files and we will add the content to these files later.
The Plugin Basics

The heart of a wordpress plugins is the below 2 functions (commonly called `hooks`)

add_action ($tag, $func) documentation
add_filter ($tag,$func) documentation

It is very important to know the difference between the above functions.
add_action –> does an action at various points of wordpress execution
add_filter –> does filtering the data (eg. escaping quotes before mysql insert, or during output to browser.

Plugin Information

Open your hello-world.php and in the first line, add this commented plugin information to your file.

Save this php file,
Place the plugin folder to wordpress > wp-content > plugins,
Go to your wordpress admin > plugins and you will see the new plugin listed, waiting to get activated.


Post a Comment