Building a custom WordPress plugin might seem intimidating, but once you understand the fundamentals, it’s one of the most powerful skills in WordPress development. This guide walks you through building your first plugin from scratch.
What is a WordPress Plugin?
A WordPress plugin is a PHP file (or collection of files) that extends WordPress functionality using WordPress’s hook system — actions and filters. Plugins add features without modifying WordPress core files, which means they survive updates intact.
Step 1: Create the Plugin File
Navigate to wp-content/plugins/ and create a new folder: my-custom-plugin/. Inside it, create my-custom-plugin.php with this header:
<?php
/**
* Plugin Name: My Custom Plugin
* Plugin URI: https://example.com
* Description: A custom plugin that does something useful
* Version: 1.0.0
* Author: Your Name
* License: GPL v2
*/
defined('ABSPATH') || exit;
This header comment is what WordPress reads to identify your plugin. The defined('ABSPATH') check prevents direct file access.
Step 2: Understanding Actions and Filters
WordPress hooks are the foundation of plugin development. There are two types:
- Actions — allow you to execute code at specific points. Example: run code when a post is published.
- Filters — allow you to modify data. Example: change the excerpt length.
// Action: run my function when WordPress initializes
add_action('init', 'my_plugin_init');
function my_plugin_init() {
// your code here
}
// Filter: modify the excerpt length
add_filter('excerpt_length', function($length) {
return 30;
});
Step 3: Register a Custom Post Type
One of the most common plugin tasks is registering a custom post type (CPT):
add_action('init', 'register_products_cpt');
function register_products_cpt() {
register_post_type('product_item', [
'labels' => ['name' => 'Products', 'singular_name' => 'Product'],
'public' => true,
'supports' => ['title', 'editor', 'thumbnail'],
'rewrite' => ['slug' => 'products'],
]);
}
Step 4: Add a Shortcode
Shortcodes let users add your plugin’s output anywhere in their content:
add_shortcode('my_greeting', 'my_greeting_shortcode');
function my_greeting_shortcode($atts) {
$atts = shortcode_atts(['name' => 'World'], $atts);
return '<p>Hello, ' . esc_html($atts['name']) . '!</p>';
}
Users can then write [my_greeting name="John"] in any post or page.
Step 5: Create an Admin Page
add_action('admin_menu', 'my_plugin_admin_menu');
function my_plugin_admin_menu() {
add_options_page(
'My Plugin Settings',
'My Plugin',
'manage_options',
'my-plugin',
'my_plugin_settings_page'
);
}
function my_plugin_settings_page() {
echo '<div class="wrap"><h1>My Plugin Settings</h1></div>';
}
Step 6: Save Settings Securely
Always sanitize input and verify nonces when saving settings:
if (isset($_POST['my_option'])) {
check_admin_referer('my_plugin_save');
update_option('my_option', sanitize_text_field($_POST['my_option']));
}
Next Steps
Once you’ve mastered the basics, explore the WordPress Plugin Handbook at developer.wordpress.org. Topics to learn next: custom database tables with $wpdb, AJAX in WordPress, REST API endpoints, and WooCommerce integration.
Need a custom plugin built professionally? Contact our plugin development team.
Leave a Reply