Wednesday, November 12, 2025

#1&2 Modules : Hooks & Filters

WordPress Plugin Tutorial - Modules 1 & 2

🎓 WordPress Plugin Development Tutorial

Learn step-by-step from basics to advanced. Each module includes copyable examples using Prism.js so you can practice easily.

“Hooks are WordPress’s event system — they let your code run at specific times, or modify existing content.”

There are two main types:
  1. Action Hooks → Let you run code at a specific event (e.g., wp_footer)
  2. Filter Hooks → Let you modify data before it’s displayed (e.g., the_content)


🧩 Module 1: Your First Plugin

This simple plugin adds a “Hello, World!” message to your site footer.

  1. Go to wp-content/plugins/
  2. Create a folder named my-first-plugin
  3. Create a file inside it called my-first-plugin.php
  4. Paste the following code:

<?php
/*
Plugin Name: My First Plugin
Plugin URI:  https://example.com
Description: A simple starter plugin.
Version:     1.0
Author:      Your Name
License:     GPL2
*/

function mfp_hello_world() {
    echo "<p style='color:green; text-align:center;'>Hello, World! My first plugin works 🎉</p>";
}
add_action('wp_footer', 'mfp_hello_world');
    

Activate it in your dashboard under Plugins → My First Plugin. Visit your homepage and you’ll see your message appear.


🧩 Module 2: Understanding Hooks & Filters

WordPress runs thousands of small events (hooks). You can connect your function to them using add_action() or add_filter().

✨ Example: Adding a Footer Message (Action Hook)


function my_footer_message() {
    echo "<p style='text-align:center; color:#00d4ff;'>Thank you for visiting my site!</p>";
}
add_action('wp_footer', 'my_footer_message');
    

🧠 Example: Modifying Post Content (Filter Hook)


function add_custom_signature($content) {
    if (is_single()) {
        $content .= '<p><em>– Thanks for reading!</em></p>';
    }
    return $content;
}
add_filter('the_content', 'add_custom_signature');
    

✅ The first example runs at the end of every page. ✅ The second modifies the post content *before* it’s displayed. Together, these are the foundation of every WordPress plugin!


© 2025 WordPress Plugin Learning Demo | Built with Prism.js | Modules 1–2

No comments:

Post a Comment

Welcome to RMC online Tutorials

WordPress Plugin Development – Full Course Index WordPress Plugin Development 12-Module Learning Portal Mod...