### Model Examples Source: https://github.com/wp-api/docs/blob/master/using-the-rest-api/backbone-javascript-client.md Examples demonstrating how to create, load, and manipulate individual post models, including managing categories, authors, and featured media. ```APIDOC ## Model Examples ### Create and Save a New Post **Description**: Creates a new post model and saves it to the server. **Method**: Not applicable (client-side JavaScript) **Endpoint**: Not applicable (client-side JavaScript) ### Request Example ```javascript var post = new wp.api.models.Post( { title: 'This is a test post' } ); post.save(); ``` ### Load an Existing Post **Description**: Loads an existing post by its ID. **Method**: Not applicable (client-side JavaScript) **Endpoint**: Not applicable (client-side JavaScript) ### Request Example ```javascript var post = new wp.api.models.Post( { id: 1 } ); post.fetch(); ``` ### Get Post Categories **Description**: Retrieves the categories associated with a post. Returns a promise. **Method**: Not applicable (client-side JavaScript) **Endpoint**: Not applicable (client-side JavaScript) ### Response Example ```javascript post.getCategories().done( function( postCategories ) { console.log( postCategories[0].name ); // e.g., "Uncategorized" } ); ``` ### Get Post Author **Description**: Retrieves the author of a post as a User model. Returns a promise. **Method**: Not applicable (client-side JavaScript) **Endpoint**: Not applicable (client-side JavaScript) ### Response Example ```javascript post.getAuthorUser().done( function( user ){ console.log( user.get( "name" ) ); } ``` ### Get Featured Media **Description**: Retrieves the featured media for a post as a Media model. Returns a promise. **Method**: Not applicable (client-side JavaScript) **Endpoint**: Not applicable (client-side JavaScript) ### Response Example ```javascript post.getFeaturedMedia().done( function( image ){ console.log( image ); } ``` ### Set Post Categories **Description**: Sets the categories for a post. **Method**: Not applicable (client-side JavaScript) **Endpoint**: Not applicable (client-side JavaScript) ### Request Example ```javascript post.setCategories( [ "apples", "oranges" ] ); ``` ### Get All Categories **Description**: Retrieves all available categories. **Method**: Not applicable (client-side JavaScript) **Endpoint**: Not applicable (client-side JavaScript) ### Request Example ```javascript var allCategories = new wp.api.collections.Categories(); allCategories.fetch(); ``` ### Add a Category to a Post **Description**: Adds a category to a post. **Method**: POST **Endpoint**: Not applicable (client-side JavaScript) ### Request Example ```javascript var appleCategory = allCategories.findWhere( { slug: "apples" } ); appleCategory.set( "parent_post", post.get( "id" ) ); postCategories.create( appleCategory.toJSON(), { type: "POST" } ); ``` ### Remove a Category from a Post **Description**: Removes a category from a post. **Method**: DELETE **Endpoint**: Not applicable (client-side JavaScript) ### Request Example ```javascript postCategories.at( 0 ).destroy(); ``` ``` -------------------------------- ### Collection Examples Source: https://github.com/wp-api/docs/blob/master/using-the-rest-api/backbone-javascript-client.md Examples for fetching collections of posts with options for pagination, filtering, and ordering. ```APIDOC ## Collection Examples ### Get Last 10 Posts **Description**: Fetches the last 10 posts. **Method**: GET **Endpoint**: `/wp/v2/posts` ### Request Example ```javascript var postsCollection = new wp.api.collections.Posts(); postsCollection.fetch(); ``` ### Get Last 25 Posts **Description**: Fetches the last 25 posts. **Method**: GET **Endpoint**: `/wp/v2/posts` ### Request Example ```javascript postsCollection.fetch( { data: { per_page: 25 } } ); ``` ### Filter and Order Posts **Description**: Fetches posts filtered by title and ordered ascendingly. **Method**: GET **Endpoint**: `/wp/v2/posts` ### Request Example ```javascript postsCollection.fetch( { data: { 'filter': { 'orderby': 'title', 'order': 'ASC' } } } ); ``` ### Get Next Page of Posts **Description**: Retrieves the next page of results for a collection. **Method**: GET **Endpoint**: `/wp/v2/posts` ### Request Example ```javascript postsCollection.more(); ``` ### Get Specific Page of Posts **Description**: Fetches a specific page of posts (e.g., page 5). **Method**: GET **Endpoint**: `/wp/v2/posts` ### Request Example ```javascript posts.fetch( { data: { page: 5 } } ); ``` ### Check for More Posts **Description**: Checks if there are more posts available in the collection. **Method**: Not applicable (client-side JavaScript) **Endpoint**: Not applicable (client-side JavaScript) ### Response Example ```javascript posts.hasMore(); // Returns true or false ``` ``` -------------------------------- ### WP REST API Batch Operations Example Source: https://context7.com/wp-api/docs/llms.txt Demonstrates a batch request to the WordPress REST API, allowing multiple operations (GET, POST, PUT) in a single request. This is useful for improving efficiency by reducing the number of HTTP requests. ```json { "requests": [ { "method": "GET", "route": "/wp/v2/posts", "params": {"_fields": "id,title,slug"} }, { "method": "POST", "route": "/wp/v2/posts", "body": {"title": "New Post via Batch", "status": "publish"} }, { "method": "GET", "route": "/wp/v2/users/me", "params": {"_fields": "id,name,email"} }, { "method": "PUT", "route": "/wp/v2/posts/123", "body": {"title": "Updated via batch"} } ], "stop_on_error": false } ``` -------------------------------- ### Example POST Request for Product Creation Source: https://context7.com/wp-api/docs/llms.txt This is an example JSON payload demonstrating the structure and expected values for creating a product using the WP-API. It includes all optional and required fields such as name, price, SKU, quantity, categories, tags, attributes, launch date, website, and contact email. ```json { "name": "Premium Widget", "price": 49.99, "sku": "WIDGET-001", "quantity": 100, "categories": [1, 3, 5], "status": "active", "tags": ["premium", "bestseller"], "attributes": { "weight": 1.5, "dimensions": {"length": 10, "width": 5, "height": 2}, "color": "#FF5733" }, "launch_date": "2025-02-01T00:00:00", "website": "https://example.com/products/widget", "contact_email": "support@example.com" } ``` -------------------------------- ### GET /wp-json/my-shop/v1/products Source: https://github.com/wp-api/docs/blob/master/extending-the-rest-api/routes-and-endpoints.md Retrieves a list of products from the API. ```APIDOC ## GET /wp-json/my-shop/v1/products ### Description This endpoint retrieves a list of products. ### Method GET ### Endpoint /wp-json/my-shop/v1/products ### Parameters #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "GET request to /wp-json/my-shop/v1/products" } ``` ### Response #### Success Response (200) - **products** (array) - An associative array where keys are product IDs and values are product names. #### Response Example ```json { "1": "I am product 1", "2": "I am product 2", "3": "I am product 3" } ``` ``` -------------------------------- ### GET /my-shop/v1/products Source: https://github.com/wp-api/docs/blob/master/extending-the-rest-api/routes-and-endpoints.md Retrieves a collection of products from the shop. ```APIDOC ## GET /my-shop/v1/products ### Description Retrieves a list of all available products. ### Method GET ### Endpoint /my-shop/v1/products ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this endpoint." } ``` ### Response #### Success Response (200) - **products** (object) - An object containing product data. #### Response Example ```json { "1": "I am product 1", "2": "I am product 2", "3": "I am product 3" } ``` ``` -------------------------------- ### String Pattern Validation (PHP) Source: https://github.com/wp-api/docs/blob/master/extending-the-rest-api/schema.md Validates that a string field matches a regular expression using the pattern keyword. The regex is ECMA 262 compliant and not automatically anchored. This example requires strings to start with '#' followed by one or more digits. ```php array( 'type' => 'string', 'pattern' => '#[0-9]+', ); ``` -------------------------------- ### Discover API Extensions/Namespaces (JSON Example) Source: https://github.com/wp-api/docs/blob/master/using-the-rest-api/discovery.md This JSON output demonstrates how to discover the extensions or namespaces supported by the API. It lists available namespaces like 'oembed/1.0/' and 'wp/v2'. ```json { "name": "Example WordPress Site", "namespaces": [ "oembed/1.0/" ] } ``` ```json { "name": "Example WordPress Site", "namespaces": [ "wp/v2", "oembed/1.0/" ] } ``` ```json { "name": "Example WordPress Site", "namespaces": [ "wp/v2", "oembed/1.0/", "testplugin/v1" ] } ``` -------------------------------- ### Register Karma Field for Comments (PHP) Source: https://github.com/wp-api/docs/blob/master/extending-the-rest-api/modifying-responses.md This example demonstrates how to use `register_rest_field` to add a 'karma' field to comment responses in the WordPress REST API. It includes callbacks for getting and updating the comment karma, and defines the field's schema. This makes the 'karma' field accessible and modifiable via API requests. ```php function( $comment_arr ) { $comment_obj = get_comment( $comment_arr['id'] ); return (int) $comment_obj->comment_karma; }, 'update_callback' => function( $karma, $comment_obj ) { $ret = wp_update_comment( array( 'comment_ID' => $comment_obj->comment_ID, 'comment_karma' => $karma ) ); if ( false === $ret ) { return new WP_Error( 'rest_comment_karma_failed', __( 'Failed to update comment karma.' ), array( 'status' => 500 ) ); } return true; }, 'schema' => array( 'description' => __( 'Comment karma.' ), 'type' => 'integer' ), ) ); } ); ?> ``` -------------------------------- ### POST /wp-json/myapp/v1/products Source: https://context7.com/wp-api/docs/llms.txt Creates a new product with the provided details. The endpoint accepts product information such as name, price, SKU, quantity, categories, status, tags, attributes, launch date, website, and contact email. ```APIDOC ## POST /wp-json/myapp/v1/products ### Description Creates a new product with the provided details. The endpoint accepts product information such as name, price, SKU, quantity, categories, status, tags, attributes, launch date, website, and contact email. ### Method POST ### Endpoint /wp-json/myapp/v1/products ### Parameters #### Request Body - **name** (string) - Required - The name of the product. - **price** (number) - Required - The price of the product. - **sku** (string) - Required - The Stock Keeping Unit for the product. - **quantity** (number) - Required - The current stock quantity of the product. - **categories** (array of numbers) - Required - A list of category IDs for the product. - **status** (string) - Required - The status of the product (e.g., 'active', 'draft'). - **tags** (array of strings) - Optional - A list of tags for the product. - **attributes** (object) - Optional - A key-value object for product attributes (e.g., weight, dimensions, color). - **weight** (number) - Optional - The weight of the product. - **dimensions** (object) - Optional - The dimensions of the product. - **length** (number) - Optional - The length. - **width** (number) - Optional - The width. - **height** (number) - Optional - The height. - **color** (string) - Optional - The color of the product. - **launch_date** (string) - Optional - The launch date of the product in ISO 8601 format. - **website** (string) - Optional - The website URL for the product. - **contact_email** (string) - Optional - The contact email for product inquiries. ### Request Example ```json { "name": "Premium Widget", "price": 49.99, "sku": "WIDGET-001", "quantity": 100, "categories": [1, 3, 5], "status": "active", "tags": ["premium", "bestseller"], "attributes": { "weight": 1.5, "dimensions": {"length": 10, "width": 5, "height": 2}, "color": "#FF5733" }, "launch_date": "2025-02-01T00:00:00", "website": "https://example.com/products/widget", "contact_email": "support@example.com" } ``` ### Response #### Success Response (201) - **id** (number) - The ID of the newly created product. - **message** (string) - A confirmation message indicating successful creation. #### Response Example ```json { "id": 123, "message": "Product created successfully" } ``` ``` -------------------------------- ### REST API Interaction for Meta Fields (Example) Source: https://context7.com/wp-api/docs/llms.txt Demonstrates how to retrieve and update registered meta fields for a post via the WordPress REST API. GET requests retrieve the post data including meta, while POST requests to the post endpoint with a 'meta' object in the body can update these fields. ```http // Usage via REST API: // GET /wp/v2/posts/1 // Response: { // "id": 1, // "title": "...", // "meta": { // "subtitle": "A comprehensive guide", // "book_details": { // "isbn": "978-0-123456-78-9", // "publisher": "Tech Press", // "pages": 450, // "price": 29.99, // "publication_date": "2025-01-15" // }, // "authors": [ // {"name": "John Doe", "role": "author", "email": "john@example.com"}, // {"name": "Jane Smith", "role": "co-author", "email": "jane@example.com"} // ] // } // } // Update via REST API: // POST /wp/v2/posts/1 // Body: { // "meta": { // "book_details": { // "isbn": "978-0-123456-78-9", // "publisher": "Tech Press", // "pages": 450, // "price": 29.99, // "publication_date": "2025-01-15" // } // } // } ``` -------------------------------- ### GET /wp/v2/taxonomies Source: https://github.com/wp-api/docs/blob/master/reference/taxonomies.md Retrieve a specific taxonomy record. You can query this endpoint to get details about available taxonomies. ```APIDOC ## GET /wp/v2/taxonomies ### Description Query this endpoint to retrieve a specific taxonomy record. ### Method GET ### Endpoint /wp/v2/taxonomies ### Query Parameters - **context** (string) - Optional - Scope under which the request is made; determines fields present in response. Default: `view`. One of: `view`, `embed`, `edit` - **type** (string) - Optional - Limit results to taxonomies associated with a specific post type. ### Request Example ```bash curl https://example.com/wp-json/wp/v2/taxonomies ``` ### Response #### Success Response (200) - **capabilities** (object) - All capabilities used by the taxonomy. - **description** (string) - A human-readable description of the taxonomy. - **hierarchical** (boolean) - Whether or not the taxonomy should have children. - **labels** (object) - Human-readable labels for the taxonomy for various contexts. - **name** (string) - The title for the taxonomy. - **slug** (string) - An alphanumeric identifier for the taxonomy. - **show_cloud** (boolean) - Whether or not the term cloud should be displayed. - **types** (array) - Types associated with the taxonomy. - **rest_base** (string) - REST base route for the taxonomy. - **rest_namespace** (string) - REST namespace route for the taxonomy. - **visibility** (object) - The visibility settings for the taxonomy. #### Response Example ```json { "capabilities": { "edit_terms": "edit_categories", "assign_terms": "edit_categories" }, "description": "", "hierarchical": true, "labels": { "name": "Categories", "singular_name": "Category" }, "name": "category", "slug": "category", "show_cloud": false, "types": [ "post" ], "rest_base": "categories", "rest_namespace": "wp/v2", "visibility": { "public": true, "choose_from_most_used": true, "show_admin_column": true, "show_in_rest": true } } ``` ``` -------------------------------- ### GET /wp/v2/users/me Source: https://github.com/wp-api/docs/blob/master/reference/users.md Retrieves the currently authenticated user's information. Useful for getting details about the logged-in user. ```APIDOC ## GET /wp/v2/users/me ### Description Retrieves the currently authenticated user's information. Useful for getting details about the logged-in user. ### Method GET ### Endpoint /wp/v2/users/me ### Parameters #### Query Parameters - **context** (string) - Optional - Scope under which the request is made; determines fields present in response. Default: `view`. One of: `view`, `embed`, `edit`. ### Request Example ```bash $ curl https://example.com/wp-json/wp/v2/users/me ``` ### Response #### Success Response (200) - **id** (integer) - The user's ID. - **username** (string) - The user's login name. - **name** (string) - The user's display name. - **first_name** (string) - The user's first name. - **last_name** (string) - The user's last name. - **email** (string) - The user's email address. - **url** (string) - The user's URL. - **description** (string) - The user's description. - **link** (string) - A link to the user's profile. - **locale** (string) - The user's locale. - **nickname** (string) - The user's nickname. - **slug** (string) - The user's slug. - **roles** (array) - The user's roles. - **registered_date** (string) - The date the user registered. - **meta** (object) - Meta fields. #### Response Example { "example": "{\"id\": 1, \"username\": \"admin\", \"name\": \"Administrator\", \"first_name\": \"\", \"last_name\": \"\", \"email\": \"admin@example.com\", \"url\": \"\", \"description\": \"\", \"link\": \"https://example.com/wp-json/wp/v2/users/1\", \"locale\": \"en_US\", \"nickname\": \"\", \"slug\": \"admin\", \"roles\": [\"administrator\"], \"registered_date\": \"2023-01-01T12:00:00\", \"meta\": {}}" } ``` -------------------------------- ### POST /myapp/v1/products Source: https://context7.com/wp-api/docs/llms.txt Creates a new product with detailed specifications. Supports extensive validation rules for product attributes. ```APIDOC ## POST /myapp/v1/products ### Description Creates a new product with detailed specifications. Supports extensive validation rules for product attributes. ### Method POST ### Endpoint /myapp/v1/products ### Parameters #### Request Body - **name** (string) - Required - Product name. Min length: 3, Max length: 100. - **price** (number) - Required - Product price in USD. Minimum: 0, Maximum: 999999.99. - **sku** (string) - Required - Stock keeping unit. Must match pattern `^[A-Z0-9-]{5,20}$`. - **quantity** (integer) - Optional - Available quantity. Defaults to 0. Minimum: 0. - **categories** (array) - Required - Product category IDs. Each item must be an integer >= 1. Min items: 1, Max items: 10. Items must be unique. - **status** (string) - Optional - Product status. Allowed values: `draft`, `active`, `archived`. Defaults to `draft`. - **tags** (array) - Optional - Product tags. Each item must be a string with min length 2 and max length 30. - **attributes** (object) - Optional - Product attributes. - **weight** (number) - Optional - Product weight. Minimum: 0. - **dimensions** (object) - Optional - Product dimensions. - **length** (number) - Required - Minimum: 0. - **width** (number) - Required - Minimum: 0. - **height** (number) - Required - Minimum: 0. - **color** (string) - Optional - Product color in hex format (e.g., `#RRGGBB`). - **launch_date** (string) - Optional - Product launch date in `date-time` format. - **website** (string) - Optional - Product website URL. Must be a valid URI. - **contact_email** (string) - Optional - Contact email. Must be a valid email format. - **metadata** (object) - Optional - Additional metadata with any properties. ### Request Example ```json { "name": "Example Widget", "price": 19.99, "sku": "WIDGET-001", "quantity": 100, "categories": [1, 5], "status": "active", "tags": ["electronics", "gadget"], "attributes": { "weight": 0.5, "dimensions": { "length": 10, "width": 5, "height": 2 }, "color": "#FF0000" }, "launch_date": "2023-10-27T10:00:00Z", "website": "https://example.com/widget", "contact_email": "support@example.com", "metadata": { "internal_id": "XYZ789" } } ``` ### Response #### Success Response (201 Created) - **id** (integer) - The unique identifier for the newly created product. - **message** (string) - A confirmation message indicating successful creation. #### Response Example ```json { "id": 123, "message": "Product created successfully." } ``` ``` -------------------------------- ### POST /wp-json/my-shop/v1/products Source: https://github.com/wp-api/docs/blob/master/extending-the-rest-api/routes-and-endpoints.md Creates a new product resource via the API. ```APIDOC ## POST /wp-json/my-shop/v1/products ### Description This endpoint creates a new product. ### Method POST ### Endpoint /wp-json/my-shop/v1/products ### Parameters #### Query Parameters None #### Request Body None (In a real scenario, this would contain product data). ### Request Example ```json { "example": "POST request to /wp-json/my-shop/v1/products" } ``` ### Response #### Success Response (200) - **message** (string) - A confirmation message indicating the product has been created. #### Response Example ```json { "message": "Product has been created" } ``` ``` -------------------------------- ### Create a Page Source: https://github.com/wp-api/docs/blob/master/reference/pages.md Creates a new page with the provided details. ```APIDOC ## POST /wp-api/pages ### Description Creates a new page in the WordPress site. ### Method POST ### Endpoint /wp-api/pages ### Parameters #### Request Body - **date** (string) - Optional - The date the post was published, in the site's timezone. - **date_gmt** (string) - Optional - The date the post was published, as GMT. - **slug** (string) - Optional - An alphanumeric identifier for the post unique to its type. - **status** (string) - Optional - A named status for the post. One of: `publish`, `future`, `draft`, `pending`, `private`. ### Request Example ```json { "title": "My New Page", "slug": "my-new-page", "status": "publish" } ``` ### Response #### Success Response (201) - **id** (integer) - The ID of the created page. - **date** (string) - The date the page was published. - **slug** (string) - The slug of the created page. - **status** (string) - The status of the created page. #### Response Example ```json { "id": 10, "date": "2023-10-27T10:30:00", "slug": "my-new-page", "status": "publish" } ``` ``` -------------------------------- ### Permissions Callback Example Source: https://github.com/wp-api/docs/blob/master/extending-the-rest-api/routes-and-endpoints.md Example of registering a permissions callback for a private data endpoint in the WordPress REST API. ```APIDOC ## GET /my-plugin/v1/private-data ### Description Retrieves private data, accessible only to users with the 'edit_posts' capability. ### Method GET ### Endpoint /my-plugin/v1/private-data ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "example": "No request body needed for this GET request." } ``` ### Response #### Success Response (200) - **data** (string) - The private data string. #### Response Example ```json { "data": "This is private data." } ``` #### Error Response (401) - **code** (string) - 'rest_forbidden' - **message** (string) - 'OMG you can not view private data.' - **data** (object) - {'status': 401} #### Error Response Example ```json { "code": "rest_forbidden", "message": "OMG you can not view private data.", "data": { "status": 401 } } ``` ``` -------------------------------- ### Discover API Authentication Methods (JSON Example) Source: https://github.com/wp-api/docs/blob/master/using-the-rest-api/discovery.md This JSON structure shows how to discover available authentication methods for the API. The 'authentication' key contains details for each method, such as OAuth1. ```json { "name": "Example WordPress Site", "description": "YOLO", "routes": { ... }, "authentication": { "oauth1": { "request": "http://example.com/oauth/request", "authorize": "http://example.com/oauth/authorize", "access": "http://example.com/oauth/access", "version": "0.1" } } } ``` -------------------------------- ### GET /my-namespace/v1/schema-arg Source: https://github.com/wp-api/docs/blob/master/extending-the-rest-api/schema.md This endpoint demonstrates the use of argument schema for a GET request. It expects a 'my-arg' string parameter, which is validated and sanitized. ```APIDOC ## GET /my-namespace/v1/schema-arg ### Description This endpoint retrieves a single item and demonstrates how to use JSON Schema for argument validation and sanitization. The 'my-arg' parameter is required and must be a string. ### Method GET ### Endpoint /my-namespace/v1/schema-arg ### Parameters #### Query Parameters - **my-arg** (string) - Required - This is the argument our endpoint returns. It will be validated to ensure it is a string and sanitized using `sanitize_text_field`. ### Request Example ```json { "example": "(No request body for GET, parameters are passed as query parameters)" } ``` ### Response #### Success Response (200) - **string** - The sanitized value of the 'my-arg' query parameter. #### Response Example ```json { "example": "value_from_my_arg" } ``` #### Error Responses - **400** - If 'my-arg' is not provided or is not a string. - **500** - If an internal error occurs during sanitization. ``` -------------------------------- ### Create Post Source: https://context7.com/wp-api/docs/llms.txt Demonstrates how to create a new post using the WordPress REST API. ```APIDOC ## POST /wp-json/wp/v2/posts ### Description Creates a new post on the WordPress site. ### Method POST ### Endpoint /wp-json/wp/v2/posts ### Request Body - **title** (string) - Required - The title of the post. - **content** (string) - Optional - The main content of the post. - **status** (string) - Optional - The status of the post (e.g., 'publish', 'draft', 'pending'). Defaults to 'publish'. ### Request Example ```json { "title": "Post from Python", "content": "Created using Python requests library", "status": "publish" } ``` ### Response #### Success Response (201 Created) - **id** (integer) - The unique identifier of the newly created post. - **title** (object) - The title of the post. - **content** (object) - The content of the post. - **status** (string) - The status of the post. #### Response Example ```json { "id": 123, "title": { "rendered": "Post from Python" }, "content": { "rendered": "
Created using Python requests library
", "protected": false }, "status": "publish" } ``` ``` -------------------------------- ### Bash: WP-API Complex Query with Multiple Optimizations Source: https://context7.com/wp-api/docs/llms.txt An example of a comprehensive `curl` command to the WP-API, demonstrating the combination of multiple query parameters for pagination, sorting, filtering, field selection, and embedding. ```bash # Complex query with multiple optimizations curl "https://example.com/wp-json/wp/v2/posts?\ per_page=5&\ page=1&\ orderby=date&\ order=desc&\ status=publish&\ _fields=id,title,excerpt,date,_links,_embedded&\ _embed=author" ``` -------------------------------- ### Example: Delete Plugin using cURL Source: https://github.com/wp-api/docs/blob/master/reference/plugins.md This example shows how to delete a plugin using the cURL command-line tool with the WP REST API. It sends a DELETE request to the specified endpoint, including the plugin identifier. ```shell $ curl -X DELETE https://example.com/wp-json/wp/v2/plugins/Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
", "protected": false }, "author": 1, "excerpt": { "rendered": "", "protected": false }, "featured_media": 0, "comment_status": "open", "ping_status": "open", "format": "standard", "meta": {}, "sticky": false } ``` ``` -------------------------------- ### Resource Discovery Link Header (HTML Example) Source: https://github.com/wp-api/docs/blob/master/using-the-rest-api/discovery.md This HTML link element, typically found in the `` section, provides a mechanism for discovering the REST API route equivalent of the current document. It specifies the alternate resource as JSON. ```html ``` -------------------------------- ### GET /wp/v2/templates/This is my custom template.
", "title": "My Custom Template", "description": "", "status": "publish", "author": 1, "source": "theme", "is_custom": false } } ``` ``` -------------------------------- ### Routes and Endpoints Explanation Source: https://github.com/wp-api/docs/blob/master/glossary.md Explains the concept of routes and endpoints in the context of the WP-API. ```APIDOC ## Routes and Endpoints ### Description This section clarifies the distinction and relationship between routes and endpoints within the WP-API framework. ### Concept An endpoint is a specific function exposed by the API, such as retrieving data or updating a resource. A route is the URL path used to access these endpoints. A single route can expose multiple endpoints, differentiated by the HTTP verb used (e.g., GET, PUT, DELETE). ### Example For the URL `http://example.com/wp-json/wp/v2/posts/123`: - The **route** is `wp/v2/posts/123`. - This route has associated endpoints for different HTTP verbs: - `GET`: Triggers a `get_item` method to retrieve post data. - `PUT`: Triggers an `update_item` method to modify the post. - `DELETE`: Triggers a `delete_item` method to remove the post. ### Permalink Variations On sites without pretty permalinks, the route is passed as a query parameter: `http://example.com/?rest_route=wp/v2/posts/123` ``` -------------------------------- ### HTTP Methods Overview Source: https://github.com/wp-api/docs/blob/master/extending-the-rest-api/routes-and-endpoints.md Explains the standard HTTP methods used by the WordPress REST API and how to handle unsupported methods. ```APIDOC ## HTTP Methods Overview ### Description This section details the common HTTP methods used in the WordPress REST API and provides information on handling methods that might not be directly supported by all clients. ### Methods * **GET**: Used for retrieving data. * **POST**: Used for creating new resources. * **PUT**: Used for updating existing resources. * **DELETE**: Used for deleting resources. * **OPTIONS**: Used to provide context about available resources. ### Handling Unsupported Methods If a client cannot send a specific HTTP method (e.g., `DELETE`), the API supports overriding the method using the `_method` query parameter or the `X-HTTP-Method-Override` header. This is typically done by sending a `POST` request with the desired method specified in the parameter or header. ### Example of Method Override To delete a resource using a `POST` request when `DELETE` is not supported: ``` POST /wp-json/my-shop/v1/products/1?_method=DELETE ``` This allows clients to perform actions like deletion even with method restrictions. ``` -------------------------------- ### GET /wp/v2/sidebars Source: https://github.com/wp-api/docs/blob/master/reference/sidebars.md Query this endpoint to retrieve a specific sidebar record. ```APIDOC ## GET /wp/v2/sidebars ### Description Query this endpoint to retrieve a specific sidebar record. ### Method GET ### Endpoint /wp/v2/sidebars ### Query Parameters - **context** (string) - Optional - Scope under which the request is made; determines fields present in response. Default: `view`. One of: `view`, `embed`, `edit` ### Request Example ```bash curl https://example.com/wp-json/wp/v2/sidebars ``` ### Response #### Success Response (200) - **id** (string) - ID of sidebar. - **name** (string) - Unique name identifying the sidebar. - **description** (string) - Description of sidebar. - **class** (string) - Extra CSS class to assign to the sidebar in the Widgets interface. - **before_widget** (string) - HTML content to prepend to each widget's HTML output when assigned to this sidebar. Default is an opening list item element. - **after_widget** (string) - HTML content to append to each widget's HTML output when assigned to this sidebar. Default is a closing list item element. - **before_title** (string) - HTML content to prepend to the sidebar title when displayed. Default is an opening h2 element. - **after_title** (string) - HTML content to append to the sidebar title when displayed. Default is a closing h2 element. - **status** (string) - Status of sidebar. One of: `active`, `inactive`. - **widgets** (array) - Nested widgets. #### Response Example ```json { "id": "1", "name": "Main Sidebar", "description": "", "class": "", "before_widget": "The JavaScript Post Snippets plugin allows you to add custom JavaScript snippets to your posts and pages.
", "protected": false, "protected_scheme": null }, "author": { "name": "WordPress Contributors", "uri": "https://wordpress.org/", "slug": "wordpress-contributors", "avatar_url": "https://secure.gravatar.com/avatar/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?s=24&d=mm&r=g", "description": "", "link_to_author_url": null }, "author_uri": "https://wordpress.org/", "title": "JavaScript Post Snippets by WordPress Contributors", "author_name": "WordPress Contributors", "plugin_uri_link": "https://wordpress.org/plugins/jsps/", "version_link": "https://wordpress.org/plugins/jsps/changelog/", "network_only": false, "requires_wp": "4.7", "requires_php": "5.6", "textdomain": "jsps", "website": [ "https://wordpress.org/plugins/jsps/" ], "update": null, "update_uri": null, "auto_updates": null, "package": null, "plugin_dir": "jsps" } ``` ```