Black Friday Save 45% on any WPAssist Hub plugin See deals
WordPress Development
By WPAssist Hub TeamUpdated Nov 15, 20265 min read

How to Create a WordPress Plugin from Scratch (Beginner’s Guide)

Illustration of code brackets next to a WordPress plugin icon
In this article9 sections
  1. What you need before you start
  2. Step 1: Create the folder and the main file
  3. Step 2: Add the plugin header
  4. Step 3: Hook your code into WordPress
  5. Step 4: Add a settings page
  6. Step 5: Security habits you must not skip
  7. Step 6: Test and package it
  8. Frequently asked questions
  9. Summary

A plugin is just a PHP file that WordPress loads. That is all. If you can write a few lines of PHP, you can create a WordPress plugin, and it is the right way to add features that should survive a theme change.

This guide builds a small but real plugin, one that prints a note in your site footer and has its own settings page. Along the way you will learn the structure, the hooks and the security habits every plugin needs.

What you need before you start

  • A local WordPress site to test on. Tools such as LocalWP or XAMPP work well. Do not experiment on a live site.
  • A code editor such as VS Code.
  • Basic PHP. Functions, arrays and if statements are enough.
  • WP_DEBUG turned on in wp-config.php, so you see errors while you work.
Infographic showing six steps to create a WordPress plugin

Step 1: Create the folder and the main file

Go to wp-content/plugins/ and create a folder named hello-footer-note. Inside it, create a file called hello-footer-note.php. Use the same name for the folder and the main file, in lowercase with hyphens.

Step 2: Add the plugin header

The comment at the top of the file is what makes WordPress recognise your plugin:

<?php
/**
 * Plugin Name:       Hello Footer Note
 * Description:       Adds a short note to the bottom of every page.
 * Version:           1.0.0
 * Requires at least: 6.0
 * Requires PHP:      7.4
 * Author:            Your Name
 * License:           GPL-2.0-or-later
 * Text Domain:       hello-footer-note
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Stop direct access to this file.
}

Only Plugin Name is required, but the other fields help users and are needed if you plan to submit to WordPress.org. The ABSPATH check stops someone from loading your file directly in a browser.

Now go to Plugins in your dashboard. You should see “Hello Footer Note” in the list. Activate it.

Step 3: Hook your code into WordPress

WordPress runs code at specific moments called hooks. You attach your function to one with add_action(). Our plugin prints a note in the footer:

function hfn_print_note() {
    $note = get_option( 'hfn_note', 'Thanks for visiting!' );
    echo '<p class="hfn-note">' . esc_html( $note ) . '</p>';
}
add_action( 'wp_footer', 'hfn_print_note' );

Reload your site and scroll to the bottom. If you want to understand hooks properly, read our guide to WordPress hooks: actions vs filters.

Note: The function name starts with hfn_. A unique prefix stops your function from clashing with one in another plugin or the theme.

Step 4: Add a settings page

Letting the site owner change the note needs a settings page. WordPress has the Settings API for this:

function hfn_add_menu() {
    add_options_page( 'Footer Note', 'Footer Note', 'manage_options', 'hfn-settings', 'hfn_render_page' );
}
add_action( 'admin_menu', 'hfn_add_menu' );

function hfn_register_settings() {
    register_setting( 'hfn_group', 'hfn_note', array(
        'type'              => 'string',
        'sanitize_callback' => 'sanitize_text_field',
        'default'           => 'Thanks for visiting!',
    ) );
}
add_action( 'admin_init', 'hfn_register_settings' );

function hfn_render_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    ?>
    <div class="wrap">
        <h1>Footer Note</h1>
        <form method="post" action="options.php">
            <?php settings_fields( 'hfn_group' ); ?>
            <input type="text" name="hfn_note" class="regular-text"
                   value="<?php echo esc_attr( get_option( 'hfn_note', '' ) ); ?>" />
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

Go to Settings → Footer Note, change the text and save. Your footer updates.

Step 5: Security habits you must not skip

Plugins run on thousands of sites, so small mistakes become big problems. Build these into every plugin:

  • Sanitize input when you save it (sanitize_text_field(), absint(), esc_url_raw()).
  • Escape output when you print it (esc_html(), esc_attr(), esc_url()).
  • Check permissions with current_user_can() before any admin action.
  • Use nonces on custom forms and AJAX requests to prevent forged requests.
  • Never trust $_POST or $_GET. Treat them as hostile until cleaned.

Our WordPress security checklist covers what site owners should do on their side.

Step 6: Test and package it

  1. Keep WP_DEBUG on and watch for notices and warnings.
  2. Test with a default theme such as Twenty Twenty-Five, and with other plugins deactivated.
  3. Test on the lowest PHP version you claim to support.
  4. Zip the plugin folder so the zip contains hello-footer-note/hello-footer-note.php.
  5. Install the zip on a clean test site to confirm it activates.

If you plan to publish on WordPress.org, you will also need a readme.txt file in the correct format, a GPL-compatible license, and code that passes their review.

Frequently asked questions

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

Yes, you need basic PHP, along with some HTML. JavaScript becomes important if you want to build blocks or dynamic admin screens.

Can I use classes instead of functions?

Yes. Larger plugins usually use classes or namespaces to keep code organised. Functions with a unique prefix are fine for small plugins.

Should I put my code in the theme’s functions.php instead?

Only for code tied to that theme’s design. Anything that should keep working after a theme change belongs in a plugin.

Summary

A plugin is a folder, a header comment and a few hooks. Start small, prefix your names, sanitise and escape everything, and test with debug mode on. Once this feels comfortable, move on to custom post types and custom fields or a custom REST API endpoint.

WPAssist Hub
Written byWPAssist Hub Team

We build lightweight WordPress plugins and write practical, tested guides for site owners and developers. Our team is led by a WordPress Core Contributor with 6+ years of WordPress experience.

About us →

Leave a Comment

Your email address will not be published. Required fields are marked *