How to Create a WordPress Plugin

In this WordPress plugin tutorial, you will learn how to create a complete WordPress plugin with its own admin page. The most significant reason for creating a plugin is that it enables you to separate your own code from the core code of WordPress. The rest of the site will generally continue to function if something goes wrong with your plugin.

What you are going to need

Editor of text

FTP hosting account access

A working installation for WordPress

You will need a text editor such as Notepad++ or NetBeans to complete steps in this WordPress plugin tutorial. FTP access and WordPress installation are required for your hosting account.

You will need a text editor like Notepad++ or Sublime to fill in the steps of this WordPress plugin tutorial. FTP access and WordPress installation are required for your hosting account.

This plugin tutorial for WordPress is intended for people with basic PHP knowledge. A new function is written, WordPress functionalities are called by parameters and PHP comments are written.

It is also strongly advised to create a website backup before continuing.

What are the plugins for WordPress?

you should also read this: What is WordPress and how does it work?

A WordPress plugin is an independent code package that enhances and expands WordPress functionality. A plugin can add new features to any part of your website, including the Admin Control Panel, through any combination of PHP, HTML, CSS, JavaScript/JQuery, or other Web programming language. You can change WordPress’s predetermined behavior or delete undesired behavior. You can easily personalize WordPress plugins and customize them to fit your needs.

Due to their standalone WordPress plug-ins, they don’t alter the WordPress core code physically. You may copy and install them on any installation of WordPress. The alternative way to make WordPress changes (and highly discouraged) is to write new functions into the functions.php WordPress that is stored in the folder /wp-includes / or the functions.php file that is part of a subject. There are a number of possible problems here.

Regular updates are available to WordPress and its themes. And, without WordPress child theme, your new code will be deleted and you’ll have to write everything over again when functions.php is overwritten with an update. You may have to replace the whole file with the original, deleting any changes that you have made if you write a lot of functions and any of them have an error, which you cannot debug. If you remove your functions from the file, your site may be covered by PHP errors if it attempts to call the missing functions.

When installing WordPress updates, plugins are not overwritten or removed automatically. When you have coding errors with your plugin, you can normally disable it in the Admin Control Panel while fixing it. If your plugin has a serious mistake, sometimes WordPress will automatically disable it to you to continue working.

  • Action hooks (Add/remove functions)
  • Filter hooks (To modify data that is produced by functions)

Action and Action Hooks

When you visit a WordPress page, several PHP (named actions) functions are called and attached to action hooks. You can add your own functions to the lists of actions that will be executed when any action hook is called and also remove previous functions from any action hook using the action hooks provided by WordPress. When actions are called, action hooks dictate. The action hook wp_head() is called, and the actions hooked on the wp_head() are running before closing </head> tag of any page.

Action hooks are contexts – some are requested on each page of the website, others are called only when the admin panel is visible, etc. The WordPress Plugin API/Action Reference Page contains a complete list of action hooks and their context.

Add functions to a hook with add_action()

In order to add a function to any action hook, you should call your plugin with at least two parameters with a WordPress feature named add_action().

// Hook to the ‘init’ action, which is called after WordPress is finished loading the core code

add_action( ‘init’, ‘add_Cookie’ );

// Set a cookie with the current time of day

function add_Cookie() {

setcookie(“last_visit_time”, date(“r”), time()+60*60*24*30, “/”);

}

  • The first parameter required is the name of the action hook to which the function is hooked.
  • The second parameter you need is the name of the function to be removed.
  • The third (optional) parameter is the primary function priority. This parameter has to be the same as that set in the action hook before the action was added. Don’t include the parameter if you haven’t defined a priority in your custom function.
  • The fourth (optional) parameter is the number of parameters that your custom filters function can take. The default value is 1.

Basics To Know Before Creating a Plugin for WordPress

In this section, when creating a WordPress plugin, we will reveal the first few steps you need to follow. Moreover, as you write your plugin, we will mention all the different things you need to take into consideration. Terrific. Great. Let’s begin with the fundamentals.

How to Name A Plugin for WordPress

You need to come up with a unique name for your WordPress plugin before doing anything else. Thinking of what your plugin will do is one of the best ways to determine a favorable name. For example, if your plugin helps individuals share content via social media, you can include the phrase “sharing social media” in the name. Another thing, several words can be plugin names, so don’t reduce creativity.

To avoid conflicts with other plugins, your plugin name must be unique. You can do a Google search for your name to make sure your name is unique. In addition, you can search for different plugin directories, including the repository for the WordPress plugin.

We have to create at least one plugin file (the main PHP file) to name any plugin, which introduces us to our next section.

Let See: 5 Best WordPress Hosting in 2022

How to Create Files for Plugins

Depending on what it’s designed to do, a plugin can be made up of a single PHP file or several files. The main PHP file, which is the equivalent of index.php and index.html for WordPress themes and HTML designs, is the most important file.

It’s recommended that developers of WordPress name their main plugin file by the convention after their plugin. The main plugin file for a plugin named WP WordReplace plugin, for example, would be wp-wordreplace.php. Use only hyphens (-) between words as opposed to underscores (_) when adding a separator to your name.

A plugin can be made of single or several files, as stated above (images, JavaScript, language, CSS files, etc). Either way, your plugin files must be stored in a single directory. So for plugins named WP WordReplace, the wp-wordreplace.php file would be stored in a wp-wordreplace folder. Additional subfolders can be added to the main plugin folder to contain and organize other files.

After you lay down all the plugin code, you will compress your main folder into a zip file (in this case it would be wp-wordreplace.zip archive) to be uploaded and installed on your WordPress site.

Add a File Header to the main PHP file

You should add other information when naming your plugin, such as definition, version, license, author name, essentially all that appears in the WordPress Plugins Screen under and next to your plugin, the plugin header. You must use an information header of the default plugin at the top of your principal PHP file. So looks like a typical header:

/*
Plugin Name: WP WordReplace
Plugin URI:  https://joomtechsolutions.com/wordpress
Description: This plugin replaces words with your selected words.
Version:     1.0
Author:      Sanjeev Kumar Raghuwanshi
Author URI:  http://jomtechsolutions.com
License:     GPL2 etc

Each parameter in the above header is self-explanatory, so I’m not going to go into the details. Just make sure that when writing your Plugin and Author URIs, or the links will not work, you include the relevant http:// or https://.

If you use a GPL2 license or a license compatible with GPL2, follow your header with the following license information:

/* Copyright PLUGIN AUTHOR NAME YEAR (email : your email address)
(Plugin Name) is free software: you can redistribute it and/or modify it.
Under the terms of the GNU General Public License as published by the GNU General Public License.
The Free Software Foundation, either version 2 of the License or the Free Software Foundation.
Any later version of it.

(Plugin Name) is distributed with the hope that it will be useful.
But WITHOUT EVERY WARRANTY; without even implicit warranty.
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
GNU General Public License for further details.

A copy of the GNU General Public License should have been received.
Well, along with (Plugin Name). See (http://link to your plugin license) if not.
*/

The scope of this tutorial does not allow us to move beyond these basic steps. You’ll need to check out how to write a plugin guide in the codex to learn more about WordPress plugin hooks, template tags, save plugin data to databases, plugin options mechanism, and update your plugin among other things. The codex also includes a massive collection of Plugin Resources, which is full of video guides, advanced topics, and more.

Now that we’ve covered the basics, let’s write a simple WordPress plugin that performs two basic (but nifty) functions:

  • Replace words in your content with your choice of words
  • Add a “Thank you for reading this tutorial” note at the end of each blog post.

How to Write a Simple WordPress Plugin – WP Word Replace

You can also read this –  10 Best Free Multipurpose WordPress Themes 2022

We’ll write code for the WP Word Replace plugin in this section, which I’ve been mentioning all along.

What you need to do:

Your favorite editor of code (e.g. Notepad++ & SublimeText)

A browser where your plugin can be seen at work (e.g. Chrome)

A working installation for WordPress

Our Plugin Naming

We checked the repository of the WordPress plugin first and did a Google search for our name; WP Word Replace was free. WP Rename was my initial choice but it was already taken.

Moving on… Open a new file in your code editor, and add the following code to the top after you open the  plugin  <?php:

<?php
/*
Plugin Name: WP WordReplace
Plugin URI:  https://joomtechsolutions.com/wordpress
Description: This plugin replaces words with your selected words.
Version:     1.0
Author:      Sanjeev Kumar Raghuwanshi
Author URI:  http://jomtechsolutions.com
License:     GPL2 etc

Copyright 2025 JoomTech Solutions (email : sanjeev1.raghuwanshi@gmail.com)
(Word Replace) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
 
(Word Replace) is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with (Word Replace). If not, see (http://link to your plugin license).
*/

Save the file as wp-wordreplace.php in the wordreplace folder. If you do not already have this folder, create it. Your main PHP file will be wp-wordreplace.php.

Add Functions
Now to add to the plugin the actual functions. Just below the above code, add the following function to Joomla to correct Joomla’s misspellings:

function wordreplace_content_replace( $text ) {
	return str_replace( 'joomla', 'Joomla', $text );
}
add_filter( 'the_content', 'wordreplace_content_replace' );

wordreplace_content_replace is the unique name we’ve given to our function. When adding new functions, never start wp_ to prevent future incompatibilities with WordPress code functions that all use wp_ prefixes.

Our PHP function takes ($text) as an argument, and returns the 1st string ‘Joomla replaced by the 2nd string ‘Joomla.’

We’ve added a filter (add filter) to our plugin to tell our function (wordreplace_content_replace) to work on the text we’ve selected, which in this case is the entire content of the post (the_content).

To replace more than one word (maybe you want to edit multiple words across your blog or use the plugin as a simple profanity filter), replace the above code with the following code:

function wordreplace_content_replace( $content ) {
    $search  = array( 'wordpress', 'joomla', 'jomtech', 'developer' );
    $replace = array( 'WordPress', 'Joomla', 'JoomTech', 'Blogger' );
    return str_replace( $search, $replace, $content );
}
add_filter( 'the_content', 'wordreplace_content_replace' );

In our code above, we have already selected words to replace e.g. wordpress, joomla, joomtech, etc. We also selected the replacement words, e.g. WordPress, Joomla, JoomTech, and so on. Let us hope that the code is somewhat self-explanatory:

The wordreplace_content_replace function ($content) takes all the words in the $search array to be replaced by the argument and returns the word that has now been modified to WordPress.

All the words to be replaced by $search

$replace includes substitute words

str_replace is doing the best, substituting words with new words

Note how we add every function to the prefix wordreplace. This prevents conflicts with possible other plug-ins. You should get used to adding prefixes to your functions, be they plugins, themes, or widgets.

If your plugin has completed the steps above, it can effectively replace all the words you selected. Let’s now add the note “Thank you for reading this lesson” at the bottom of every posting. Please add the following code (wordreplace_content_replace) to your main plugin file before closing the PHP bracket (?>) in the final line:

function wordreplace_content_footer_note( $content ) {
	$content .= 'Thank you for reading this wordpress tutorial. For more WordPress tutorials visit our WordPress Tutorial';
 return $content; }
 add_filter( 'the_content', 'wordreplace_content_footer_note' );

Save the modifications. This function adds the HTML markup to the $content parameter. The text replaces content footer note returns the new WordPress value. We also have a footer class added to our text so that later it can easily be styled.

We included a filter (add filter) that indicates that our feature is working on the selected text that is the post content of the content.

Compress Your Directory

You should look like this at the end of your wp-wordreplace.php file:

<?php
/*
Plugin Name: WP WordReplace
Plugin URI:  https://joomtechsolutions.com/wordpress
Description: This plugin replaces words with your selected words.
Version:     1.0
Author:      Sanjeev Kumar Raghuwanshi
Author URI:  http://jomtechsolutions.com
License:     GPL2 etc

Copyright 2025 JoomTech Solutions (email : sanjeev1.raghuwanshi@gmail.com)
(Word Replace) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
 
(Word Replace) is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with (Word Replace). If not, see (http://link to your plugin license).
*/

function wordreplace_content_replace( $content ) {
	$search  = array( 'wordpress', 'joomla', 'jomtech', 'developer' );
	$replace = array( 'WordPress', 'Joomla', 'JoomTech', 'Blogger' );
	return str_replace( $search, $replace, $content );
}
add_filter( 'the_content', 'wordreplace_content_replace' );


function wordreplace_content_footer_note( $content ) {
	$content .= 'Thank you for reading this wordpress tutorial. For more WordPress tutorials visit our WordPress Tutorial';
	return $content;
}
add_filter( 'the_content', 'wordreplace_content_footer_note' );

Save all your changes. Save all your changes. Compress the WP folder WordReplace in an archive wp-wordreplace.zip (On your Mac it’s just the right-click, it’s a compressed file and it’s similar, I think, on your PC). Just make sure that your file is saved or that your plugin will not install as an a.ZIP extension. Use the plugin Upload and activate your new WP plugin WordReplace with the WordPress Screen plugins. Congrats on writing the first plugin for you!

how to create a plugin for a website
how to create a plugin for a website

Summary

I hope this tutorial will point you in the right direction as far as understanding plugins is concerned. This post should serve as a stepping stone to developing complex WordPress plugins that do whatever you want to do. Don’t stop here, check out the resources I recommended above to improve your knowledge of WordPress plugin development.

If you found this tutorial helpful or if you had anything else to add, we’d love to know about it. In the comment section below, please share your thoughts. See you all around

You may like to read these also:

Let See: How to Install WordPress on Windows[XAMPP]

Related Posts