wpnav_gutenberg_insert_pattern
Insert Gutenberg block patterns into WordPress posts at specific locations using pattern slugs and post IDs.
Instructions
Insert a Gutenberg block pattern at specified path. Use wpnav_gutenberg_list_patterns to discover available patterns first.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| post_id | Yes | Post ID to modify | |
| path | Yes | Path where pattern should be inserted (e.g., [0] for first position) | |
| pattern_slug | Yes | Pattern slug/ID from wpnav_gutenberg_list_patterns |
Implementation Reference
- src/tools/gutenberg/index.ts:570-619 (handler)The handler function executes the tool: validates post_id, path (using validatePath helper), and pattern_slug; makes a POST request to WP REST API /wpnav/v1/gutenberg/patterns/insert; returns formatted success or error content.handler: async (args, context) => { validateRequired(args, ['post_id', 'path', 'pattern_slug']); const id = validateId(args.post_id, 'Post'); if (!validatePath(args.path)) { return { content: [{ type: 'text', text: context.clampText(JSON.stringify({ error: 'invalid_path', message: 'Path must be non-empty array of non-negative integers', }, null, 2)), }], isError: true, }; } // Call REST API const result = await context.wpRequest('/wpnav/v1/gutenberg/patterns/insert', { method: 'POST', body: JSON.stringify({ post_id: id, path: args.path, pattern_slug: args.pattern_slug, }), }); if (!result.success) { return { content: [{ type: 'text', text: context.clampText(JSON.stringify({ error: result.error }, null, 2)), }], isError: true, }; } return { content: [{ type: 'text', text: context.clampText(JSON.stringify({ success: true, post_id: id, path: args.path, pattern_slug: args.pattern_slug, message: 'Pattern inserted successfully', }, null, 2)), }], }; },
- src/tools/gutenberg/index.ts:550-568 (schema)Input schema defining parameters: post_id (number, required), path (array of numbers, required), pattern_slug (string, required).inputSchema: { type: 'object', properties: { post_id: { type: 'number', description: 'Post ID to modify', }, path: { type: 'array', items: { type: 'number' }, description: 'Path where pattern should be inserted (e.g., [0] for first position)', }, pattern_slug: { type: 'string', description: 'Pattern slug/ID from wpnav_gutenberg_list_patterns', }, }, required: ['post_id', 'path', 'pattern_slug'], },
- src/tools/gutenberg/index.ts:546-621 (registration)Registration of the wpnav_gutenberg_insert_pattern tool in toolRegistry, including definition (name, description, schema), inline handler, and category.toolRegistry.register({ definition: { name: 'wpnav_gutenberg_insert_pattern', description: 'Insert a Gutenberg block pattern at specified path. Use wpnav_gutenberg_list_patterns to discover available patterns first.', inputSchema: { type: 'object', properties: { post_id: { type: 'number', description: 'Post ID to modify', }, path: { type: 'array', items: { type: 'number' }, description: 'Path where pattern should be inserted (e.g., [0] for first position)', }, pattern_slug: { type: 'string', description: 'Pattern slug/ID from wpnav_gutenberg_list_patterns', }, }, required: ['post_id', 'path', 'pattern_slug'], }, }, handler: async (args, context) => { validateRequired(args, ['post_id', 'path', 'pattern_slug']); const id = validateId(args.post_id, 'Post'); if (!validatePath(args.path)) { return { content: [{ type: 'text', text: context.clampText(JSON.stringify({ error: 'invalid_path', message: 'Path must be non-empty array of non-negative integers', }, null, 2)), }], isError: true, }; } // Call REST API const result = await context.wpRequest('/wpnav/v1/gutenberg/patterns/insert', { method: 'POST', body: JSON.stringify({ post_id: id, path: args.path, pattern_slug: args.pattern_slug, }), }); if (!result.success) { return { content: [{ type: 'text', text: context.clampText(JSON.stringify({ error: result.error }, null, 2)), }], isError: true, }; } return { content: [{ type: 'text', text: context.clampText(JSON.stringify({ success: true, post_id: id, path: args.path, pattern_slug: args.pattern_slug, message: 'Pattern inserted successfully', }, null, 2)), }], }; }, category: ToolCategory.CONTENT, });
- src/tools/gutenberg/helpers.ts:44-54 (helper)validatePath helper function used in the handler to validate the path parameter is a non-empty array of non-negative integers.export function validatePath(path: number[]): boolean { if (!Array.isArray(path)) { return false; } if (path.length === 0) { return false; } return path.every(n => Number.isInteger(n) && n >= 0); }