How To Create A WordPress Plugin?

Functionality to your theme that changes the default gravatar to your own custom gravatar. The only issue is, you’ve just changed themes and now that’s gone. If you had added that code to a plugin it would still be there when you decided to switch themes. We ran into this issue with the Tabata Times multisite network. They use a handful of themes that need to share custom functionality.

Create a WordPress plugin

FTP into your site

The first thing you’ll need to do is access your site via FTP using the FTP program of your choice. If you’re not familiar with FTP, I recommend you read up on that before moving forward.

Navigate to the WordPress plugins folder

Once you’ve accessed your site via FTP, you’ll need to navigate to the WordPress plugins folder. That folder is almost always located at /wp-content/plugins.

Create a new folder for your plugin

Now that you’re in the plugins folder it’s time to create a folder for yours. Go ahead and create a new folder, giving it a unique name using lowercase letters and dashes such as my-first-plugin.

Create the main PHP file for your plugin

To create a PHP file within your new plugin folder and give it the same name such as my-first-plugin.php. After you’ve done that, open your plugin’s main file and get ready to do some editing.

Setup your plugin’s information

Finally, copy and paste the plugin information below into your main plugin file. Make sure to edit the details such Plugin Name and Plugin URI as they pertain to your plugin.

Actions and filters

If you’re going to start coding your own plugins, I highly suggest you familiarize yourself with how actions and filters work and which ones are available for you to use.

WordPress functions

Again, I spend a lot of my time in the WordPress Codex reading up on core functions as I develop my plugins. There are so many core functions that I wouldn’t expect you to know what each and every one of them is and does.

Creating an options page

Creating an options page isn’t necessary, there are many plugins that install and do something without one, but having one can be a nice addition for users of your plugin.

Plugin safety

If you plan to distribute your plugin, then security is of utmost importance, because now you are fiddling with other people’s websites, not just your own. All of the security measures you should take merit their own article, so keep an eye out for an upcoming piece on how to secure your plugin.

Retrieving and showing the views

The WordPress get_post_meta() function to retrieve the views, so writing a separate function for this might seem a bit redundant. In this case, it might well be redundant, but it promotes some object-oriented thinking, and it gives us greater flexibility when further developing the plugin.

Test your plugin

As you continue developing your plugin, it’s important that you save your work often and test your changes on your staging site. You’ll also want to keep an eye out for any security issues, so you can resolve them before publishing your plugin.

Conclusion

Plugins will turn your site from a simple blog into a powerful website that includes advanced features and is secure and robust. Try adding plugins to your site today and coding your own to see how you can use plugins to improve your WordPress site.