A WordPress plugin can be a single PHP file. If you’ve ever pasted code into a theme’s functions.php, you can write a plugin, and it’s the better home for that code: it survives theme changes and updates, and you can switch it off in one click. This guide builds a small, real plugin step by step.
What you’ll build
- A plugin that WordPress recognises, with a proper header.
- An action that adds something to every page and a filter that changes content.
- A shortcode you can use in any post.
- A saved setting, plus the security basics every plugin needs.
Before you start
Work on a local or staging site, never on a live one: a PHP error in a plugin can take the site down until you remove the file. Local tools like LocalWP, XAMPP or wp-env take minutes to set up. Turn on debugging in wp-config.php so you see errors:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true ); // Errors go to wp-content/debug.log
define( 'WP_DEBUG_DISPLAY', false );Step 1: create the plugin file
Create a folder wp-content/plugins/my-first-plugin/ and inside it a file my-first-plugin.php. The comment block at the top is what makes WordPress see it as a plugin:
<?php
/**
* Plugin Name: My First Plugin
* Description: Small site tweaks, kept out of the theme.
* Version: 1.0.0
* Author: Your Name
* Requires at least: 6.0
* Requires PHP: 7.4
* License: GPL-2.0-or-later
* Text Domain: my-first-plugin
*/
defined( 'ABSPATH' ) || exit; // Stop if someone opens the file directly.Go to Plugins in the admin. “My First Plugin” is listed; activate it. It does nothing yet, which is exactly right.
Step 2: hook into WordPress with an action
Plugins work by attaching functions to hooks: points where WordPress says “anyone want to do something here?”. If hooks are new to you, our WordPress hooks explained guide covers them in plain English. An action runs your code at a moment. This one adds a small notice to the footer of every page:
add_action( 'wp_footer', 'mfp_footer_note' );
function mfp_footer_note() {
echo '<p class="mfp-note">' . esc_html__( 'Thanks for visiting!', 'my-first-plugin' ) . '</p>';
}Note the mfp_ prefix. Every function in WordPress shares one global space, so a unique prefix stops your function names clashing with another plugin’s.
Step 3: change content with a filter
A filter receives a value, lets you change it, and must return it. This one adds a “last updated” line to the end of every blog post:
add_filter( 'the_content', 'mfp_last_updated' );
function mfp_last_updated( $content ) {
if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
return $content; // Always return the value, even when you change nothing.
}
$date = get_the_modified_date();
return $content . '<p class="mfp-updated">' . esc_html( sprintf( __( 'Last updated: %s', 'my-first-plugin' ), $date ) ) . '</p>';
}Forgetting the return is the most common beginner bug with filters: the post content simply disappears.
Step 4: add a shortcode
Shortcodes let editors drop your output into any post or page. This one shows the current year, handy for copyright lines, with an optional prefix:
add_shortcode( 'mfp_year', 'mfp_year_shortcode' );
function mfp_year_shortcode( $atts ) {
$atts = shortcode_atts( array( 'prefix' => '' ), $atts, 'mfp_year' );
return esc_html( trim( $atts['prefix'] . ' ' . wp_date( 'Y' ) ) ); // Return, never echo.
}Use it as [mfp_year prefix="©"]. Shortcodes must return their output; echoing puts it in the wrong place on the page.
Step 5: save a setting
Let’s make the footer text editable under Settings → General, using the Settings API so WordPress handles saving and security checks:
add_action( 'admin_init', 'mfp_register_setting' );
function mfp_register_setting() {
register_setting( 'general', 'mfp_footer_text', array( 'sanitize_callback' => 'sanitize_text_field', 'default' => 'Thanks for visiting!' ) );
add_settings_field( 'mfp_footer_text', 'Footer note', 'mfp_field_html', 'general' );
}
function mfp_field_html() {
printf( '<input type="text" class="regular-text" name="mfp_footer_text" value="%s">', esc_attr( get_option( 'mfp_footer_text' ) ) );
}Then change mfp_footer_note() to print esc_html( get_option( 'mfp_footer_text' ) ). The note is now editable without touching code.
Security basics every plugin needs
- Escape everything you output:
esc_html()for text,esc_attr()for attributes,esc_url()for links. - Sanitize everything you save:
sanitize_text_field(),absint(),sanitize_email(). - Check permissions before doing anything privileged:
current_user_can( 'manage_options' ). - Use nonces on your own forms and AJAX calls (
wp_nonce_field()/check_admin_referer()) to block forged requests. - Use
$wpdb->prepare()for any custom database query.
Where to go next
- Load CSS and JS properly with
wp_enqueue_style()andwp_enqueue_script()on thewp_enqueue_scriptsaction, and only on pages that need them. - Clean up with an
uninstall.phpfile that deletes your options when the plugin is removed. - Extend other plugins. The same action/filter pattern lets your plugin change WooCommerce, Contact Form 7 or any plugin with hooks. Browse 700,000+ plugin hooks in our directory, or start with how to find a plugin’s hooks.
- Make your plugin extendable by adding your own hooks with
do_action()andapply_filters().
Need something bigger than a first plugin, like a custom integration or a change to how an existing plugin behaves? Tell us what you need and we’ll build it the update-safe way.
Frequently asked questions
Plugin or functions.php: where should custom code go?
Code that changes how the site looks can live in a child theme. Anything that adds features (shortcodes, custom post types, integrations) belongs in a plugin, so it keeps working if you change theme.
Do I need to know PHP to create a WordPress plugin?
Basic PHP is enough for small plugins like this one. You’ll pick up WordPress’s own functions as you go; the official Plugin Handbook is the best reference.
Can I publish my plugin on WordPress.org?
Yes, for free, if it’s GPL-compatible and passes the plugin team’s review. Add a readme.txt in the WordPress.org format and submit it from the “Add your plugin” page.
Leave a Reply