WP Code Generator — Examples

Concrete runs with the WP Code Generator: a plugin as ZIP, a typed hook, a block theme, an ability, Block Bindings, a REST route, and a 7.x snippet.

Back to the overview: WP Code Generator · Open the tool live: www.jpkc.com/tools/wp/

The manual describes every generator and its fields. This page adds concrete workflows: typical tasks played through step by step, with the code the tool actually produces along the way. All excerpts come from real runs in the tool; long files are shortened. The examples loosely build on each other — a small "Book Shelf" plugin for a reading list — but each one also works on its own.

Example 1: A plugin with readme.txt as a ZIP

You're starting a new plugin and want to be able to install it right away.

  1. Open the WP Code Generator — the Plugin generator is active on load.
  2. Type Book Shelf into Plugin Name. Plugin slug becomes book-shelf, the Text Domain follows the slug, and PHP namespace becomes BookShelf.
  3. Enter Author (Jane Doe), Author URI, and a one-line Description. Requires WP (7.0), Requires PHP (8.3), and License (GPL-2.0-or-later) fit as they are.
  4. In the readme.txt block, put your wordpress.org username under Contributors and something like books, library, reading, wordpress under Tags. A warning appears above the output right away: "readme.txt: wordpress.org ignores the tag(s) wordpress — removed." The tool sticks to the directory's rules.
  5. The output has three tabs: book-shelf.php, readme.txt, and uninstall.php. The main file starts like this:
book-shelf.php (excerpt)
<?php
/**
 * Plugin Name:       Book Shelf
 * Description:       Keeps a list of books you have read.
 * Version:           1.0.0
 * Requires at least: 7.0
 * Requires PHP:      8.3
 * Author:            Jane Doe
 * Author URI:        https://example.com
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       book-shelf
 * Domain Path:       /languages
 *
 * @package BookShelf
 */

declare(strict_types=1);

namespace BookShelf;

defined( 'ABSPATH' ) || exit;

/**
 * Main plugin class.
 */
final class Plugin {

    public const string VERSION = '1.0.0';

Tested up to and Stable tag are deliberately missing here — they live in readme.txt, where wordpress.org reads them. When the plugin is deleted, uninstall.php removes the book_shelf_version option that activation created, on multisite for every site.

  1. Click ZIP. You get book-shelf.zip with the folder book-shelf/ and the three files inside — ready for the plugin upload in the WordPress admin.

Example 2: A hook with real parameter types

Your plugin should do something when a post is saved.

  1. Under Hooks & Assets, pick the Hook (Action / Filter) generator.
  2. Type save_post into Hook Name. Because the tool knows this hook, it sets Type to Action and Accepted Args to 3.
  3. Enter a prefixed name of your own under Callback Function Name, such as book_shelf_on_save. If you leave the field empty, the tool derives a name from the hook — but your own prefix protects better against collisions.
  4. With Typed parameters (known hooks) active, the signature carries the real types from the WordPress documentation:
hooks.php
<?php
declare(strict_types=1);

defined( 'ABSPATH' ) || exit;

/**
 * Callback for the 'save_post' action.
 *
 * @param int     $post_id
 * @param WP_Post $post
 * @param bool    $update
 */
function book_shelf_on_save( int $post_id, WP_Post $post, bool $update ): void {

    // Your code here.

}
add_action( 'save_post', 'book_shelf_on_save', 10, 3 );
  1. Switch to a filter such as the_title, and the function returns the filtered value, with a return type that matches the first parameter. Remember: a filter function must return the value, otherwise it's gone.

Example 3: A block theme in one go

A matching block theme should go with the plugin.

  1. Pick Theme under Plugin & Theme. If you entered an author in example 1, it's already in the Author field here — the tool copies shared fields straight into untouched fields of the other generators.
  2. Type Book Shelf Theme into Theme Name and turn on Block Theme (Full Site Editing). The block theme options expand; the defaults are a usable start: a four-color palette, 800 px content width, fluid typography, seven templates, header and footer as template parts, a dark style variation, a hero pattern, and an editor stylesheet.
  3. The output now has 15 tabs. style.css gets the header including the suggested tags:
style.css
/*
Theme Name:        Book Shelf Theme
Author:            Jane Doe
Author URI:        https://example.com
Description:       A custom WordPress theme.
Requires at least: 7.0
Tested up to:      7.1
Requires PHP:      8.3
Version:           1.0.0
License:           GNU General Public License v2 or later
License URI:       https://www.gnu.org/licenses/gpl-2.0.html
Text Domain:       book-shelf-theme
Tags:              full-site-editing, block-patterns, style-variations, wide-blocks, editor-style
*/

/* Theme styles go here. */

theme.json starts with "$schema": "https://schemas.wp.org/wp/7.1/theme.json" and "version": 3; styles/dark.json inverts the palette (Base #1e293b, Contrast #ffffff). functions.php needs very little: block themes enable the title tag, featured images, and so on automatically, which leaves the editor stylesheet, loading the stylesheet, and the pattern category.

  1. ZIP gives you book-shelf-theme.zip with all 15 files in the folder book-shelf-theme/ — from templates/index.html to patterns/hero.php. Upload, activate, and carry on in the Site Editor.

Example 4: An ability for AI assistants

An AI assistant or MCP client should be able to fetch the latest posts of your site.

  1. Under APIs, pick the Abilities API generator.
  2. Set Namespace to book-shelf. Function Prefix and Text Domain derive from it. Everything else stays at the defaults: ability get-recent-posts, input count (1 to 20, default 5) and post_type (post or page), output a list with id, title, and link, permission read, annotation readonly.
  3. The tool shows GET as the method — an ability that only reads runs through GET. The generated code registers the category and the ability on the hooks meant for them:
abilities.php (excerpt)
function book_shelf_register_abilities(): void {

    wp_register_ability(
        'book-shelf/get-recent-posts',
        array(
            'label'               => __( 'Get Recent Posts', 'book-shelf' ),
            'description'         => __( 'Returns the most recent published posts (ID, title, permalink). Use it to get an overview of recent content.', 'book-shelf' ),
            'category'            => 'book-shelf-content',
            // input_schema and output_schema shortened
            'execute_callback'    => 'book_shelf_execute_get_recent_posts',
            'permission_callback' => 'book_shelf_can_get_recent_posts',
            'meta'                => array(
                'annotations'  => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ),
                'public'       => true,
                'show_in_rest' => true,
                'mcp'          => array( 'type' => 'tool' ),
            ),
        )
    );

}
add_action( 'wp_abilities_api_init', 'book_shelf_register_abilities' );
  1. With these defaults, the execute callback isn't empty but works: it reads the input, queries only published posts with get_posts(), and returns id, title, and link. The tool casts every input value itself, because the schema validates values but doesn't convert them.
  2. The Usage (PHP + curl) extra adds a comment block showing how to call the ability in PHP and through the REST API.

Example 5: Show post meta in a block with Block Bindings

For every book, ISBN, subtitle, and purchase link should be stored as meta fields and shown directly in blocks — without a custom block.

  1. Under Blocks & Editor, pick the Block Bindings generator. The defaults almost fit already: plugin my-bindings, source my-bindings/book-data, and the fields isbn, subtitle, and buy_url with matching sanitizers.
  2. For your own post types, change Post Types; they need show_in_rest and must support custom-fields, otherwise the editor can neither read nor save the values.
  3. The output has four tabs: the plugin my-bindings.php, assets/editor.js, and two HTML files with example markup. block-markup.html shows how a paragraph is bound to a field:
block-markup.html (excerpt)
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"my-bindings/book-data","args":{"field":"isbn"}}}}} -->
<p>Fallback ISBN</p>
<!-- /wp:paragraph -->
  1. In the PHP part, the source only reads the value after passing the same visibility checks as the core source core/post-meta — a draft, private, or password-protected post doesn't give away its meta values to visitors who may not read it. If it returns null, the fallback text stays in the block.
  2. ZIP gives you my-bindings.zip with all four files. After activation, you pick the "Book data" source and the field for a paragraph under Attributes in the editor.

Example 6: A REST route that really saves

An external application should be able to create, read, update, and delete books.

  1. Under APIs, pick the REST Route generator. Namespace my-plugin/v1, route /books, and the ID segment are preset.
  2. In addition to GET, turn on the methods POST, POST, PUT, PATCH (EDITABLE), and DELETE, and switch on Real CRUD.
  3. The tool warns right away: "Real CRUD: wp_insert_post() refuses a post whose title, content and excerpt are all empty (400 empty_content) — add a title, content or excerpt argument for POST." So you use Add Argument to add an argument title of type string with the description "Title of the book.", limited to POST and EDIT — the warning goes away.
  4. The generated code registers two routes and checks exactly the right permission for each method:
rest-route.php (excerpt, indentation reduced)
[
    'methods'             => WP_REST_Server::CREATABLE,
    'callback'            => 'my_plugin_rest_create_book',
    'permission_callback' => static fn (): bool => current_user_can( get_post_type_object( 'post' )?->cap->create_posts ?? 'do_not_allow' ),
    'args'                => [
        'title' => [ 'type' => 'string', 'description' => __( 'Title of the book.', 'my-plugin' ) ],
    ],
],

New books are created as drafts, and a response with 201 and a Location header reports the new address. DELETE moves to the trash and refuses when the trash is turned off, instead of deleting permanently. GET only returns the raw title in the edit context and only to users who may edit the post.

  1. The docblock at the top of the file lists all endpoints with their functions and names the line that loads the file from your plugin: require_once __DIR__ . '/rest-route.php';.

Example 7: Turn off AI features with a snippet

On a client site, the AI features of WordPress 7.0 should stay off for now.

  1. Under Snippets, pick Code Snippets, and in the select field, in the WordPress 7.x & Editor group, pick Disable AI Features.
  2. Output format is set to Must-use plugin file. The editor shows a complete file:
disable-ai.php
<?php
/**
 * Plugin Name: Disable AI features (snippet)
 * Description: Turns off the AI features and the built-in AI provider connectors (WordPress 7.0+).
 * Requires at least: 7.0
 * Requires PHP: 8.3
 */

declare(strict_types=1);

defined( 'ABSPATH' ) || exit;

/**
 * Disable AI features (WordPress 7.0+): wp_supports_ai() returns false and the
 * built-in AI provider connectors are not registered.
 * Site-wide alternative in wp-config.php: define( 'WP_AI_SUPPORT', false );
 */
add_filter( 'wp_supports_ai', '__return_false' );
  1. Download it with Download as disable-ai.php and put it in wp-content/mu-plugins/. Must-use plugins load automatically, without you having to activate them.
  2. Prefer wp-config.php? The comment names the alternative, and the WP-Config generator writes it for you under WordPress 7.x as WP_AI_SUPPORT = false.
  3. The other snippets in the group work much the same, for example Speculative Loading (disable / configure): by default it turns off link preloading with a single filter; with Configure mode and eagerness you set mode and eagerness instead.

There's more depth in the manual for every generator and field, and in the tips & tricks for strategy and gotchas. You can try everything directly in the tool.