Create WordPress Plugin

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

To create a PHP file, you can use any text editor – even notepad works, though an editor like VS Code will make your life easier.

Step 1: Creating plugin file:

A plugin file is just a simple PHP file, containing some structured data at the top of the file as a comment. The plugin file name can be anything, but in the majority of cases, it is a sanitized name of the plugin. Ex: if you are creating a plugin named “James Analytics”, a suitable name for the file would be “james-analytics.php”. But it’s not a requirement. The plugin header should only be in the main plugin file, not in every file used by the plugin.


Step 2: Adding plugin information to the file:

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

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

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

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

The plugin doesn’t have any feature yet. So let’s add something so it will actually 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’s email address when your plugin is activated.

Send Email on WordPress Plugin Activation

In the above code, three native WordPress functions have been used. You can learn about their usage from the WordPress developer reference pages, 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 –

The next thing is to save the file and put it in the plugins directory. By default, the WP plugins directory/folder is relative to your WordPress setup. So if WordPress is set up at public_html/, the 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 beside the other plugins of your site. Activating the plugin will send an email to the site administrator’s email, and deactivating it will send another. Note: If you are testing this on localhost such as XAMPP/WAMP etc, you won’t actually get any email, as a local server doesn’t come with an email server by default.

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


What should you do next?

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