May 16, 2025
wordpress API

Starting to work with wordpress API

Ah, creating WordPress posts via the API using Postman is a fantastic way to automate content creation! Here’s a breakdown of how you can do it:

Prerequisites:

  • WordPress REST API Enabled: Ensure the WordPress REST API is enabled on your site. This is usually the default for recent WordPress versions.
  • Authentication: You’ll need a way to authenticate your requests. The most common methods are:
    • Basic Authentication: This involves using a username and password. You might need to install a plugin like “Basic Auth” for the WordPress REST API. Be cautious using this in production environments as it’s not the most secure.
    • OAuth 1.0a: A more secure method, often requiring a plugin like “OAuth 1.0a Server for WordPress REST API.”
    • Application Passwords: Introduced in WordPress 5.6, this is a more secure alternative to basic authentication for specific applications. You can create these within your WordPress user profile under “Account Security.”
  • Postman Installed: Make sure you have the Postman application installed on your computer.

Steps:

  1. Identify Your WordPress REST API Endpoint:The base URL for your WordPress REST API is typically your WordPress site’s URL followed by /wp-json/wp/v2/. To create a post, the specific endpoint will be: YOUR_WORDPRESS_SITE_URL/wp-json/wp/v2/posts Replace YOUR_WORDPRESS_SITE_URL with the actual URL of your WordPress website.
  2. Open Postman and Create a New Request:
    • Launch Postman.
    • Click the “+” button to open a new tab.
    • In the request builder, select POST as the HTTP method.
    • Enter the WordPress posts endpoint URL you identified in step 1 into the request URL field.
  3. Configure Authentication in Postman:Based on the authentication method you’ve chosen:
    • Basic Authentication:
      • Go to the “Authorization” tab in your Postman request.
      • Select “Basic Auth” from the “Type” dropdown.
      • Enter your WordPress username and password in the “Username” and “Password” fields.
    • OAuth 1.0a:
      • Go to the “Authorization” tab.
      • Select “OAuth 1.0” from the “Type” dropdown.
      • You’ll need to fill in the required details such as Consumer Key, Consumer Secret, Token, Token Secret, etc., which you would have obtained after setting up the OAuth plugin in WordPress.
    • Application Passwords:
      • Go to the “Authorization” tab.
      • Select “Basic Auth” from the “Type” dropdown.
      • Enter your WordPress username in the “Username” field.
      • Enter the generated Application Password in the “Password” field.
  4. Define the Post Data in the Request Body:
    • Go to the “Body” tab in your Postman request.
    • Select the “raw” radio button.
    • Choose “JSON” from the dropdown menu next to “raw.”
    • Enter the post data as a JSON object. Here are some common fields you might want to include:
    • JSON{ "title": "My New Post Title", "content": "This is the content of my new WordPress post.", "status": "publish" // You can also include other fields like: // "author": 1, // User ID of the author // "categories": [3, 5], // Array of category IDs // "tags": ["api", "wordpress", "postman"], // Array of tag names // "format": "standard", // "slug": "my-custom-slug", // "date": "2025-04-22T10:00:00" // Publication date }
      • title (required): The title of your post.
      • content (required): The main content of your post. You can include HTML here.
      • status (optional): The status of the post. Common values are "publish", "draft", "pending", "private". Defaults to "draft".
      • author (optional): The ID of the user who should be the author. Defaults to the currently authenticated user.
      • categories (optional): An array of category IDs to assign to the post.
      • tags (optional): An array of tag names to assign to the post. WordPress will automatically create these tags if they don’t exist.
      • format (optional): The post format (e.g., "standard", "aside", "image", "video"). Defaults to "standard".
      • slug (optional): A custom slug for the post URL. If not provided, WordPress will generate one from the title.
      • date (optional): The publication date of the post in ISO 8601 format (e.g., "2023-10-27T10:00:00").
  5. Send the Request:
    • Click the “Send” button in Postman.
  6. Examine the Response:
    • Postman will display the server’s response in the lower part of the window.
    • A successful post creation will typically return a JSON response with information about the newly created post, including its ID, title, content, link, and other details.
    • Check the status code of the response. A 201 Created status code indicates that the post was successfully created.
    • If there are errors, the response body will usually contain an error message and code to help you diagnose the issue. Common errors might be related to authentication, invalid data, or server-side problems.

Example using Basic Authentication and the JSON body above:

  • Method: POST
  • URL: https://your-wordpress-site.com/wp-json/wp/v2/posts
  • Authorization: Basic Auth (with your WordPress username and password or application password)
  • Body (raw JSON): JSON{ "title": "My New Post Title from Postman", "content": "<p>This post was created using the WordPress REST API and Postman!</p>", "status": "publish", "tags": ["api", "postman", "automation"] }

Important Considerations:

  • Security: Be very careful about exposing your WordPress username and password. Using Application Passwords is generally recommended over basic authentication for better security. For production environments, OAuth 1.0a offers the most robust security.
  • Error Handling: Always check the HTTP status code and the response body for any errors.
  • Rate Limiting: Be mindful of potential rate limiting on your WordPress host, especially if you’re making frequent API requests.
  • Media Uploads: Creating posts with featured images or other media involves a separate endpoint (/wp-json/wp/v2/media) and a different request type (multipart/form-data).
  • Custom Fields (Meta Data): To add custom fields, you’ll often need to register them via PHP in your WordPress theme’s functions.php file or a plugin and then include them in your JSON request under the meta key.

By following these steps, you should be able to successfully create WordPress posts using the REST API and Postman! Let me know if you have any specific issues or more advanced scenarios you’d like to explore.

Jason Benway

Christ follower, husband, father, IT geek, and Xbox gamer

View all posts by Jason Benway →

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.