To build a WordPress plugin, create a folder in wp-content/plugins, add a PHP file with a plugin header comment, then hook your own functions into WordPress with actions and filters. Activate it from the Plugins screen in your dashboard and your code starts running. From there you add shortcodes, a settings page, activation and uninstall routines, and security checks. Below I walk through a small working plugin step by step, explain the security basics, and cover what it takes to publish on wordpress.org.

Most people who want to build a WordPress plugin start with a small itch: a snippet they keep pasting into functions.php, a feature a client keeps asking for, or a tool they wish existed. That’s the right way to start. You don’t need a big idea to build a WordPress plugin that’s useful.

I build custom plugins for clients, and I’ve published my own on wordpress.org: AltScribe, which writes alt text for images with AI. Going through the official review taught me more about writing clean plugin code than any tutorial did, so I’ll point out what reviewers look for along the way.

This guide assumes you know basic PHP: variables, functions, arrays and if statements. You don’t need object-oriented programming or JavaScript to build a WordPress plugin like the one below. Everything here follows the official WordPress Plugin Handbook.

What is a WordPress plugin?

A plugin is a PHP file, or a folder of files, that adds features to WordPress without changing WordPress core. WordPress loads every active plugin on each request, and your code runs at the points where you hook into it.

The big advantage over pasting code into your theme’s functions.php is that a plugin stays put when you change themes. If the feature belongs to the site, such as a custom post type, a shortcode or an integration, it belongs in a plugin. If it’s only about how one theme looks, it can stay in the theme. My post on custom vs premium themes covers that side.

What you need before you build a WordPress plugin

  • A local WordPress site. Tools like Local, Laragon or DevKinsta give you a test site on your own computer. Never write a new plugin on a live site.
  • A code editor. VS Code is free and has good PHP extensions.
  • Debug mode on. Set WP_DEBUG to true in wp-config.php on your local site so PHP notices and warnings show up while you work.
  • Basic PHP. Functions, arrays and conditionals are enough for a first plugin.

That’s the whole setup. You don’t need a framework, Composer or a build step to build a WordPress plugin for your first project.

How to build a WordPress plugin: the example

We’ll build a small plugin called MW Reading Time. It shows an estimated reading time above blog posts, offers a shortcode, and has a settings page for words per minute. It’s small enough to read in one sitting, but it uses the same parts as bigger plugins: a header, hooks, a shortcode, options, a settings screen and cleanup on uninstall.

Steps to build a WordPress plugin, from folder to release
The six steps we follow, from an empty folder to a plugin you can share.

Step 1: Create the folder and main file to build a WordPress plugin

Go to wp-content/plugins and create a folder called mw-reading-time. Inside it, create mw-reading-time.php. The folder name and main file name usually match. That name also becomes your plugin’s slug.

Every time you build a WordPress plugin, you start with a header comment. The Plugin Handbook’s header requirements say only Plugin Name is required, but fill in the rest so WordPress and users know what they’re installing:

<?php
/**
 * Plugin Name:       MW Reading Time
 * Description:       Shows an estimated reading time above blog posts.
 * Version:           1.0.0
 * Requires at least: 6.0
 * Requires PHP:      7.4
 * Author:            Your Name
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       mw-reading-time
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Stop if someone opens this file directly.
}

Save the file and open Plugins in your dashboard. MW Reading Time is already listed. You can activate it, although it doesn’t do anything yet. The ABSPATH check stops the file from running if someone loads it directly in the browser instead of through WordPress.

Step 2: Understand hooks, actions and filters

Hooks are how you build a WordPress plugin without touching core files. WordPress fires named hooks at set points while it loads a page. Your plugin attaches functions to them. The hooks chapter of the handbook explains both types:

Hook typeWhat it doesExample hookRegister with
ActionRuns your function at a point in timeinit, admin_menu, wp_enqueue_scriptsadd_action()
FilterReceives a value, changes it, returns itthe_content, the_title, excerpt_lengthadd_filter()

The rule that trips up beginners: a filter must always return a value. If your filter function forgets to return the content, the post appears empty. Actions don’t return anything.

Step 3: Build a WordPress plugin feature with a filter

Now the plugin does real work. Add these functions below the ABSPATH check. The first counts words and works out minutes. The second uses the the_content filter to put a line above the post text.

function mwrtn_get_minutes( $post_id ) {
	$content = get_post_field( 'post_content', $post_id );
	$words   = str_word_count( wp_strip_all_tags( $content ) );
	$wpm     = max( 1, (int) get_option( 'mwrtn_wpm', 200 ) );
	return max( 1, (int) ceil( $words / $wpm ) );
}

function mwrtn_add_to_content( $content ) {
	if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
		return $content;
	}
	$minutes = mwrtn_get_minutes( get_the_ID() );
	$label   = sprintf(
		/* translators: %d: number of minutes */
		_n( 'Reading time: %d minute', 'Reading time: %d minutes', $minutes, 'mw-reading-time' ),
		$minutes
	);
	return '<p class="mwrtn-time">' . esc_html( $label ) . '</p>' . $content;
}
add_filter( 'the_content', 'mwrtn_add_to_content' );

Notice three habits here. Every function starts with the prefix mwrtn_, so it won’t clash with another plugin’s function of the same name. The handbook’s best practices page recommends a unique prefix of at least four or five letters. The output passes through esc_html(). And the text is wrapped in a translation function so the plugin can be translated later.

Open a blog post and you’ll see the reading time above the content. You’ve just built a WordPress plugin that works.

Step 4: Add a shortcode

Some users will want the reading time somewhere else, like inside a page builder layout. A shortcode handles that. The function returns HTML; it never echoes it.

function mwrtn_shortcode() {
	$minutes = mwrtn_get_minutes( get_the_ID() );
	return '<span class="mwrtn-time">' . esc_html( $minutes ) . ' min read</span>';
}
add_shortcode( 'mwrtn_reading_time', 'mwrtn_shortcode' );

Now [mwrtn_reading_time] works in any post, page or shortcode widget. The shortcode name has the prefix too, for the same reason as the function names.

Step 5: Add a settings page

Hard-coded values are fine for a first test, but when you build a WordPress plugin for real users, it should let the site owner change them. WordPress has a Settings API that handles saving, sanitizing and security for you. This adds a Reading Time page under Settings:

function mwrtn_register_settings() {
	register_setting( 'mwrtn_settings', 'mwrtn_wpm', array(
		'type'              => 'integer',
		'sanitize_callback' => 'absint',
		'default'           => 200,
	) );
}
add_action( 'admin_init', 'mwrtn_register_settings' );

function mwrtn_add_settings_page() {
	add_options_page( 'Reading Time', 'Reading Time', 'manage_options', 'mwrtn', 'mwrtn_render_settings_page' );
}
add_action( 'admin_menu', 'mwrtn_add_settings_page' );

function mwrtn_render_settings_page() {
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}
	?>
	<div class="wrap">
		<h1>Reading Time</h1>
		<form action="options.php" method="post">
			<?php settings_fields( 'mwrtn_settings' ); ?>
			<label for="mwrtn_wpm">Words per minute</label>
			<input type="number" id="mwrtn_wpm" name="mwrtn_wpm" min="50"
				value="<?php echo esc_attr( get_option( 'mwrtn_wpm', 200 ) ); ?>">
			<?php submit_button(); ?>
		</form>
	</div>
	<?php
}

settings_fields() prints the hidden fields WordPress needs, including a nonce, and absint makes sure only a positive whole number is saved. The capability check means only administrators can see the page. That’s three security layers you get almost for free by using the Settings API instead of writing your own form handler.

Step 6: Activation, deactivation and uninstall

When you build a WordPress plugin properly, it sets up what it needs when activated and removes its data when deleted. The handbook covers activation and deactivation hooks and uninstall methods. In the main file:

function mwrtn_activate() {
	add_option( 'mwrtn_wpm', 200 );
}
register_activation_hook( __FILE__, 'mwrtn_activate' );

Then create a separate file called uninstall.php in the plugin folder:

<?php
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}
delete_option( 'mwrtn_wpm' );

Deactivation should not delete data. People deactivate plugins to troubleshoot and expect their settings to still be there when they switch back on. Only uninstall, which happens when someone deletes the plugin, should clean up.

Security basics every plugin needs

This is where most first plugins fail review, and where most security problems in the wild come from. When you build a WordPress plugin, treat every piece of data that comes from a user, a URL or the database as untrusted. The handbook’s security chapter breaks it into four habits:

Security checks when you build a WordPress plugin
Four habits that cover most plugin security problems.
HabitWhat it meansCommon functions
Sanitize inputClean data before you save itsanitize_text_field(), absint(), sanitize_email()
Escape outputMake data safe at the moment you print itesc_html(), esc_attr(), esc_url(), wp_kses_post()
Check noncesConfirm a request came from your own formwp_nonce_field(), check_admin_referer(), wp_verify_nonce()
Check capabilitiesConfirm the user is allowed to do itcurrent_user_can()

A simple way to remember it: sanitize early, escape late, and check who is asking before you do anything. For database queries with user input, use $wpdb->prepare(), never string concatenation.

How to organize files as the plugin grows

One file is fine when you build a WordPress plugin this small. Once it passes a few hundred lines, split it. A common structure looks like this:

mw-reading-time/
  mw-reading-time.php   (header, constants, loads the other files)
  uninstall.php
  readme.txt
  includes/             (PHP logic)
  admin/                (settings screens)
  assets/css/           (styles)
  assets/js/            (scripts)
  languages/            (translation files)

Load CSS and JavaScript with wp_enqueue_style() and wp_enqueue_script() on the wp_enqueue_scripts or admin_enqueue_scripts hooks, and only on the pages that need them. Plugins that load their files on every page are one of the reasons sites get slow, which I see constantly when I run speed audits.

Testing: did you build a WordPress plugin that works everywhere?

  • Keep WP_DEBUG on locally and fix every notice and warning, not only fatal errors.
  • Install Query Monitor to see hooks, database queries and PHP errors per page.
  • Run Plugin Check. The official Plugin Check plugin from WordPress.org tests your code against many of the same rules the review team uses.
  • Test with a default theme such as Twenty Twenty-Five, then with the theme you actually use.
  • Test on the lowest PHP and WordPress versions you list in your header.
  • Deactivate and delete it to confirm uninstall cleans up and nothing breaks.

Publishing on wordpress.org

You don’t have to publish. Many people build a WordPress plugin for one site and stay private. But if you want to share it, the WordPress.org directory gives you free hosting, updates through the dashboard and a support forum.

The process, from the handbook’s guide to planning, submitting and maintaining plugins:

  1. Read the detailed plugin guidelines. The first rule is that plugins must be GPL-compatible, and GPLv2 or later is recommended.
  2. Write a readme.txt in the format the directory expects, with a description, installation steps, FAQ and changelog.
  3. Zip the complete plugin and submit it with a short overview of what it does.
  4. Wait for review. WordPress.org says it reviews queued plugins within 14 business days, and contacts you if it finds problems.
  5. Once approved, you get a Subversion (SVN) repository. You commit your code there, tag releases, and users get updates.

When I submitted AltScribe, the useful part of the process was being forced to clean up things I’d have skipped otherwise: escaping every output, prefixing everything, and explaining clearly in the readme what the plugin sends to an outside AI service. If your plugin calls an external API, say so plainly. The guidelines require it.

Common mistakes when you build a WordPress plugin

  • No prefix on functions, options or shortcodes, causing clashes with other plugins.
  • Echoing unescaped data, which opens the door to cross-site scripting.
  • Forgetting to return a value in a filter, so content disappears.
  • Loading scripts everywhere instead of only where they’re needed.
  • Deleting data on deactivation instead of uninstall.
  • Developing on a live site, where one fatal error takes the whole site down.
  • Copying old tutorials that use deprecated functions. Check the handbook and the code reference when in doubt.

When you shouldn’t build your own plugin

Not every idea needs custom code. Before you build a WordPress plugin, search the directory. If a well-maintained plugin already does 90% of what you want, using it is usually cheaper than maintaining your own for years.

Building your own makes sense when the feature is specific to your business, when existing plugins are bloated for what you need, or when you’re connecting WordPress to a private system such as an internal API or CRM. It’s also the right call when you’re tired of pasting the same snippet into every site. Once you build a WordPress plugin, it also has to be kept up to date with new WordPress and PHP versions, which is part of the routine I describe in my list of WordPress maintenance tasks.

If the plugin is part of a bigger site project, it’s worth planning it with the rest of the build. I explain how I approach that in my guide to WordPress website development.

Frequently asked questions

How long does it take to build a WordPress plugin?

A small plugin like the reading time example can be built in an afternoon if you know basic PHP. Plugins with settings screens, custom database tables or outside API connections take days or weeks, mostly because of testing and security work.

Do I need to know PHP to build a WordPress plugin?

Yes, for anything beyond copying code. You need functions, arrays and conditionals for a first plugin. JavaScript becomes important if you build blocks for the block editor or interactive admin screens.

What is the minimum code a plugin needs?

A PHP file in wp-content/plugins with a header comment that includes Plugin Name. That alone makes WordPress list it on the Plugins screen. Everything else is the code you hook into WordPress.

Should I put custom code in functions.php or build a WordPress plugin?

Build a plugin for anything that should survive a theme change, such as shortcodes, custom post types or integrations. Keep theme-specific display tweaks in a child theme’s functions.php.

Is it free to publish a plugin on wordpress.org?

Yes. Hosting in the WordPress.org plugin directory is free. The plugin must be GPL-compatible, follow the plugin guidelines and pass a manual review before it’s listed.

How do I keep my plugin secure?

Sanitize input before saving, escape output when printing, check nonces on forms and actions, check user capabilities, and use $wpdb->prepare() for database queries. Running the Plugin Check tool catches many common problems.

Need a custom WordPress plugin built?

If you’d rather have someone else build a WordPress plugin for your site, or review one you’ve written, that’s a big part of my WordPress developer service. Custom plugins start at $199 and smaller jobs are $15 an hour. Tell me what the plugin needs to do and I’ll tell you honestly whether an existing plugin would do the job first.