// article

Build a WordPress Plugin from Scratch

Step-by-step plugin architecture, hooks, shortcodes, settings pages and secure deployment for production-ready WordPress extensions.

What is a plugin?

A WordPress plugin is a package of PHP, assets and configuration that adds or modifies functionality without changing the core platform. It is the recommended way to extend WordPress cleanly and keep updates safe.

Prerequisites

To build a plugin, you need:

  • A working local WordPress install
  • Basic PHP and WordPress API knowledge
  • Editor with syntax highlighting

Plugin boilerplate

Create a new folder inside wp-content/plugins and add a PHP file with plugin metadata.

<?php
/*
Plugin Name: Hello Custom Plugin
Description: My first plugin.
Version: 1.0
Author: Ashutosh Rajbhar
*/

Hooks

Hooks let your plugin connect to WordPress lifecycle events. Use add_action() and add_filter() to run code where it matters.

function ashutosh_plugin_notice() {
  echo '<!-- Plugin active -->';
}
add_action('wp_footer','ashutosh_plugin_notice');

Shortcodes

Shortcodes allow content editors to embed plugin output directly within pages and posts.

function welcome_shortcode() {
  return '<h2>Welcome to the site!</h2>';
}
add_shortcode('welcome', 'welcome_shortcode');

Settings API

For configurable plugins, use the Settings API to store options and create an admin settings page.

function my_plugin_menu() {
  add_options_page('Plugin Settings', 'Custom Plugin', 'manage_options', 'custom-plugin', 'my_plugin_settings_page');
}
add_action('admin_menu', 'my_plugin_menu');

Security

Follow these security best practices:

  • Validate and sanitize all input
  • Escape output with esc_html() or equivalent
  • Check capabilities with current_user_can()
  • Protect forms using nonces
  • Prevent direct access by checking ABSPATH

Recommended file structure

my-plugin/
  my-plugin.php
  readme.txt
  assets/
  includes/
  languages/

Testing

Test your plugin on activation, deactivation, URL generation, and UI flows. Verify form submission, shortcode rendering, and settings persistence.

Deployment

Package your plugin as a ZIP file, include proper readme documentation, and publish to the WordPress plugin directory or deliver the ZIP to the client.