Building a WordPress plugin is easier than most developers think. Let’s create a simple structured approach.
1. Create Plugin Folder and File
Inside /wp-content/plugins/, create:
my-simple-plugin/
my-simple-plugin.php
2. Add Plugin Header
Every plugin needs metadata:
<?php
/**
* Plugin Name: My Simple Plugin
* Description: A basic WordPress plugin example.
* Version: 1.0
* Author: Your Name
*/
This tells WordPress how to recognize your plugin.
3. Add Basic Functionality
Let’s create a simple shortcode.
function msp_hello_world() {
return "<h2>Hello from My Simple Plugin!</h2>";
}
add_shortcode('hello_msp', 'msp_hello_world');
Now you can use:
[hello_msp]
in posts or pages.
4. Enqueue Styles Properly
Never add CSS directly in PHP output.
Instead:
function msp_assets() {
wp_enqueue_style(
'msp-style',
plugin_dir_url(__FILE__) . 'style.css'
);
}
add_action('wp_enqueue_scripts', 'msp_assets');
5. Add an Admin Menu (Optional)
You can extend functionality:
function msp_admin_menu() {
add_menu_page(
'Simple Plugin',
'Simple Plugin',
'manage_options',
'msp-settings',
'msp_settings_page'
);
}
add_action('admin_menu', 'msp_admin_menu');
6. Best Practices
- Keep code modular
- Avoid global variables
- Use hooks instead of modifying core
- Follow WordPress coding standards
Conclusion
A WordPress plugin is essentially a structured collection of hooks and functions. Once you understand the basics, you can build anything from simple tools to complex systems.