create-category
Efficiently add a new category to your WordPress site by specifying name, description, slug, and optional parent ID. Authenticates securely to streamline content organization.
Instructions
Create a new WordPress category
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| description | No | HTML description of the term | |
| meta | No | Meta fields | |
| name | Yes | HTML title for the term | |
| parent | No | The parent term ID | |
| password | Yes | WordPress application password | |
| siteUrl | Yes | WordPress site URL | |
| slug | No | An alphanumeric identifier for the term unique to its type | |
| username | Yes | WordPress username |
Implementation Reference
- src/index.ts:1937-1981 (handler)Handler function that constructs category data and sends POST request to WordPress /wp-json/wp/v2/categories endpoint via makeWPRequest to create new category, returns success/error message.async ({ siteUrl, username, password, name, description, slug, parent, meta, }) => { try { const categoryData: Record<string, any> = { name }; if (description) categoryData.description = description; if (slug) categoryData.slug = slug; if (parent) categoryData.parent = parent; if (meta) categoryData.meta = meta; const category = await makeWPRequest<WPCategory>({ siteUrl, endpoint: "categories", method: "POST", auth: { username, password }, data: categoryData }); return { content: [ { type: "text", text: `Successfully created category:\nID: ${category.id}\nName: ${name}\nSlug: ${category.slug || "No slug"}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error creating category: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }
- src/index.ts:1927-1936 (schema)Zod input schema defining parameters for create-category tool: required siteUrl, username, password, name; optional description, slug, parent, meta.{ siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), name: z.string().describe("HTML title for the term"), description: z.string().optional().describe("HTML description of the term"), slug: z.string().optional().describe("An alphanumeric identifier for the term unique to its type"), parent: z.number().optional().describe("The parent term ID"), meta: z.record(z.any()).optional().describe("Meta fields"), },
- src/index.ts:1924-1982 (registration)Tool registration via server.tool('create-category') call, including name, description, input schema, and handler function.server.tool( "create-category", "Create a new WordPress category", { siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), name: z.string().describe("HTML title for the term"), description: z.string().optional().describe("HTML description of the term"), slug: z.string().optional().describe("An alphanumeric identifier for the term unique to its type"), parent: z.number().optional().describe("The parent term ID"), meta: z.record(z.any()).optional().describe("Meta fields"), }, async ({ siteUrl, username, password, name, description, slug, parent, meta, }) => { try { const categoryData: Record<string, any> = { name }; if (description) categoryData.description = description; if (slug) categoryData.slug = slug; if (parent) categoryData.parent = parent; if (meta) categoryData.meta = meta; const category = await makeWPRequest<WPCategory>({ siteUrl, endpoint: "categories", method: "POST", auth: { username, password }, data: categoryData }); return { content: [ { type: "text", text: `Successfully created category:\nID: ${category.id}\nName: ${name}\nSlug: ${category.slug || "No slug"}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error creating category: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/index.ts:158-194 (helper)Shared helper function makeWPRequest used by create-category (and other tools) to make authenticated HTTP requests to WordPress REST API endpoints.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; } }
- src/index.ts:76-84 (schema)TypeScript interface WPCategory used for typing the response from WordPress categories API in create-category tool.interface WPCategory { id: number; name?: string; slug?: string; description?: string; parent?: number; count?: number; meta?: Record<string, any>; }