WordPress is powered by plugins. From SEO to e-commerce, plugins extend functionality and make websites highly customizable. But what if you want to create your own? Whether it’s a custom feature for a client or a public plugin you plan to release, building a WordPress plugin from scratch is simpler than it looks.
In this step-by-step guide, you’ll learn how to create a WordPress plugin from scratch, from setup to coding best practices.
What Is a WordPress Plugin?
A plugin is a piece of software that adds new features or extends existing functionality in WordPress without modifying core files.
Examples:
- Contact forms (WPForms, Contact Form 7)
- SEO tools (Yoast SEO, Rank Math)
- Online stores (WooCommerce)
Instead of hacking WordPress core or your theme, plugins allow modular development that is portable and easy to maintain.
Why Create a WordPress Plugin?
You might want to create a plugin if:
- A client needs a custom feature not available in existing plugins.
- You want to lightweight alternatives to heavy plugins.
- You plan to release plugins publicly (free or premium).
- You want to learn WordPress development.
Step 1: Setting Up Your Development Environment
Before writing code, prepare your setup:
- Local Server: Install Local by Flywheel, XAMPP, or MAMP.
- Text Editor/IDE: Use VS Code, PhpStorm, or Sublime Text.
- WordPress Installation: Have a fresh WordPress site ready for testing.
This keeps development isolated and safe from breaking live sites.
Step 2: Creating the Plugin Folder & File
- Navigate to
wp-content/plugins/. - Create a new folder for your plugin, e.g.,
my-first-plugin. - Inside it, create a PHP file with the same name:
my-first-plugin.php.
At the top of this file, add plugin header comments:
<?php
/*
Plugin Name: My First Plugin
Plugin URI: https://example.com/
Description: A simple custom plugin for WordPress.
Version: 1.0
Author: Your Name
Author URI: https://yourwebsite.com/
License: GPL2
*/
This tells WordPress about your plugin. After saving, you’ll see it in the Plugins → Installed Plugins section.
Step 3: Writing Your First Plugin Code
Let’s start small. Add this code to your my-first-plugin.php file:
<?php
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Simple function
function mfp_hello_world() {
echo "<p>Hello, World! This is my first WordPress plugin.</p>";
}
// Hook into WordPress footer
add_action('wp_footer', 'mfp_hello_world');
When activated, this plugin will print a message in your site’s footer.
Step 4: Adding Shortcodes
Shortcodes make plugins more user-friendly. Example:
function mfp_greeting_shortcode() {
return "Hello! Thanks for visiting my site.";
}
add_shortcode('greeting', 'mfp_greeting_shortcode');
Now, adding [greeting] in a post will display the message.
Step 5: Adding CSS & JavaScript
You can enqueue custom styles and scripts:
function mfp_enqueue_scripts() {
wp_enqueue_style('mfp-style', plugin_dir_url(__FILE__) . 'css/mfp-style.css');
wp_enqueue_script('mfp-script', plugin_dir_url(__FILE__) . 'js/mfp-script.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'mfp_enqueue_scripts');
Create /css/mfp-style.css and /js/mfp-script.js in your plugin folder.
Step 6: Adding Admin Menu Pages
To add a custom settings page in the WordPress dashboard:
function mfp_add_admin_menu() {
add_menu_page(
'My Plugin Settings',
'My Plugin',
'manage_options',
'my-first-plugin',
'mfp_settings_page',
'dashicons-admin-generic',
20
);
}
add_action('admin_menu', 'mfp_add_admin_menu');
function mfp_settings_page() {
echo "<h1>My First Plugin Settings</h1>";
echo "<p>Welcome to the settings page.</p>";
}
Step 7: Following Best Practices
- Always use prefixes (e.g.,
mfp_) to avoid conflicts. - Validate and sanitize user input (
sanitize_text_field(),esc_html()). - Load scripts and styles only where necessary.
- Keep functions modular and well-documented.
- Test thoroughly before release.
Step 8: Packaging & Sharing Your Plugin
When your plugin is ready:
- Zip the folder.
- Share it manually or upload it to the WordPress Plugin Repository (if open-source).
- If premium, consider platforms like CodeCanyon or your own website.
Final Thoughts
Creating a plugin may sound complex, but it all starts with a simple PHP file. From there, you can expand with shortcodes, admin pages, settings, and more. With practice, you can go from simple tweaks to building full-featured plugins that power businesses.
Ready to level up your skills? Start today and create a WordPress plugin from scratch—your next big idea could power thousands of websites!



