WordPress Custom Plugin

WordPress Custom Plugin

How I made my first plugin.

Making My First WordPress Plugin

This blog entry is about how I learned to make a custom WordPress plugin. An example and step-by-step guide is provided for other beginners to learn, too. There are some additional resources sprinkled throughout the article that I gained my knowledge from and that you might find useful as well.

What is a WordPress Plugin?

A WordPress plugin is an extra snippet of code that adds some sort of functionality to an existing WordPress site. It is an add-on to everything else already in WordPress, so that you can customize features without worrying about wrongly editing the original code. It is written in the PHP language.

Why Should I Make a Custom Plugin?

"Any functionality you want to add or modify should be done using plugins." - WordPress Developer Handbook

  • Instead of messing around with core WordPress files, we can add a plugin to do a desired function without worrying about crashing everything.

  • There are lots of available plugins already available, but sometimes you want something that is yours and done exactly the way you want it.

  • You could edit the functions.php file in your theme, but what if you change your theme later on? All that code is lost. A plugin can easily be uploaded and contains all your customization in one place.

What Minimal Requirements Are Needed For a Working Plugin?

All you need is the name, but that alone doesn't have any functionality. Add in more details like the description at the very least, then put the function under the comment block.

In this example, it is called Crystal's First Plugin and its function is to hide the admin bar. The admin bar is that black bar on the top of the screen when you preview a website.

Oh, and it has to end in the .php file.

Example:

crystal-plugin.php

<?php
   /*
   Plugin Name: Crystal's First Plugin
   description: It does great things, like hides the admin bar
   Version: 1.0
   Author: Crystal
   */
// Remove the admin bar from the front end
add_filter( 'show_admin_bar', '__return_false' );

note: you don't need to add the closing php tag

Why would you want to hide the admin bar? Honestly, I don't know. I would just use it as an easy way to test that the plugin was uploaded correctly then I'd swap it out with what I actually want.

After you are happy with it, make sure you compress it into a .zip file. The way I will show you how to upload it onto WordPress requires it to be in a zip file. There are other ways to do it, but this way is super easy so that's what I picked.

How Do I Upload my Plugin?

First, make sure you have put it in a .zip file.

There are a couple of ways to do this, but the easiest is to go to your WordPress Dashboard, click on "Plugins" on the left menu, "Add New" and then instead of searching for an existing one to download, you can upload your own.

Screen Shot 2021-11-17 at 1.28.15 PM.png

Once you upload it, you should see it in your list similar to this:

Screen Shot 2021-11-17 at 2.04.49 PM.png

Then try it out to see if it actually does what you want it to do!

Plugin Ideas That Could Work

Some real-life examples of where making your own plugin could work:

  • Any piece of custom code that you really like the look of and you want to repeat, like a navigation menu, and current available plugins just aren't good enough.
  • Research existing plugins and see if there room for improvement. If there is, code it!

There are also some blog posts that have some ideas with functions that you can copy/paste, like the admin bar one I showed above. You can also look at what is popular to see what sorts of things people are using plugins for currently.

10 Useful Code Snippets for WordPress Users

24 Must Have WordPress Plugins for Business Websites in 2021

Resources

WordPress Developer Handbook for Plugins

How to Create a Simple WordPress Plugin | 2021 WordPress Tutorial (video)