WordPress
Show published posts, pages and categories from your WordPress site, and search them. The connector reads public content and cannot edit your site.
- Asks for
- WordPress website address
- Runs in
- Browser preview, Expo Go and native builds
On this page
What the connector reads
The WordPress connector reads published, public content through your site's REST API: posts, pages and categories, and a text search across your public content. Use it for a news feed or a blog screen inside your app.
It needs no password or API key. It cannot sign in to WordPress, read private content, manage your site or write to it. Your app asks your site for the content directly; Appsanic does not pass the requests on.
What you need
- WordPress website address
- Required. Your public site's address, starting with
https://, without a page path,/wp-admin,/wp-jsonor a trailing slash. If WordPress serves its REST API from a subfolder, keep that folder in the address. In WordPress it is the Site Address under Settings, General.
The site must answer at /wp-json/wp/v2 under that address and allow public reads. If a security plugin blocks the REST API, ask your site's administrator to allow it.
Connectors refuses an http:// address, a port, a query string, a user name or password in the address, and local host names such as .local, .localhost, .internal or .test. WordPress.com sites that need its public API proxy are not supported.
Add your site
- Publish the posts and pages you want in your app.
- Choose Connect in the rail, then Connect WordPress.
- Under Test details, enter the WordPress website address. Add a second site under Live details if you have one.
- Choose Check connection, then Connect.
The check asks your site for one post through Appsanic's server and shows Connection verified when the reply is a valid WordPress post list. What was checked? lists what it covered. It does not test every operation, or whether the browser preview can reach your site. The steps every connector shares, such as Use Live in app, are in Connectors.
What you can request
| Operation | What it reads | Parameters |
|---|---|---|
posts | Published posts | limit, page, query |
post | One published post, by its numeric ID | id |
pages | Published pages | limit, page, query |
categories | Public post categories | limit, page, query |
search | Public content that matches query | query (required), limit, page |
limit: how many items to return, a whole number from 1 to 100. The default is 20.page: which page of results, counting from 1. The default is 1.query: text to search for, up to 300 characters. Forsearchit is required and cannot be blank.id: the post's numeric ID, a positive whole number.
Lists come back as a JSON array and post as one object, both as unknown values. The connector does not pass on WordPress's response headers, so totals and page counts are not available: do not show a page count you cannot know.
Use it in code
Import getConnector from @appsanic/connectors with your connection's id. How to use WordPress, in the connection's settings, shows an example with your id for any operation, and the generated @appsanic/connectors.ts lists it too. There is no availability to check: this client does not need a native build.
import { getConnector } from "@appsanic/connectors";
// Use the id of your own connection, shown under How to use WordPress.
const site = getConnector("wordpress-5d2c81aa");
type Post = Record<string, unknown> & { id: number };
function isPost(value: unknown): value is Post {
return (
typeof value === "object" &&
value !== null &&
"id" in value &&
typeof value.id === "number"
);
}
export async function loadPosts(page = 1): Promise<Post[]> {
const result = await site.request("posts", { limit: 10, page });
if (!Array.isArray(result)) throw new Error("WordPress sent an unexpected reply.");
return result.filter(isPost);
}Replies can contain HTML from your site. Treat it as untrusted: show it as plain text and never inject it as markup. A failed request throws an error with a readable message, for example when the site denies access, so plan for loading, empty and error states.
On a paid plan, Build with Sanic AI in the connection's settings starts a Sanic AI draft that names this connection; finish it with what you want, such as a news feed. Sanic AI reads the connector's operations and limits, never the address you entered. See With Sanic AI.
Where it runs
WordPress requests work in the browser preview, in Expo Go and in a native build. Each time, your app asks your site directly, with the details your app uses (Test or Live).
The browser preview adds one condition: it runs on its own isolated page, so your site must allow requests from other websites (CORS). A site that refuses them can still work on a phone in Expo Go. The connection check cannot tell you which case you have, so open the screen in the preview and then on a phone: see Preview and test on a phone.
Limits and good practice
- Enter the address your site answers on directly. Requests that are redirected fail, so if your site sends visitors on to another address, use that one.
- Ask only for what a screen shows. A request that takes more than 15 seconds fails, and a reply larger than 4 MB is refused.
- Test does not create a separate WordPress site. Use a test site, or your public site if you only read its published content.
