Create WordPress Plugin

Developing or writing a WordPress Plugin is very simple if you have a basic PHP knowledge. A single PHP file can be used as a WordPress Plugin having some structured information.

To create a PHP file, you can use notepad, or if you already have any advanced editor, that’s fine also.

Step 1: Creating plugin file:

Plugin file is just a simple PHP file. But containing some structured data at the top of the file, commented information in general. Plugin file name can be anything, but majority of the cases, it is a sanitized name of the plugin. Ex: if you are creating a plugin named “James Analytics”, the suitable name for the file would be “james-analytics.php”. But it’s not a required thing. The plugin data should be only on the plugin file, not all of the file that are used by the plugin.


Step 2: Adding plugin information to the file:

For each and every WordPress Plugin, the minimum information needed to be provided within plugin file is Plugin Name.

<?php
/**
 * Plugin Name: MyPlugin
 */
?>

Some more information can be added here such as plugin version, author name, but those are all optional information’s and can be left empty.

Now the basic structure is set, and it can be used as a Plugin.

The plugin doesn’t have any feature yet. So lets add something that will do act like a plugin and do something.


Let’s create an Email feature for your plugin, to send a notification email to the site administrator email address when you plugin is activated.

Send Email on WordPress Plugin Activation

On the above code, three native WordPress function has been used. You can learn about their usage from WordPress Codex Page, register_activation_hook, get_option & wp_mail .

We will add another method that will also send an email but only on deactivation.

Send Email on WordPress Plugin Deactivation

Now put everything together. It should look like this –

Next thing is to save the file and put it on plugins directory. By default, WP Plugins directory/folder is relative to your WordPress Setup. So if WordPress is setup is at public_html/, plugins directory will be at public_html/wp-content/plugins/ . Just upload or drop your file in that folder (no need to create a new folder for it).

Now, You should see your plugin listed besides other plugins of your site. Activating the plugin will send an email to the site administrator’s email and upon deactivation another email. Note: If you are testing this on localhost such as WAMP/XAMP etc, you wont actually get any email, as localserver doesn’t come with an email server by default.

So that’s a small, simple Plugin you have created. Next u might want to add more things to you plugin.


What’s should do Next ?

You could check another post that describe how you can capture user’s login time and save it for future reference – User’s Last login time. Additionally, you could also check the usage of add_action function in your plugin or theme.