If you have ever pasted a PHP snippet into your theme’s functions.php and lost it at the next update, you already know why developers build plugins. When you create a WordPress plugin, your code lives in its own folder, survives theme changes, and can be switched on or off with one click. In this guide, you will build one from scratch: a reading-time indicator for blog posts, covering the header, hooks, shortcodes, a settings page, and security.
Why build a plugin instead of editing your theme?
The rule is simple: anything that must survive a theme change belongs in a plugin. A custom post type, a shortcode, or a block your editors rely on should keep working after a redesign — but code in functions.php is tied to the active theme, so switching themes silently drops the feature.
A plugin also gives you clean boundaries: its own folder, settings, and lifecycle — activate, deactivate, delete. For the theme side, see my guide on how to create a custom WordPress theme from scratch.
What you need before you start
You need a local WordPress site, a code editor such as VS Code, and basic PHP. Never build on a live site — a single fatal error in your main file can lock you out of the dashboard, and on a local site that mistake costs seconds, not a client emergency.
Use LocalWP for the fastest setup, or the official wp-env tool if you are comfortable with Docker and Node.js. Build against a current version — WordPress 7.1.4 is the production version as of late November 2026 — and PHP 8.3 or newer (PHP 7.4 is the minimum still supported). Before you write anything, pick three names: the plugin name, the folder slug, and a short function prefix — here, Reading Time, reading-time, and rt_.
Step 1: Create the plugin folder and main file
WordPress finds plugins by scanning wp-content/plugins/ for PHP files that start with a plugin header comment. Create a folder named reading-time there, then a file inside it with the same name: reading-time.php.
Open the file and add the header — only Plugin Name is required, but a releasable plugin fills in the rest:
<?php
/**
* Plugin Name: Reading Time
* Description: Shows an estimated reading time after each blog post.
* Version: 1.0.0
* Requires at least: 6.5
* Requires PHP: 7.4
* Author: Amaar Ahmed
* Author URI: https://amaar.site
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: reading-time
*/
Two fields matter most. Requires at least and Requires PHP declare the oldest versions your code supports, and WordPress refuses to activate the plugin on anything older. Text Domain must match your plugin slug; it is what translations use. Set these fields honestly: if you use a function that only exists in WordPress 6.5, say so.
Step 2: Add the direct-access guard and a unique prefix
Directly under the header, add the guard that stops anyone from loading your PHP file on its own, outside WordPress:
// Stop direct access: this file should only ever be loaded by WordPress.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Every function, option name, CSS class, and hook should start with rt_ or reading-time. WordPress has no namespaces for plugin functions by default, so a generic name like add_reading_time() can collide with another plugin and crash the site with a fatal “cannot redeclare function” error. Prefixing is the cheapest insurance when you create a WordPress plugin.
Step 3: Make it do something with hooks
Hooks are how your plugin talks to WordPress: actions run code at a specific moment (when a post is saved), filters change a value before WordPress uses it (the content of a post). Our reading-time feature needs a filter: the_content.
Add this function to your main file — it counts the words in a post, divides by 200 words per minute, and appends a short line after the content:
function rt_add_reading_time( $content ) {
if ( ! is_single() ) {
return $content;
}
$words = str_word_count( wp_strip_all_tags( $content ) );
$minutes = max( 1, ceil( $words / 200 ) );
$notice = sprintf(
'<p class="rt-reading-time">Estimated reading time: %d minute(s).</p>',
$minutes
);
return $content . $notice;
}
add_filter( 'the_content', 'rt_add_reading_time' );
Three details matter: is_single() keeps the notice off archive pages; wp_strip_all_tags() removes HTML before counting; and the function returns the modified content — a filter must always return a value, or the post renders blank.
Step 4: Add a shortcode
For manual placement, register a shortcode with add_shortcode():
function rt_reading_time_shortcode( $atts ) {
$atts = shortcode_atts(
array( 'minutes' => 5 ),
$atts,
'reading_time'
);
return sprintf(
'<span class="rt-reading-time">Reading time: %d minutes.</span>',
absint( $atts['minutes'] )
);
}
add_shortcode( 'reading_time', 'rt_reading_time_shortcode' );
Now anyone can type [reading_time minutes=”8″] in a post and get the label in that exact place. Two rules apply to every shortcode callback: it must return its output, never echo it (echoed output lands at the top of the page), and you must sanitize the attributes — absint() above guarantees the minutes value is a positive integer.
Step 5: Add a settings page
A hardcoded speed is fine for version 1, but a real plugin lets the site owner change it. The Settings API builds that screen for you — form, saving, and nonce check included. Register one setting and one options page:
function rt_register_settings() {
register_setting(
'rt_settings',
'rt_words_per_minute',
array(
'type' => 'integer',
'sanitize_callback' => 'absint',
'default' => 200,
)
);
add_settings_section(
'rt_main',
'Reading Time Settings',
'__return_false',
'rt-settings'
);
add_settings_field(
'rt_wpm',
'Words per minute',
'rt_wpm_field',
'rt-settings',
'rt_main'
);
}
add_action( 'admin_init', 'rt_register_settings' );
function rt_add_settings_page() {
add_options_page(
'Reading Time',
'Reading Time',
'manage_options',
'rt-settings',
'rt_settings_page'
);
}
add_action( 'admin_menu', 'rt_add_settings_page' );
The manage_options capability restricts the page to administrators, and absint stores even a malicious submitted value as a safe integer. Then update the Step 3 filter to read get_option( 'rt_words_per_minute', 200 ).
Step 6: Handle activation, deactivation, and uninstall
A plugin has three lifecycle moments, each with its own hook. On activation, set your defaults:
function rt_activate() {
add_option( 'rt_words_per_minute', 200 );
}
register_activation_hook( __FILE__, 'rt_activate' );
On deactivation, clean up anything temporary — clear a scheduled cron event or flush rewrite rules if you registered a post type — but do not delete the user’s settings: they may be switching the plugin off for five minutes to test a conflict, as my guide on how to fix WordPress plugin conflicts explains.
On uninstall — only when the plugin is deleted, not deactivated — remove everything with an uninstall.php file:
<?php
// Exit if this file is called directly.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
delete_option( 'rt_words_per_minute' );
Without the WP_UNINSTALL_PLUGIN check, anyone who guesses the file URL could trigger your cleanup code. Remember: activation sets defaults, deactivation pauses, uninstall removes.
Step 7: Security basics every plugin needs
Security is four habits. First, check capabilities: any admin screen, Ajax handler, or form processor must verify the user is allowed to be there — current_user_can( 'manage_options' ) or the fitting capability. Second, use nonces: every form should include wp_nonce_field(), and every handler must verify it with check_admin_referer().
Third, sanitize on save (sanitize_text_field(), absint()) and escape on display (esc_html(), esc_attr(), esc_url()). Sanitizing cleans data before it is stored; escaping makes stored data safe to print. Fourth, never build SQL by concatenating user input — use $wpdb->prepare() for every custom query. My WordPress security hardening guide covers the same principles at the site level.
Step 8: Test it with Plugin Check before you ship
Before your plugin touches a client site, run it through the official Plugin Check tool: install it from the directory, go to Tools > Plugin Check, select your plugin, and run the checks — dozens of automated checks across Plugin Repo, Security, Performance, and Accessibility, also available as wp plugin check reading-time.
A directory submission must pass every check in the Plugin Repo category; the tool also flags unprefixed functions, missing text domains, late escaping, and direct database queries — the exact issues this guide warned you about. Passing Plugin Check does not guarantee approval: a human team still reviews every submission against the eighteen Detailed Plugin Guidelines, and your code must be GPL-compatible. Add a readme.txt with matching version values before you submit, and keep the WordPress Plugin Developer Handbook open while you build.
Common mistakes to avoid
Most first plugins fail in the same few ways. Generic function names without a prefix top the list — one other plugin with the same name white-screens the site. Echoing output from a shortcode or a filter is second; always return, never echo. Writing files into the plugin folder at runtime is third — plugin folders are not always writable, and updates wipe them, so use the uploads directory or the options table.
Two more: hardcoding URLs and paths instead of plugin_dir_url( __FILE__ ) and plugin_dir_path( __FILE__ ); and shipping without a clean-install test, where the missing default option you never noticed on your dev site finally shows up.
Frequently asked questions
Do I need to know PHP to create a WordPress plugin?
Basic PHP is enough: variables, functions, arrays, and conditionals — no object-oriented PHP or framework required. You do need a working understanding of actions and filters.
How do I submit my plugin to WordPress.org?
Create a WordPress.org account, prepare a readme.txt and the plugin files, run Plugin Check until the Plugin Repo category is clean, and submit through the directory’s Add Your Plugin page. Once approved, upload releases through Subversion. Expect the first review to take days, not hours.
Your first plugin is a foundation, not a finish line
You now have a working plugin: header, filter, shortcode, settings page, lifecycle hooks, and security habits. The same structure scales — the next feature is another hook, another settings field, or a custom post type in its own plugin instead of the theme.
Keep building: add a block version of the shortcode, store the reading time as post meta, or submit it to the directory and learn from the review. And if you need a custom plugin for your business — a booking flow, a client portal, an integration with your tools — get in touch. I build custom WordPress plugins and themes for clients and agencies, and I reply within 24 hours.