WP2 Wiki
README

Untitled page

This Coda pack integrates with the WordPress REST API to manage custom items called “Things.” It provides functions for creating, updating, retrieving, and deleting “Things” and includes features for handling metadata, error management, and streamlined payload construction. This integration uses WordPress’s Application Passwords for authentication.

Features

User Authentication: Secure authentication using WordPress Application Passwords.
Create, Update, and Delete: Functions for creating new “Things,” updating existing ones, and deleting them by ID.
Data Retrieval: Sync table to retrieve all “Things” and automatically continue paging if necessary.
Error Handling: Custom error handling to display clear messages based on the WordPress API response.
Metadata Support: Supports additional metadata fields through flexible key-value parameters.

Getting Started

Authentication

This pack uses WordPress Application Passwords to authenticate. To get started:
Generate an Application Password in WordPress for your account.
Enter your WordPress username and Application Password in the Coda pack settings.
Add the WordPress site endpoint URL (e.g., https://example.com) in the authentication settings.
For more information, see the .

Functions

CreateThing

Creates a new “Thing” in WordPress.
Parameters:
Name: The name of the “Thing.”
Handle: A slug or handle for the “Thing.”
Description: A description for the “Thing.”
Key and Value: Vararg parameters to add metadata key-value pairs.

DeleteThing

Deletes a “Thing” by its ID.
Parameters:
Thing: The ID of the “Thing” to delete.

Thing

Creates or updates a “Thing.” If the Thing ID is provided, it attempts to update it; otherwise, it creates a new “Thing.”
Parameters:
Thing: (Optional) The URL or ID of the existing “Thing” to update.
Name: The name of the “Thing.”
Handle: A slug or handle for the “Thing.”
Description: A description for the “Thing.”
Key and Value: Vararg parameters to add metadata key-value pairs.

SyncThings

Retrieves all “Things” from WordPress. Automatically paginates if there are multiple pages of results.
Schema:
ThingId: Unique identifier for each “Thing.”
Name: The name of the “Thing.”
Status: Publication status (e.g., “publish”).
Description: Description text.
Additional fields such as Meta, Collections, Tags, etc.

Helper Functions

The pack includes helper functions to manage “Things” and streamline API interactions:
extractRefId: Retrieves a specified link (e.g., “self”) from the response body.
handleError: Custom error handler that formats error messages based on WordPress’s standard REST API response structure.
buildMetaObject: Converts an array of alternating keys and values into a metadata object.
buildThingPayload: Constructs a payload object for creating or updating a “Thing.”
constructThingUrl: Builds a URL for a WordPress “Thing” item.
extractThingInfo: Extracts type and item ID from a WordPress “Thing” URL.
formatThings: Formats an array of “Things” into a simplified structure for easy reading and usage.

Usage Example

To create a “Thing,” you can use the CreateThing formula with a name, handle, description, and optional metadata fields.
For example:
CreateThing("My Thing", "my-thing-handle", “This is a description", “metaKey1", "metaValue1", "metaKey2", “metaValue2");

This will create a new “Thing” with the specified fields in WordPress.

Error Handling

If an error occurs, the handleError function generates a user-friendly message based on the WordPress REST API error format. Common errors, such as invalid parameters, are displayed with relevant details.

Dependencies

Coda Pack SDK : Required for creating and managing the pack.

Outro

This pack enables seamless management of custom “Things” in WordPress, providing a powerful toolkit for content and metadata control. For further customization, the helper functions can be adapted to fit more complex workflows.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.