Hey Champs, Welcome everyone to another blog.
In today’s blog we are going to learn about “how to create a simple wordpress plugin - beginner friendly”. Very First, What is a plugin? If you’ve been working with WordPress for a while, you probably know how important the plugins are. Almost Each and every feature we add like contact forms, SEO tools, page builders, payment gateways — all come from plugins.
But here’s the interesting part: creating a basic WordPress plugin is actually much easier than most people think. For the first time I thought about creating a plugin which feels like we need to do big lines of code and it scared me. But actually you don’t need thousands of lines of code. Sometimes even a single PHP file is enough to build a working plugin.
In this blog, we’ll walk through how to create a very simple WordPress plugin from scratch. To keep things practical, we’ll build a Post Word Count plugin that automatically shows the number of words in a blog post.
This example will help you understand how plugins work and how you can start building your own custom functionality in WordPress. First, let’s have a quick look about what is a plugin?.
What is a WordPress Plugin?
A WordPress plugin is simply a piece of code that adds extra functionality to your website without modifying the WordPress core files.
Think of plugins like apps for your website. Just like you install apps on your phone to add features, plugins allow you to extend the capabilities of your WordPress site.
Plugins can do many things, such as:
- Adding contact forms
- Improving SEO
- Creating eCommerce stores
- Adding security features
- Displaying custom information in posts
In our case, we will build a plugin that counts the words in a blog post and displays it automatically.
What We Are Going to Build
The plugin we’re going to create will:
- Count the number of words in a blog post
- Display the word count at the end of the post
- Work automatically for all blog posts
For example, when someone finishes reading your article, they might see something like this at the bottom:
Word Count: 850
This might seem like a small feature, but it’s a great way to understand how plugins interact with WordPress content.
Step 1: Go to the Plugins Directory
First, open your WordPress installation folder.
Navigate to the following path:
wp-content → plugins
This is the folder where all WordPress plugins are stored.
Step 2: Create a New Plugin Folder
Inside the plugins folder, create a new folder for your plugin.
Example:
post-word-count
Your folder structure will look like this:
Wp-content > plugins > post-word-count
Step 3: Create the Plugin File
Inside the post-word-count folder, create a new PHP file. The file name should be same as your folder name.
For example:
post-word-count.php
Now your structure should look like this:
Post-word-count > post-word-count.php
Step 4: Add the Plugin Header
Every WordPress plugin needs a plugin header. This header tells WordPress that the file is a plugin and provides some basic information about it like name, description, author, version and so on.
Open your PHP file and add the following code:
<?php
/*
Plugin Name: Post Word Count
Description: A simple plugin that displays the word count for blog posts.
Version: 1.0
Author: Your Name
*/
Once this file is saved, WordPress will automatically recognize it as a plugin.
Step 5: Create the Word Count Function
Now we will create a function that calculates the number of words in a blog post.
Add this code below the plugin header:
function pwc_display_wordcount($content) {
if (is_single() && is_main_query()) {
$word_count = str_word_count(strip_tags($content));
$wordcount_html = "<p style='font-weight:bold;'>Word Count: " . $word_count . "</p>";
$content .= $wordcount_html;
}
return $content;
}
Let’s understand what’s happening here.
First, we created a function called pwc_display_wordcount. This function receives the post content as a parameter.
Inside the function, we check whether the page being viewed is a single blog post. This is important because we don’t want the word count to appear on archive pages, category pages, or the homepage.
Next, we calculate the number of words using the str_word_count() function. Before counting the words, we remove HTML tags like <p>,<b>,<div> etc so we use strip_tags() so that only the actual text is counted.
Then we create a small HTML element that displays the word count.
Finally, we append that HTML to the post content so it appears at the bottom of the article.
Step 6: Connect the Function to WordPress
Now we need to tell WordPress when to run our function. This is done using something called a hook.
There are two types of hooks called “Actions” & “Filters”
Actions - used to do something but it won’t return any value
Filters - Used to modify data and it must return a value.
Add this line at the bottom of the file:
add_filter('the_content', 'pwc_display_wordcount');
This line connects our function to the_content filter.
In simple terms, it means:
Whenever WordPress prepares the post content to display on the page, it will run our function and allow us to modify that content.
Final Plugin Code
Your complete plugin file should now look like this:
<?php
/*
Plugin Name: Post Word Count
Description: A simple plugin that displays the word count for blog posts.
Version: 1.0
Author: Your Name
*/
function pwc_display_wordcount($content) {
if (is_single() && is_main_query()) {
$word_count = str_word_count(strip_tags($content));
$wordcount_html = "<p style='font-weight:bold;'>Word Count: " . $word_count . "</p>";
$content .= $wordcount_html;
}
return $content;
}
add_filter('the_content', 'pwc_display_wordcount');
Step 7: Activate the Plugin
Now go to your WordPress dashboard.
Navigate to:
Dashboard → Plugins → Installed Plugins
You will see your plugin listed as Post Word Count.
Click Activate.
After activation, open any blog post on your website and scroll to the bottom. You should now see the word count displayed automatically.
Why This Example is Important
Even though this plugin is simple, it teaches some very important concepts in WordPress development:
- How plugins are structured
- How WordPress hooks work
- How to modify post content using filters
- How PHP functions interact with WordPress
Once you understand these basics, you can start building more advanced plugins.
Further Ideas
If you want to experiment further, you can extend this plugin with additional features.
For example:
You could add reading time estimation, like:
“Estimated Reading Time: 3 minutes”
You could allow users to enable or disable the word count from the admin panel.
You could also display the word count at the top of the article instead of the bottom.
These small improvements will help you practice real plugin development skills.
Final Thoughts
Learning to create WordPress plugins is one of the best steps you can take as a WordPress developer. It allows you to build custom solutions instead of depending entirely on third-party tools.
If you learned it completely you will be able to create your own plugin and sell it.
The example we covered here is simple, but it shows how powerful WordPress hooks and filters can be.
Once you get comfortable with this process, you can start building plugins for more advanced features like custom dashboards, WooCommerce enhancements, performance tools, and much more.
Every advanced plugin starts with small experiments like this — so keep building and keep exploring.
