create-comment
Add comments to WordPress posts by specifying site URL, credentials, post ID, and content. Use this tool to automate comment creation with REST APIs for efficient site management.
Instructions
Create a new comment on a WordPress post
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| author_email | No | Comment author email (for users who can't set this) | |
| author_name | No | Comment author name (for users who can't set this) | |
| content | Yes | Comment content | |
| password | Yes | WordPress application password | |
| postId | Yes | ID of the post to comment on | |
| siteUrl | Yes | WordPress site URL | |
| username | Yes | WordPress username |
Implementation Reference
- src/index.ts:1077-1113 (handler)The main handler function that executes the tool logic: constructs comment data and makes a POST request to the WordPress /comments endpoint using the makeWPRequest helper.async ({ siteUrl, username, password, postId, content, author_name, author_email }) => { try { const commentData: Record<string, any> = { post: postId, content }; if (author_name) commentData.author_name = author_name; if (author_email) commentData.author_email = author_email; const comment = await makeWPRequest<WPComment>({ siteUrl, endpoint: "comments", method: "POST", auth: { username, password }, data: commentData }); return { content: [ { type: "text", text: `Successfully created comment with ID: ${comment.id} on post #${postId}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error creating comment: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }
- src/index.ts:1068-1076 (schema)Zod input schema defining parameters for the create-comment tool: siteUrl, username, password, postId, content, optional author_name and author_email.{ siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), postId: z.number().describe("ID of the post to comment on"), content: z.string().describe("Comment content"), author_name: z.string().optional().describe("Comment author name (for users who can't set this)"), author_email: z.string().email().optional().describe("Comment author email (for users who can't set this)"), },
- src/index.ts:1065-1114 (registration)Registration of the 'create-comment' tool using server.tool(), including name, description, input schema, and handler reference.server.tool( "create-comment", "Create a new comment on a WordPress post", { siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), postId: z.number().describe("ID of the post to comment on"), content: z.string().describe("Comment content"), author_name: z.string().optional().describe("Comment author name (for users who can't set this)"), author_email: z.string().email().optional().describe("Comment author email (for users who can't set this)"), }, async ({ siteUrl, username, password, postId, content, author_name, author_email }) => { try { const commentData: Record<string, any> = { post: postId, content }; if (author_name) commentData.author_name = author_name; if (author_email) commentData.author_email = author_email; const comment = await makeWPRequest<WPComment>({ siteUrl, endpoint: "comments", method: "POST", auth: { username, password }, data: commentData }); return { content: [ { type: "text", text: `Successfully created comment with ID: ${comment.id} on post #${postId}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error creating comment: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/index.ts:66-74 (schema)TypeScript interface defining the structure of a WordPress comment object, used in the makeWPRequest response typing.interface WPComment { id: number; author_name?: string; content?: { rendered: string; }; post?: number; date?: string; }
- src/index.ts:158-194 (helper)Shared utility function for making authenticated HTTP requests to the WordPress REST API using axios, used by the create-comment handler.async function makeWPRequest<T>({ siteUrl, endpoint, method = 'GET', auth, data = null, params = null }: { siteUrl: string; endpoint: string; method?: 'GET' | 'POST' | 'PUT' | 'DELETE'; auth: { username: string; password: string }; data?: any; params?: any; }): Promise<T> { const authString = Buffer.from(`${auth.username}:${auth.password}`).toString('base64'); try { const response = await axios({ method, url: `${siteUrl}/wp-json/wp/v2/${endpoint}`, headers: { 'Authorization': `Basic ${authString}`, 'Content-Type': 'application/json', }, data: data, params: params }); return response.data as T; } catch (error) { if (axios.isAxiosError(error) && error.response) { throw new Error(`WordPress API error: ${error.response.data?.message || error.message}`); } throw error; } }