WordPress

How to Create Your First WordPress Plugin (With a Minimal Amount of Coding)

wordpress plugin illustration
Written by Ellice

wordpress plugin illustration

If we had to rank the reasons we love WordPress, plugins would be near the top of the list. These handy tools let you add all kinds of features to your site in seconds. However, sometimes you can’t find a plugin that meets your needs or you may simply want to try your hand at developing your own solution.

If that’s the case, then you’re in luck. It’s actually considerably easier to create a WordPress plugin than you might expect. In fact, you can create a plugin with just a few lines of code, and you need very little prior knowledge or experience to put together something simple.

In this article, we’ll explore all the basics of plugin development. We’ll explain how plugins work, and discuss how they fit into the WordPress ecosystem. Then we’ll discuss what you need to know, before ending on a step-by-step guide to creating your very first WordPress plugin. There’s a lot to cover, so let’s get started!

The Basics of a WordPress Plugin

wordpress.org plugins home page

If you’re familiar with WordPress, you’re most likely familiar with plugins too. As the official documentation puts it: “Plugins are ways to extend and add to the functionality that already exists in WordPress.”

WordPress itself consists of a relatively small set of features, which are referred to collectively as the platform’s ‘core.’ Plugins are downloadable add-ons, containing files and code that expand or alter the functionality of that core. In fact, the WordPress philosophy states that if a feature is likely to be used by no less than 80 percent of all users, it should be included in the core. Otherwise, it should be a plugin.

For this reason, the breadth of possibilities offered by plugins is huge. You can use them to add opt-in forms, sliders, or pop-ups. They can be very simple, such as Hello Dolly, which displays a random line from the titular song in the dashboard. Or they can be incredibly expansive, such as the Jetpack plugin, which adds dozens of new settings and features to your site.

Did You Know? Jetpack Professional now comes bundled with our DreamPress Plus and Advanced plans

This fact means that plugins are one of the most important features of WordPress. They make it easy to add almost any functionality to your site, with no coding or prior knowledge required. What’s more, downloading and installing plugins is as simple as a few clicks.

Plugins are also an important vehicle for those who want to get started with development themselves. As our own Mika Epstein put it:

“I’ve taken over a few abandoned plugins, forced others to do things the original authors never dreamed of, and I’ve written some from scratch. The ability to take other people’s code, read it, learn from it, and improve on it, is one of the basic tenets of Open Source, and it’s allowed me to become a bit of a developer.”

That last part is an important point to remember. Since WordPress and plugins are open source, their code is available for anybody to use and experiment with. This is an excellent way for budding developers to see how things work behind the scenes, and get involved in creating plugins themselves.

The Differences Between Plugins and Themes

Before we move on, we need to quickly cover some important groundwork. First, let’s talk about the differences between themes and plugins. On the surface, this may seem like an obvious distinction. Surely a theme just changes the look of your site, while a plugin adds functionality?

The truth is slightly more blurry than that, though.

In fact, a theme can also change a site’s functionality, while a plugin can make alterations to its appearance. All WordPress themes contain a functions.php file, which includes code that adds functionality to your site. This works very similarly to the way a plugin does. In fact, you can add the same code to either a plugin or functions.php, and it will work the same on your site.

The difference is that when you add code to functions.php, it gets linked to your current theme. This can be useful if you want to change some of your theme’s functionality, or you want to quickly add a function without needing to write a whole plugin. You can also use this method if you’re creating a theme and want to insert custom functions.

However, if you decide to change themes, the code you’ve added will no longer be active. On the other hand, plugins are standalone entities that are (usually) not tied to any specific theme, which means that you can switch themes without losing the plugin’s features. Using a plugin instead of a theme also makes the functionality you want to create easier to maintain and share with others.

Further Reading: How to Install a WordPress Child Theme

How Plugins Work: An Introduction to Hooks, Actions, and Filters

Now, it’s time to get a little more technical. We’ve already mentioned that plugins literally ‘plug in’ to WordPress core. This is done using ‘hooks,’ which enable one piece of code to interact with another. As such, hooks determine when and where on your site a plugin is actually used.

We realize that this can be a bit confusing if you’re new to the topic. Therefore, let’s consider an example. Imagine you have a plugin that changes the error message that appears when somebody tries to sign in to your site with the wrong password. In this scenario, the error message is the hook. A plugin could connect onto the code that displays that message, and change the text that’s shown.

There are two types of hooks that WordPress understands. They are:

  • Actions: These are used to add or change WordPress functionality.
  • Filters: These are used to alter the functionality of actions.

As you can probably guess, there’s a lot more to how hooks actually work. However, this should give you a rough idea of how the two differ. For now, this is all you’ll need to know, but we recommend that you read up on all the hook types in the Plugin Developer Handbook.

What You Need to Know Before Creating a Plugin

By now, you may be itching to start creating your own plugin. We’ll get to that momentarily. First, however, you’ll need to ensure that you’re well-prepared.

While it’s not a requirement in order to follow the steps below, some programming or coding experience will be incredibly useful. This is especially true if you want to write your own plugin as opposed to tweaking someone else’s code. Having a solid technical foundation will also make it much easier to adhere to WordPress’ coding standards for developing plugins.

Before you create your plugin, you should also consider how you’ll use names. This applies both to the plugin itself and to the functions it contains. You’ll need to avoid naming collisions, which occur when multiple elements on a site have the same name.

For example, if you name a function in your plugin add_media_page, it will clash with the WordPress function that already uses that string. The best way of avoiding this is to choose a unique prefix, which you’ll apply to every function.

You should also determine how you’ll want to structure the plugin. In practice, this means deciding whether you’ll use a single file for your code or spread it out across multiple files. In addition, you’ll also want to plan out how you’ll structure your plugin’s folders.

Of course, there’s plenty more to consider before you become a full-fledged WordPress developer. For example, plugin security is essential in order to make sure your plugin doesn’t contain any vulnerabilities. You might also want to provide documentation for your plugin if you plan on sharing it.

We realize that this is a lot to take in, but don’t worry. A lot of these considerations only become applicable once you create plugins that are intended for others to use, and you’re probably still a little while away from doing that. Instead, let’s dial it back now and get practical. That’s right — it’s time to develop a plugin!

How to Create Your First WordPress Plugin (In 4 Steps)

The time has come to create your first plugin, using a minimal amount of coding. As such, we’ll stick to something basic. Throughout the following steps, we’ll be creating a plugin that will change the link text that displays when you insert a Read More tag into a post.

Step 1: Set Up a Testing Environment

When you’re developing anything, whether it’s creating a plugin or making other changes that could affect your site, you should always use a testing environment. This is also known as a ‘staging site’ or ‘local environment,’ depending on whether the site is stored on an external server or your own computer.

Regardless of the location, the testing environment should be a private copy of your site. This lets you add to and edit your site’s files and functionality, without the risk of causing damage to the actual live website. This is especially important when you’re working with core files and plugins, as mistakes could cause lasting harm to your site.

Related: A Beginner’s Guide to Website Staging (And How to Set It Up)

Fortunately, setting up a testing site is pretty simple, thanks to the wide variety of tools that are available to help you out. To set up a local environment, we recommend a tool like DesktopServer. This program makes it easy to install a local version of WordPress on your computer.

serverpress.com download landing page
You can also create an online staging site. This will function as an exact copy of your actual site, except that only you and other admins can access it. Here at DreamHost, it’s easy to simply create a copy of your site.

dreamhost.com wordpress hosting home pageLater on, you can even copy the changes you’ve made on your staging site and import them back to the live version.

When you have your risk-free testing environment ready, it’s time to create the actual plugin.

Step 2: Create a New Plugin File

To start putting together a new plugin, you will need access to your site’s directory. The easiest way to do this is by using SFTP, which is a method for viewing and managing your site’s files when they’re located on an external server. We’ll be using a client called FileZilla in our example, since it’s both free and easy to use.

Start up FileZilla and use the SFTP credentials provided by your web host to access your site. Once you’re in, you’ll need to navigate to the folder that contains your plugins, which is located at /wp-content/plugins/.

filezilla ftp example 'plugins' folderTo add your new plugin, you’ll need to create a new folder inside this directory. Do so now, and give it any unique name you’d like. We’ll call ours very-first-plugin.

filezilla ftp example directory 'very-first-plugin'
This folder is where everything related to your plugin will ‘live’ on your site. Since this plugin is going to be very simple, it only needs to contain a single file, which you’ll create now. This will be a PHP file, which will contain the plugin’s code.

As such, open the new very-first-plugin directory and right-click inside it to select Create new file.

filezilla ftp example 'create new file'You’ll be asked to name the file. This should be identical to the folder, so call it very-first-plugin.php (make sure to use the right file extension). This file will now be added to the folder.

filezilla ftp example 'very-first-plugin' added to folderAt this point, you’ll need to add some basic information to the file. That information will be used by WordPress to display the plugin’s name, content, and other information. To do this, right-click on the PHP file and select View/Edit. You will then be asked to open the file, which you can do in any text editor, such as TextEdit or Notepad.

text editor example blank pageThe file is empty now so paste the following text into it:

<?php
/**
* Plugin Name: Very First Plugin
* Plugin URI: https://www.yourwebsiteurl.com/
* Description: This is the very first plugin I ever created.
* Version: 1.0
* Author: Your Name Here
* Author URI: http://yourwebsiteurl.com/
**/

Naturally, feel free to change the information here to match your actual details, before saving the file. When asked if you want to upload the file back to your site’s server, click on Yes.

When that’s done, you can actually see the plugin in your site’s admin dashboard. Log in now and take a look in your plugin library.

plugin library in wordpress admin dashboardYou can even go ahead and Activate your plugin now. Of course, the plugin doesn’t actually do anything yet. That’s because we haven’t added any functionality to it.

Step 3: Add Code to Your Plugin

As we mentioned earlier, this plugin is going to change the text that appears when you insert a Read More tag. To see what this looks like normally, create a new blog post and click on the Insert Read More tag icon.

wordpress post dashboard view 'insert read more tag'
If you publish the post and check it out on the front end of your site, you will see that the tag has been replaced with a link that says “Continue reading”:

wordpress post image tag 'continue reading'This is the text that we want to change. To do that, open up your very-first-plugin.php file again. Then, add a new line at the very end of the file, and paste in the following code:

function dh_modify_read_more_link() {

    return '<a class="more-link" href="' . get_permalink() . '">Click to Read!</a>';

}

add_filter( 'the_content_more_link', 'dh_modify_read_more_link' );

Let’s quickly break down what this code contains. First, it creates a new function, which we’ve called ‘dh_modify_read_more_link.’ You’ll notice that we added the “dh” prefix, to make sure the name is unique.

All this function does is get the link for the current post, and then apply the anchor text “Click to Read!”. This is the text that will replace “Continue reading.” Of course, you can feel free to change this text to anything you want to use.

The final line in this code uses a filter to hook into a function called the_content_more_link, which represents the Read More link. The filter instructs WordPress to call our new function instead, so the standard link will be replaced with our new version.

At this point, you can save the plugin file. Since you already activated the plugin, its functionality should work right away. Test this by checking your website to see if the link text has changed.

If it worked, then congratulations! If not, you will need to check your code to make sure you entered everything correctly. In addition, check to see if the plugin is active and that all the function names are correct.

Once you’ve got the plugin to work, you’re ready to progress to the final step.

Step 4: Export and Install Your Plugin on a Live Site

Your new plugin is now ready to be used on a live site. Fortunately, this step is usually the simplest. All you need to do is to compress the very-first-plugin folder into a ZIP file. If you used a local environment to create your plugin, you just need to right-click on the folder and select Compress.

FTP example, right-click "compress 'very-first-plugin'"
If you’ve been using SFTP, on the other hand, you can do the same thing by first right-clicking on the folder and selecting Download.

ftp right-click 'download'

Once the folder has downloaded to your computer, you can compress it just like we mentioned above.

This zip file can then be uploaded to your live site. Open your WordPress admin dashboard, navigate to Plugins, and then click on Add New.

wordpress dashboard plugin menuOn the next screen, you can select Upload Plugin, which will let you choose a plugin file from your computer.

wordpress dashboard 'upload plugin'Select the very-first-plugin.zip file you created and click Install Now. The plugin will now be added to your live site, and you can activate it as usual.

That’s it! You’ve successfully created your first plugin and uploaded it to a live WordPress site. If this experience has made you keen to progress further with plugin development, great! There are plenty of great resources out there to help exapnd your repitoire, but the best place to start is the Plugin Developer Handbook. Who knows, you might even want to host your own plugins in the WordPress Plugin Directory one day!

Time to Get Plugin’ Away

Not only is WordPress a phenomenally user-friendly platform, but it also offers a great deal of flexibility. Due to its open-source nature, anybody can add new functionality to the platform. The best way to get started is by creating your own plugins.

Do you have any questions about how to create your own WordPress plugins? Join the DreamHost Community today and let’s start the discussion!

About the author

Ellice

Ellice comes to us from House DreamHost, the first of her name, Gatekeeper of All Things Content, Protector of the Brand and Ruler of Social Realms.

tl;dr - Ellice is the Content Marketing Manager at DreamHost and oversees all social media and content efforts.