Introduction: Why Build a WordPress Plugin?
WordPress is incredibly powerful out-of-the-box, but its true flexibility comes from plugins. Plugins allow you to extend, modify, or add new functionality to your WordPress site without altering its core code. This makes your site robust, easy to update, and highly customizable. For WordPress users, understanding plugins unlocks endless possibilities, and for developers, it’s a gateway to solving unique problems, automating tasks, and even building a business.
In this comprehensive guide, we’ll walk you through creating a simple, functional plugin that will:
- Be recognized by WordPress.
- Display a custom message using a shortcode.
- Add a basic settings page to the WordPress admin dashboard.
Step 1: Setting Up Your Plugin File and Header
Every WordPress plugin starts with a main PHP file containing a specific header. This header provides WordPress with essential information about your plugin.
Create a Plugin Folder: Navigate to your WordPress installation’s
wp-content/plugins/directory. Create a new folder, for example,my-first-plugin.Create the Main Plugin File: Inside your new folder, create a PHP file with the same name as your folder (or a very similar one), e.g.,
my-first-plugin.php.Add the Plugin Header: Open
my-first-plugin.phpand add the following code. This is the absolute minimum required for WordPress to recognize your plugin:<?php /* Plugin Name: My First Plugin Description: A simple plugin to demonstrate WordPress plugin development. Version: 1.0 Author: Your Name Author URI: https://yourwebsite.com License: GPL2 License URI: https://www.gnu.org/licenses/gpl-2.0.html Text Domain: my-first-plugin */ // Your plugin's code will go here.Explanation of Header Fields:
Plugin Name: The name displayed in the WordPress Plugins list.Description: A brief summary of what your plugin does.Version: The current version number of your plugin.Author: Your name or company name.Author URI: A link to your website or profile.License: The license under which your plugin is released (GPL2 is common).License URI: A link to the license text.Text Domain: Used for internationalization (translating your plugin). Should match your plugin’s folder name.
Save the file. Now, if you go to Plugins > Installed Plugins in your WordPress admin, you should see "My First Plugin" listed!
Step 2: Understanding WordPress Hooks (Actions & Filters)
Hooks are the core of WordPress plugin development. They allow your plugin to "hook into" specific points in the WordPress execution cycle to run your custom code or modify existing data. There are two main types:
Actions: Doing Something
Actions allow you to execute a function at a specific point in WordPress. You use add_action() to register your function with a particular action hook.
/**
* Example Action: Add text to the footer.
*/
function my_first_plugin_footer_text() {
echo '<p>Hello from My First Plugin!</p>';
}
add_action('wp_footer', 'my_first_plugin_footer_text');
In this example, my_first_plugin_footer_text will be called when WordPress executes the wp_footer action (typically just before the closing </body> tag).
Filters: Modifying Something
Filters allow you to modify data before it’s used or displayed by WordPress. You use add_filter() to register your function, and your function must return a value.
/**
* Example Filter: Prepend text to post titles.
*/
function my_first_plugin_modify_title($title) {
return 'Awesome: ' . $title;
}
add_filter('the_title', 'my_first_plugin_modify_title');
Here, my_first_plugin_modify_title will be called whenever WordPress prepares to display a post title (via the_title filter), allowing you to prepend "Awesome: " to it.
For our simple plugin, we’ll primarily use actions to add functionality.
Step 3: Creating a Shortcode
Shortcodes provide a convenient way for users to insert specific plugin functionality into posts, pages, or widgets using a simple tag like [my_shortcode].
Define your Shortcode Function: This function will return the content you want the shortcode to display.
/** * Shortcode callback function. * Displays a custom greeting message. * * @param array $atts Shortcode attributes. * @return string The message to display. */ function my_first_plugin_message_shortcode($atts) { // Define default attributes and merge with user-provided ones $atts = shortcode_atts( array( 'name' => 'WordPress User', ), $atts, 'my_message' ); return '<p>Hello, ' . esc_html($atts['name']) . ' from My First Plugin!</p>'; }Register the Shortcode: Use
add_shortcode()to link your tag to your function.add_shortcode('my_message', 'my_first_plugin_message_shortcode');
Now, if you activate your plugin and add [my_message] or [my_message name="Developer"] to any post or page, you’ll see your custom greeting!
Step 4: Adding a Basic Admin Menu Page
Plugins often need a way for administrators to configure settings. We can add a custom menu item to the WordPress admin dashboard that leads to our plugin’s settings page.
Create a function to add the menu page: This function will use
add_menu_page()oradd_submenu_page()./** * Adds a top-level menu page to the admin dashboard. */ function my_first_plugin_admin_menu() { add_menu_page( 'My First Plugin Settings', 'My First Plugin', 'manage_options', 'my-first-plugin-slug', 'my_first_plugin_settings_page_callback', 'dashicons-admin-generic', 99 // Position in the menu, higher number means lower down ); } add_action('admin_menu', 'my_first_plugin_admin_menu');'My First Plugin Settings': The<title>tag text for the page.'My First Plugin': The menu title visible in the admin sidebar.'manage_options': The capability required to access this menu item.'my-first-plugin-slug': A unique slug for your menu page.'my_first_plugin_settings_page_callback': The function that renders the content of the page.'dashicons-admin-generic': The Dashicon class for the menu icon.99: The position of the menu item.
Create the callback function for the page content: This function outputs the HTML for your settings page.
/** * Renders the content for the admin settings page. */ function my_first_plugin_settings_page_callback() { // Check user capabilities if (!current_user_can('manage_options')) { return; } ?> <div class="wrap"> <h1><?php echo esc_html(get_admin_page_title()); ?></h1> <p>Welcome to your very first WordPress Plugin settings page!</p> <p>You can add forms, options, and more advanced UI elements here.</p> </div> <?php }
After saving, activate your plugin. You should now see "My First Plugin" in your WordPress admin sidebar!
Putting It All Together: Your First Functional Plugin
Here’s the complete code for your my-first-plugin.php file:
<?php
/*
Plugin Name: My First Plugin
Description: A simple plugin to demonstrate WordPress plugin development.
Version: 1.0
Author: Your Name
Author URI: https://yourwebsite.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: my-first-plugin
*/
// Exit if accessed directly to prevent unauthorized access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Shortcode callback function.
* Displays a custom greeting message.
*
* @param array $atts Shortcode attributes.
* @return string The message to display.
*/
function my_first_plugin_message_shortcode($atts) {
// Define default attributes and merge with user-provided ones
$atts = shortcode_atts( array(
'name' => 'WordPress User',
), $atts, 'my_message' );
return '<p>Hello, ' . esc_html($atts['name']) . ' from My First Plugin!</p>';
}
add_shortcode('my_message', 'my_first_plugin_message_shortcode');
/**
* Adds a top-level menu page to the admin dashboard.
*/
function my_first_plugin_admin_menu() {
add_menu_page(
'My First Plugin Settings',
'My First Plugin',
'manage_options',
'my-first-plugin-slug',
'my_first_plugin_settings_page_callback',
'dashicons-admin-generic',
99
);
}
add_action('admin_menu', 'my_first_plugin_admin_menu');
/**
* Renders the content for the admin settings page.
*/
function my_first_plugin_settings_page_callback() {
// Check user capabilities
if (!current_user_can('manage_options')) {
return;
}
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<p>Welcome to your very first WordPress Plugin settings page!</p>
<p>You can add forms, options, and more advanced UI elements here.</p>
</div>
<?php
}
To activate and test:
- Save this code as
my-first-plugin.phpinsidewp-content/plugins/my-first-plugin/. - Go to Plugins > Installed Plugins in your WordPress admin.
- Click "Activate" next to "My First Plugin."
- Add
[my_message]to a post or page and view it. - Look for "My First Plugin" in your admin sidebar and click it.
Conclusion and Next Steps
Congratulations! You’ve successfully built your first WordPress plugin. You’ve learned about essential components like the plugin header, how to leverage hooks (actions and filters) to extend functionality, create user-friendly shortcodes, and add basic administrative interfaces.
This is just the beginning. To take your plugin development skills further, explore topics such as:
- WordPress Settings API: For creating robust and secure settings pages.
- Security Best Practices: Sanitization, validation, nonces.
- Internationalization: Making your plugin translatable.
- Enqueuing Scripts and Styles: Properly adding CSS and JavaScript.
- Object-Oriented Programming (OOP): Structuring larger plugins.
- Database Interaction: Using
$wpdbfor custom tables.
Keep experimenting, reading documentation, and building! The WordPress developer community is vast and supportive, offering endless resources for learning and growth.

