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

    // register_activation_hook function is triggered when this plugin is activated
    // from the WP admin -> Plugins page.
    register_activation_hook(__FILE__, 'myplugin_activated');

function myplugin_activated()
{
    // Email address where mail will be sent
    // get_option is a native WordPress function to get settings 
    // from WordPress options table
    $to_email = get_option('admin_email');

    // mail subject
    $subject = 'MyPlugin has been activated';

    // body of the email
    $content = 'Greetings, MyPlugin is activated and ready to use. Thank you for using MyPlugin.';

    // wp_mail is a native WordPress plug-able function used to send mail
    wp_mail( $to_email, $subject, $content );
}

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

// register_deactivation_hook function is triggered when a plugin is deactivated.
    register_deactivation_hook(__FILE__, 'myplugin_deactivated');

function myplugin_deactivated()
{
    $to_email = get_option('admin_email');
    $subject = 'MyPlugin has been deactivated';
    $content = 'MyPlugin has been deactivated. You can again activate it from your WordPress Admin Plugin Management page';

    wp_mail( $to_email, $subject, $content );
}

Now put everything together. It should look like this –

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

<?php

    register_activation_hook(__FILE__, 'myplugin_activated');

function myplugin_activated()
{
    $to_email = get_option('admin_email');
    $subject = 'MyPlugin has been activated';
    $content = 'Greetings, MyPlugin is activated and ready to use. Thank you for using MyPlugin.';
    wp_mail( $to_email, $subject, $content );
}

    register_deactivation_hook(__FILE__, 'myplugin_deactivated');

function myplugin_deactivated()
{
    $to_email = get_option('admin_email');
    $subject = 'MyPlugin has been deactivated';
    $content = 'MyPlugin has been deactivated. You can again activate it from your WordPress Admin Plugin Management page';

    wp_mail( $to_email, $subject, $content );
}

?>

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.

Discussion