No history yet

WordPress Plugin Basics

The Anatomy of a Plugin

At its core, a WordPress plugin is a package of code that extends the functionality of your site. The simplest plugin is just a single PHP file. This file contains a special block of comments at the top, called the plugin header. WordPress reads this header to understand what your plugin is, who made it, and what it does.

Without the header, WordPress won't recognize your file as a plugin, even if it's in the right folder.

This header is non-negotiable. It must be at the very top of your main plugin file. Here's what a basic one looks like:

<?php
/**
 * Plugin Name:       My First Plugin
 * Plugin URI:        https://example.com/plugins/the-basics/
 * Description:       A simple plugin to demonstrate the basics.
 * Version:           1.0
 * Author:            Your Name
 * Author URI:        https://example.com/
 */

While you can put everything in one file, it's best practice to give your plugin its own folder inside the wp-content/plugins/ directory. For example, you might create a folder called my-first-plugin and place your main PHP file, also named my-first-plugin.php, inside it. This keeps your code organized, especially as you add more files like CSS or JavaScript.

Hooks and Filters

WordPress is built to be extended without modifying its core files. The way it achieves this is through a system called hooks. Think of hooks as specific points in the WordPress code where developers can plug in their own custom functions.

Hooks are the means by which you hook into WordPress and add your own code without modifying core files.

There are two kinds of hooks: actions and filters.

Actions let you do something at a specific moment. When WordPress reaches an action hook, it pauses and runs any functions attached to it. For example, you could use an action to add a new menu item to the admin dashboard when it's being built.

Filters let you change something. A filter hook passes data to your function, you modify it, and then you must return it. For example, you could use a filter to change the title of a blog post right before it's displayed on the screen.

You use the add_action() function to attach your code to an action, and add_filter() to attach it to a filter. Both functions generally take at least two arguments: the name of the hook you want to connect to, and the name of your PHP function that should run.

Your First Plugin

Let's build a simple plugin that adds a short message at the end of every blog post. This is a perfect job for a filter.

  1. In your WordPress installation, navigate to the wp-content/plugins/ directory.
  2. Create a new folder called post-ender.
  3. Inside that folder, create a new file named post-ender.php.
  4. Open post-ender.php and paste in the following code:
<?php
/**
 * Plugin Name: Post Ender
 * Description: Appends a message to the end of every post.
 * Version: 1.0
 * Author: Your Name
 */

// This is our custom function
function add_message_to_content( $content ) {
    // Only add the message to single blog posts
    if ( is_single() ) {
        $message = '<p><em>Thanks for reading!</em></p>';
        return $content . $message; // Append our message
    }
    
    // For everything else, return the content unchanged
    return $content;
}

// Now we hook our function to the 'the_content' filter
add_filter( 'the_content', 'add_message_to_content' );

Let's break that down. We defined a function add_message_to_content that accepts one argument, $content. Inside, we check if the current page is a single post. If it is, we append our message to the content. Finally, we must return the $content, whether we changed it or not.

The last line, add_filter(...), is where the magic happens. It tells WordPress: "When you are about to display post content, run my add_message_to_content function on it first."

To see it work, save the file. Then, log in to your WordPress admin dashboard, go to "Plugins," find "Post Ender" in the list, and click "Activate." Now, when you view any single blog post on your site, you'll see "Thanks for reading!" at the bottom.

Quiz Questions 1/5

What is the most fundamental component required in the main PHP file for WordPress to recognize it as a plugin?

Quiz Questions 2/5

If you want to modify a blog post's title right before it's displayed, which type of hook should you use?

Understanding these fundamentals is the key to unlocking WordPress development. You now know how to create a basic plugin and how to use hooks to modify WordPress behavior.