update-user
Modify WordPress user details, including username, email, password, display name, and roles, using the 'update-user' tool for streamlined account management on WordPress MCP Server.
Instructions
Update an existing WordPress user
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| description | No | New description of the user | |
| No | New email address for the user | ||
| firstName | No | New first name for the user | |
| lastName | No | New last name for the user | |
| locale | No | New locale for the user | |
| name | No | New display name for the user | |
| newPassword | No | New password for the user | |
| newUsername | No | New login name for the user | |
| nickname | No | New nickname for the user | |
| password | Yes | WordPress application password | |
| roles | No | New roles assigned to the user | |
| siteUrl | Yes | WordPress site URL | |
| slug | No | New alphanumeric identifier for the user | |
| url | No | New URL of the user | |
| userId | Yes | User ID or 'me' for current user | |
| username | Yes | WordPress username |
Implementation Reference
- src/index.ts:447-518 (handler)Handler function that constructs update data from inputs, calls makeWPRequest to POST to /wp-json/wp/v2/users/{userId}, and returns success/error message.async ({ siteUrl, username, password, userId, newUsername, email, newPassword, name, firstName, lastName, url, description, locale, nickname, slug, roles, }) => { try { const userData: Record<string, any> = {}; if (newUsername) userData.username = newUsername; if (email) userData.email = email; if (newPassword) userData.password = newPassword; if (name) userData.name = name; if (firstName) userData.first_name = firstName; if (lastName) userData.last_name = lastName; if (url) userData.url = url; if (description) userData.description = description; if (locale) userData.locale = locale; if (nickname) userData.nickname = nickname; if (slug) userData.slug = slug; if (roles) userData.roles = roles; if (Object.keys(userData).length === 0) { return { content: [ { type: "text", text: "No update data provided. Please specify at least one field to update.", }, ], }; } const user = await makeWPRequest<WPUser>({ siteUrl, endpoint: `users/${userId}`, method: "POST", auth: { username, password }, data: userData }); return { content: [ { type: "text", text: `Successfully updated user:\nID: ${user.id}\nUsername: ${user.name || newUsername || "Unchanged"}\nEmail: ${email || "Unchanged"}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error updating user: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }
- src/index.ts:429-446 (schema)Zod schema defining all input parameters for the update-user tool, including site credentials, user ID, and optional fields to update.{ siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), userId: z.union([z.string(), z.number(), z.literal("me")]).describe("User ID or 'me' for current user"), newUsername: z.string().optional().describe("New login name for the user"), email: z.string().email().optional().describe("New email address for the user"), newPassword: z.string().optional().describe("New password for the user"), name: z.string().optional().describe("New display name for the user"), firstName: z.string().optional().describe("New first name for the user"), lastName: z.string().optional().describe("New last name for the user"), url: z.string().url().optional().describe("New URL of the user"), description: z.string().optional().describe("New description of the user"), locale: z.enum(["", "en_US"]).optional().describe("New locale for the user"), nickname: z.string().optional().describe("New nickname for the user"), slug: z.string().optional().describe("New alphanumeric identifier for the user"), roles: z.array(z.string()).optional().describe("New roles assigned to the user"), },
- src/index.ts:426-519 (registration)Registration of the 'update-user' tool using server.tool() with name, description, input schema, and handler function.server.tool( "update-user", "Update an existing WordPress user", { siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), userId: z.union([z.string(), z.number(), z.literal("me")]).describe("User ID or 'me' for current user"), newUsername: z.string().optional().describe("New login name for the user"), email: z.string().email().optional().describe("New email address for the user"), newPassword: z.string().optional().describe("New password for the user"), name: z.string().optional().describe("New display name for the user"), firstName: z.string().optional().describe("New first name for the user"), lastName: z.string().optional().describe("New last name for the user"), url: z.string().url().optional().describe("New URL of the user"), description: z.string().optional().describe("New description of the user"), locale: z.enum(["", "en_US"]).optional().describe("New locale for the user"), nickname: z.string().optional().describe("New nickname for the user"), slug: z.string().optional().describe("New alphanumeric identifier for the user"), roles: z.array(z.string()).optional().describe("New roles assigned to the user"), }, async ({ siteUrl, username, password, userId, newUsername, email, newPassword, name, firstName, lastName, url, description, locale, nickname, slug, roles, }) => { try { const userData: Record<string, any> = {}; if (newUsername) userData.username = newUsername; if (email) userData.email = email; if (newPassword) userData.password = newPassword; if (name) userData.name = name; if (firstName) userData.first_name = firstName; if (lastName) userData.last_name = lastName; if (url) userData.url = url; if (description) userData.description = description; if (locale) userData.locale = locale; if (nickname) userData.nickname = nickname; if (slug) userData.slug = slug; if (roles) userData.roles = roles; if (Object.keys(userData).length === 0) { return { content: [ { type: "text", text: "No update data provided. Please specify at least one field to update.", }, ], }; } const user = await makeWPRequest<WPUser>({ siteUrl, endpoint: `users/${userId}`, method: "POST", auth: { username, password }, data: userData }); return { content: [ { type: "text", text: `Successfully updated user:\nID: ${user.id}\nUsername: ${user.name || newUsername || "Unchanged"}\nEmail: ${email || "Unchanged"}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error updating user: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/index.ts:158-194 (helper)Helper function makeWPRequest used by the handler 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:59-64 (schema)TypeScript interface WPUser used as return type for makeWPRequest in the handler.interface WPUser { id: number; name?: string; slug?: string; roles?: string[]; }