Complete Custom WordPress Plugin Boilerplate Guide

Creating a custom WordPress plugin starts with a solid boilerplate that includes plugin headers, security checks, and a structured class using the Singleton pattern. This guide walks you through each part—from loading text domains to hooking into actions—so you can build maintainable, translation-ready plugins. Follow along to understand the core components and best practices.

Write me the complete boilerplate for a custom wordpress plugin, and explain everything thoroughly

Okay, I can help you with that. Here’s the boilerplate code for a custom WordPress plugin, along with a detailed explanation of each part.

<?php
/**
 * Plugin Name: My Custom Plugin
 * Description: This is a basic plugin boilerplate.
 * Version: 1.0.0
 * Author: Your Name
 * Author URI: https://www.example.com
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Main plugin class.
 */
class My_Custom_Plugin {

    /**
     * Plugin instance.
     *
     * @see get_instance()
     * @type object
     */
    protected static $instance = null;

    /**
     * Return an instance of this class.
     *
     * @return object A single instance of this class.
     */
    public static function get_instance() {
        // If the single instance hasn't been set, set it now.
        if ( null === self::$instance ) {
            self::$instance = new self();
            self::$instance->do_hooks();
        }

        return self::$instance;
    }

    /**
     * Constructor.
     */
    private function __construct() {
        // Do nothing.
    }

    /**
     * Hook into actions and filters.
     */
    private function do_hooks() {
        add_action( 'init', array( $this, 'plugin_textdomain' ) );
    }

    /**
     * Load the plugin text domain for translation.
     */
    public function plugin_textdomain() {
        load_plugin_textdomain(
            'my-custom-plugin',
            false,
            dirname( plugin_basename( __FILE__ ) ) . '/languages/'
        );
    }

    /**
     * Example function: Display a message in the WordPress admin.
     */
    public function display_admin_message() {
        echo '<div class="notice notice-success is-dismissible"><p>Hello from My Custom Plugin!</p></div>';
    }
}

/**
 * Initialize the plugin.
 */
My_Custom_Plugin::get_instance();
?>

Explanation

  1. Plugin Header: This section provides essential information about your plugin. WordPress uses it to display the plugin on the Plugins admin page.
    • Plugin Name: The name of your plugin.
    • Description: A brief description of what the plugin does.
    • Version: The current version number.
    • Author: Your name or your organization’s name.
    • Author URI: A link to your or your organization’s website.
  2. Security Check: Checks whether ABSPATH is defined. If someone accesses the file directly (outside WordPress), the script exits to prevent security issues.
  3. Main Plugin Class: Groups all plugin code to avoid naming conflicts.
  4. Singleton Pattern: Ensures only one instance of the plugin class exists:
    • $instance stores the instance.
    • get_instance() returns or creates it.
  5. Constructor: Set to private so the class can only be instantiated through get_instance().
  6. Hook into Actions and Filters: do_hooks() registers WordPress hooks (e.g., loads the text domain during init).
  7. Load Text Domain for Translation: Allows the plugin to be translated into different languages.
  8. Example Function: Shows how to display an admin notice.
  9. Initialize the Plugin: Calls My_Custom_Plugin::get_instance() to kick things off.

If you need deeper troubleshooting on a live WordPress site, Atiba can help!

 

Need human WordPress help?

WP Assistant is a free tool created by Atiba Software, a WordPress design and development company located in Nashville, TN. If you need more personalized WordPress assistance let us know, and we’ll get back to you ASAP!