Building Your First WordPress Plugin
Why Build a Plugin Instead of Editing Theme Files?
Code placed in a plugin survives theme changes and updates. Anything that adds functionality (rather than pure styling) belongs in a plugin, not functions.php.
The Minimum Required File
Create a folder in wp-content/plugins/, e.g. my-first-plugin/my-first-plugin.php, with a header comment WordPress reads to register it:
<?php /** * Plugin Name: My First Plugin * Description: Adds a simple shortcode. * Version: 1.0 */ if ( ! defined( 'ABSPATH' ) ) exit; That’s it — it will now appear in Plugins > Installed Plugins, ready to activate.
Adding a Shortcode
function mfp_hello_shortcode() { return '<p>Hello from my first plugin!</p>'; } add_shortcode( 'mfp_hello', 'mfp_hello_shortcode' ); Now editors can type [mfp_hello] in any post or page.
Hooking Into WordPress Events
Most plugin functionality is built around actions (do something) and filters (modify something):
// Action: run code when something happens add_action( 'wp_footer', function() { echo '<!-- My plugin was here -->'; }); // Filter: modify a value before it's used add_filter( 'the_title', function( $title ) { return $title . ' 🚀'; }); Adding an Admin Settings Page
add_action( 'admin_menu', function() { add_options_page( 'My Plugin Settings', 'My Plugin', 'manage_options', 'my-plugin', 'mfp_settings_page' ); }); function mfp_settings_page() { echo '<div class="wrap"><h1>My Plugin Settings</h1></div>'; } Security Basics
Always check ABSPATH is defined (prevents direct file access), escape output with functions like esc_html(), and sanitize any user input with sanitize_text_field() before saving it.
Practice Exercise
Build a plugin that adds a shortcode [latest_tutorials] displaying the titles of your 5 most recent posts as a list.

Leave a Reply