Repository navigation
Model Generator
dart run wordpress_client:generate reads the JSON schema WordPress publishes for a route and writes Dart model classes for it, ready for client.resource<T>.
dart run wordpress_client:generate \
--site https://example.com \
--route wp/v2/events \
--out lib/models/event.dartimport 'models/event.dart';
final events = client.resource<Event>('events', decoder: Event.fromJson);
final upcoming = (await events.list()).dataOrThrow();
print(upcoming.first.title?.rendered);| Option | |
|---|---|
--site <url> |
Site URL (https://example.com) or REST root (.../wp-json) |
--route <route> |
Route to read, e.g. wp/v2/events. Repeat for several; all classes go into one file |
--schema <file> |
Read a saved OPTIONS answer or bare schema instead of a site |
--name <Class> |
Class name, only with a single route or schema. Default: from the schema title (event gives Event) |
--out <file> |
Output file. Default: standard output |
--user, --app-password
|
For routes that need authentication. WP_USERNAME and WP_APP_PASSWORD are read from the environment too |
--help |
All options |
Prefer the environment variables over --app-password, so the password does not end up in your shell history.
For each route, a class that:
- implements
ISelfRepresentative, soself,metaValue,acfValueand the other model helpers work; - has one nullable field per schema property, since WordPress leaves fields out depending on
contextand_fields; - converts values leniently (
'12'asint,'1'asbool, ISO strings asDateTime); - has nested classes for object properties (
EventTitlefortitle), lists of the right type, andMap<String, dynamic>for free-form objects; - has
fromJsonandtoJson(only the fields that are set); - carries the schema descriptions as doc comments and marks read-only fields.
Property names become camelCase Dart names; Dart keywords get a Value suffix (default becomes defaultValue) and clashes get a number.
Save the schema once and generate from the file, for example in CI:
curl -X OPTIONS https://example.com/wp-json/wp/v2/events > event.schema.json
dart run wordpress_client:generate --schema event.schema.json --out lib/models/event.dartThe output file starts with a "Generated" comment and is overwritten on every run. Keep custom code in an extension or a wrapper, not in the generated file. Re-run the generator after adding fields or plugins on the site.
Getting started
- Usage
- Bootstrap and Configuration
- Authorization
- Browser Sign-In
- WordPress.com and Jetpack
- Multisite
- Supported REST Methods
Requests and data
- Sending Requests
- Error Handling and Responses
- Fluent Queries
- Interfaces and Extensions
- Custom Post Types
- Model Generator
- Meta and ACF
- Concurrent Edits
- Batch Requests
- Embed and Fields
- Pagination and Finders
- Parallel Requests
- Streaming and Polling
- Pattern Directory Items
Behaviour
- Retries
- Caching
- Request De-duplication and Concurrency
- Middlewares
- Logging
- Offline Write Queue
- Events and Statistics
Other APIs
Extend and test
Reference