WP Manifestindependent plugin directory
manifest / developer / sample-plugin

Sample Plugin

Sample plugin with php unit test .

by YOUR NAME HERE · github.com/marufmks/sample-plugin · website

0stars
0forks

Install

No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:

wp plugin install https://github.com/marufmks/sample-plugin/archive/refs/heads/main.zip

A modern, enterprise-ready WordPress plugin with PSR-4 autoloading, namespace support, and WordPress best practices.

Description

Sample Plugin is a starter WordPress plugin that demonstrates modern plugin development architecture including:

  • PSR-4 Autoloading - Uses Composer for class autoloading
  • Namespace Support - Follows PHP namespace conventions (Vendor\SamplePlugin)
  • Singleton Pattern - Single instance management
  • Hook Loader - Centralized hook registration system
  • Separation of Concerns - Organized code structure
  • Full Test Suite - PHPUnit testing with WordPress test suite

Features

  • Modern OOP architecture with namespaced classes
  • Composer-based dependency management
  • PSR-4 autoloading
  • Singleton pattern for main plugin class
  • Centralized hook registration system
  • Clean separation between admin and public functionality
  • Full PHPUnit test suite support

Requirements

  • PHP 7.4 or higher
  • WordPress 6.0 or higher
  • Composer

Installation

Manual Installation

  1. Download the plugin and upload to /wp-content/plugins/sample-plugin/
  2. Install dependencies: composer install
  3. Activate the plugin through the WordPress Plugins menu

Via Composer

composer require vendor/sample-plugin

Usage

Using the Plugin Functions

// Greet someone
echo \Vendor\SamplePlugin\greet( 'World' );
// Output: Hello, World!

// Format a price
echo \Vendor\SamplePlugin\format_price( 99.99 );
// Output: $99.99

Using Filters

// Filter the greeting
add_filter( 'sample_plugin_greet', function( $name ) {
    return 'Welcome, ' . $name . '!';
});

Architecture

sample-plugin/
├── sample-plugin.php          # Entry point
├── composer.json              # Dependencies & PSR-4 config
├── vendor/                    # Composer dependencies
├── src/
│   ├── Plugin.php             # Main plugin class (singleton)
│   ├── Loader.php             # Hook registration system
│   ├── Functions.php          # Public API functions
│   ├── Traits/
│   │   └── Singleton.php      # Singleton trait
│   └── Core/                  # Feature classes
│       ├── Greet.php
│       └── Formatter.php
└── tests/                     # PHPUnit tests

Key Components

File Purpose
Plugin.php Main plugin class, handles initialization, activation/deactivation
Loader.php Centralized hook registration
Functions.php Public API functions for theme/plugin developers
Core/ Feature modules (add new features here)

Adding New Features

  1. Create a new class in src/Core/
  2. Register hooks in the class constructor
  3. Add the class instantiation in Plugin.php load_classes() method

See Plugin_Usage.md for detailed development guide.

Development

Install Dependencies

composer install

Regenerate Autoloader

After adding new classes, run:

composer dump-autoload

Generate Translation Files

composer makepot

Running Tests

This plugin uses PHPUnit for testing with the WordPress test suite.

Prerequisites

  1. MySQL database server running
  2. WordPress test environment installed

Install Test Environment

# Database: wordpress_test, User: root, Password: root, Host: 127.0.0.1
bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 latest true

Create the test database if it doesn't exist:

mysql -h 127.0.0.1 -u root -proot -e "CREATE DATABASE IF NOT EXISTS wordpress_test"

Run Tests

composer test

Or:

./vendor/bin/phpunit

Run Specific Tests

# Run tests by class name
./vendor/bin/phpunit --filter SampleTest

# Run a specific test
./vendor/bin/phpunit --filter test_greet_returns_hello_message

Security

  • Input sanitization and output escaping
  • Nonce verification for forms
  • Capability checks for user authorization
  • Prepared SQL queries using $wpdb->prepare()

License

GPL-2.0-or-later. See License URI for details.

Changelog

0.1.0

  • Initial release
  • Modern PSR-4 architecture with namespace support
  • Singleton pattern implementation
  • Centralized hook loader
  • Sample greeting function
  • Sample price formatting function
  • Full PHPUnit test suite

Credits

Created with opencode