AI Prompts for Scaffolding a Basic WordPress Plugin

This library provides a set of five sequential AI prompts designed to generate the complete scaffold for a basic WordPress plugin. The objective is to produce a well-structured, secure, and standards-compliant code base, guiding the AI to create each component from the initial file setup to final asset loading.

This collection provides a targeted prompt for each stage of development:

  • 1. Plugin Foundation & Structure This prompt generates the main plugin file, including all required headers, security constants, and a core plugin class. It establishes the hook-based architecture (register_activation_hook) and the standard directory structure.
  • 2. Custom Database Table This prompt is tailored to generate the activation logic. It produces a method that uses the global $wpdb object and the dbDelta() function to safely create or update a custom database table.
  • 3. Admin Options Page This prompt generates a dedicated class for creating a complete settings page. It utilizes the official WordPress Settings API (register_setting, add_settings_section, add_settings_field) to ensure a standard and secure admin interface.
  • 4. Frontend Shortcode This prompt generates a class for handling public-facing logic. It produces a secure shortcode handler that processes attributes and retrieves data using prepared statements via $wpdb->prepare().
  • 5. Script & Style Enqueueing This prompt generates the methods for asset management. It uses wp_enqueue_script and wp_enqueue_style to correctly load assets and includes logic for conditional loading on specific admin pages.

Core Architectural Features

All code generated by this prompt series is built on a common, robust architecture that includes:

  • Object-Oriented PHP: All functionality is encapsulated within classes for improved organization, scalability, and prevention of namespace conflicts.
  • Adherence to Standards: The generated code is designed to follow official WordPress Coding Standards and security best practices.
  • Hook-Based Integration: The plugin integrates with WordPress correctly using the standard system of actions and filters.
  • Sequential Workflow: Each prompt is context-aware and designed to build directly upon the code generated by the previous one, ensuring a coherent and logical development process.
Prompt

# =================================================================
# TOPIC: WORDPRESS PLUGIN FOUNDATION & FILE STRUCTURE
# =================================================================
# — ROLE AND GOAL —
# You are a senior WordPress developer specializing in modern plugin architecture and WordPress Coding Standards.
# Your primary goal is to generate a robust, secure, and scalable foundation for a new WordPress plugin. This includes the main plugin file with all necessary headers, essential security checks, and global constants for later use.
# — TASK —
# Generate the main plugin file (`[PLUGIN_SLUG].php`) and list the recommended directory structure based on the configuration variables and detailed logic provided below. The generated code must adhere strictly to WordPress best practices.
# —————————————————————–
# — CONFIGURATION VARIABLES —
# The generated code should pull all its configuration from these variables.
# =================================================================
# – PLUGIN META INFORMATION –
# The full name of the plugin.
PLUGIN_NAME=”[FILL_THIS_IN]” # Example: “Advanced Post Voting”
# The URL of the plugin’s homepage or marketplace listing.
PLUGIN_URI=”[FILL_THIS_IN]” # Example: “https://example.com/plugins/advanced-post-voting/”
# A short, descriptive summary of the plugin.
DESCRIPTION=”[FILL_THIS_IN]” # Example: “This plugin allows users to upvote or downvote posts and pages.”
# The current version of the plugin.
VERSION=”1.0.0″
# The name of the plugin author(s).
AUTHOR=”[FILL_THIS_IN]” # Example: “Your Name / Company Name”
# The URL of the author’s website.
AUTHOR_URI=”[FILL_THIS_IN]” # Example: “https://example.com”
# The plugin’s license (e.g., GPL-2.0-or-later).
LICENSE=”GPL-2.0-or-later”
# The full URI of the license text.
LICENSE_URI=”https://www.gnu.org/licenses/gpl-2.0.html”
# The plugin’s unique identifier for translation files (Text Domain). Usually the plugin slug.
TEXT_DOMAIN=”[FILL_THIS_IN]” # Example: “advanced-post-voting”
# The file name (slug) of the plugin. This MUST match the Text Domain.
PLUGIN_SLUG=”[FILL_THIS_IN]” # Example: “advanced-post-voting”
# —————————————————————–
# — DETAILED CODE LOGIC —
# The generated code must implement the following logic step-by-step.
# 1. Add the WordPress Plugin Header:
# – Use all the information from the `CONFIGURATION VARIABLES` section to generate the standard WordPress plugin header comment block.
# 2. Add Security Check:
# – Include the line `defined(‘ABSPATH’) or die(‘No script kiddies please!’);` to prevent direct file access.
# 3. Define Global Constants:
# – After the security check, define two important constants that will be used in subsequent steps:
# – A constant for the plugin’s main file path: `define(‘MY_AWESOME_PLUGIN_FILE’, __FILE__);`
# – A constant for the plugin’s version: `define(‘MY_AWESOME_PLUGIN_VERSION’, ‘[VERSION]’);` (use the version from the config).
# 4. Define Activation and Deactivation Hooks:
# – Use `register_activation_hook` and `register_deactivation_hook`, referencing the main plugin file constant. These will call methods within the main plugin class.
# 5. Create the Main Plugin Class:
# – To encapsulate all plugin functionality, create a main class (e.g., `Advanced_Post_Voting`).
# – The `register_activation_hook` should call a public static method within this class (e.g., `public static function activate()`).
# – The `__construct` method will register all necessary action and filter hooks for the plugin’s operation.
# 6. List the Recommended Directory Structure:
# – Provide a list of standard folders and explain their purpose: `/admin`, `/public`, `/includes`, `/assets`, `/languages`.
# — FINAL INSTRUCTION —
# Please generate the complete, self-contained, and well-commented PHP code for the main plugin file and the directory structure list based on all the requirements above. This code will serve as the foundation for all subsequent prompts.

Recommended Tool: GitHub Copilot Chat, ChatGPT Plus, Claude.ai, Google AI Studio, Amazon CodeWhisperer, Cursor IDE, etc.
Recommended Model: GPT-4o, GPT-4, Claude 3 Opus, Claude 3 Sonnet, Gemini 1.5 Pro, Llama 3 70B, etc.
Prompt

# =================================================================
# TOPIC: CUSTOM WORDPRESS DATABASE TABLE CREATION
# =================================================================
# — ROLE AND GOAL —
# You are a senior WordPress and MySQL developer. You will now extend the plugin foundation created in the previous step.
# Your goal is to write a static method for the main plugin class that creates a custom database table. This method will be triggered by the `register_activation_hook` already defined in the foundation code.
# — TASK —
# Generate a complete, static PHP method named `activate` to be placed inside the main plugin class from Prompt 1. This method will create a custom database table using the configuration below. It must use WordPress’s `dbDelta()` function for safe table creation.
# —————————————————————–
# — CONTEXT FROM PREVIOUS STEP —
# You are adding this `activate` method to the main plugin class that was generated in Prompt 1.
# The `register_activation_hook` in the base file already calls `[YourMainClass]::activate();`.
# —————————————————————–
# — CONFIGURATION VARIABLES —
# =================================================================
# – TABLE CONFIGURATION –
# The name of the table without the WordPress database prefix (`wp_`).
TABLE_NAME=”[FILL_THIS_IN]” # Example: “post_votes”
# – TABLE COLUMNS –
# An array defining the columns and their properties for the table.
TABLE_COLUMNS=”[
‘id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT’,
‘post_id BIGINT(20) UNSIGNED NOT NULL’,
‘user_id BIGINT(20) UNSIGNED NOT NULL’,
‘vote_type VARCHAR(10) NOT NULL’,
‘ip_address VARCHAR(100) NOT NULL’,
‘voted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP’,
‘PRIMARY KEY (id)’,
‘KEY idx_post_id (post_id)’,
‘UNIQUE KEY unq_user_post (user_id, post_id)’
]”
# —————————————————————–
# — DETAILED CODE LOGIC —
# The generated method must implement the following logic step-by-step.
# 1. Use the Global `$wpdb` Object:
# – Access the WordPress database object by declaring `global $wpdb;` at the beginning of the method.
# 2. Construct the Full Table Name:
# – Create the complete table name using the WordPress prefix: `$table_name = $wpdb->prefix . TABLE_NAME;`.
# 3. Get the Character Set:
# – Retrieve the database’s character set and collation information using `$charset_collate = $wpdb->get_charset_collate();`.
# 4. Form the SQL Query:
# – Build the `CREATE TABLE` SQL statement using the configured `TABLE_NAME` and `TABLE_COLUMNS`.
# 5. Use the `dbDelta()` Function:
# – Before calling `dbDelta()`, ensure it is available by including its file: `require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);`.
# – Execute the table creation by passing the SQL query to `dbDelta()`.
# — FINAL INSTRUCTION —
# Please generate only the complete, secure, and well-commented `public static function activate()` method, ready to be inserted into the main class from the previous prompt.

Recommended Tool: GitHub Copilot Chat, ChatGPT Plus, Claude.ai, Google AI Studio, Amazon CodeWhisperer, Cursor IDE, etc.
Recommended Model: GPT-4o, GPT-4, Claude 3 Opus, Claude 3 Sonnet, Gemini 1.5 Pro, Llama 3 70B, etc.
Plugin Admin Options Page (Settings API) Login Required
Front-End Shortcode Creation Login Required
Enqueueing Scripts And Styles Login Required