R
Rishtaara
WordPress Fundamentals
Lesson 30 of 32Article20 min

Create a Plugin from Scratch (Minimal PHP Plugin)

You do not need to be a professional developer to write a small custom plugin. A plugin is simply a PHP file with a special comment header, placed inside wp-content/plugins/, that WordPress detects and lets you activate.

Why build your own plugin?

You do not need to be a professional developer to write a small custom plugin. A plugin is simply a PHP file with a special comment header, placed inside wp-content/plugins/, that WordPress detects and lets you activate.

Writing your own tiny plugin — instead of copy-pasting snippets into your theme's functions.php — is the safer, more portable way to add custom code, since it survives even if you switch themes.

Real-life example: Putting custom code in a plugin instead of your theme is like keeping your personal documents in your own folder instead of a shared office drawer — moving offices (changing themes) will not lose your files.

Step 1: the plugin file and header

Create a new folder inside wp-content/plugins/, for example rishtaara-hello, and inside it a PHP file with the same name, rishtaara-hello.php. The comment block at the top is what WordPress reads to register the plugin.

Tip: The `if ( ! defined( 'ABSPATH' ) ) { exit; }` guard prevents someone from running your PHP file directly by visiting its URL — a small but standard security habit.
wp-content/plugins/rishtaara-hello/rishtaara-hello.php
<?php
/**
 * Plugin Name: Rishtaara Hello Widget
 * Description: A tiny example plugin that adds a friendly message to every post.
 * Version: 1.0
 * Author: Your Name
 */

// Always guard against direct file access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

Step 2: hook into WordPress with actions and filters

WordPress runs on hooks — actions (do something at a certain point) and filters (modify a piece of data before it is used). Our example plugin will use the_content filter to append a message to the end of every post.

  • add_action() — run custom code when something happens (e.g. wp_head, save_post)
  • add_filter() — modify data as it passes through WordPress (e.g. the_content, the_title)
  • is_single() — checks we are on a single post page, not an archive or homepage
Adding a filter that appends text to post content
<?php
function rishtaara_append_message( $content ) {
    if ( is_single() && in_the_loop() && is_main_query() ) {
        $content .= '<p><em>Thanks for reading — more at Rishtaara!</em></p>';
    }
    return $content;
}
add_filter( 'the_content', 'rishtaara_append_message' );

Step 3: activate, test, and add a settings page (optional)

Go to Plugins → Installed Plugins in wp-admin, find “Rishtaara Hello Widget,” and click Activate. Visit any single post to see your custom message appear at the end.

For a real plugin, you would typically also register a settings page (using add_options_page) so users can customize the message text without editing PHP — a natural next step once you are comfortable with hooks.

Tip: Always test new plugin code on a local or staging site first. A small PHP typo can trigger a fatal error and take your whole site offline until fixed.
Registering a simple settings page (starter pattern)
<?php
add_action( 'admin_menu', function () {
    add_options_page(
        'Rishtaara Hello Settings',
        'Rishtaara Hello',
        'manage_options',
        'rishtaara-hello',
        function () {
            echo '<h1>Rishtaara Hello Settings</h1><p>Settings form goes here.</p>';
        }
    );
});