How to Extend WordPress Rest Api with Custom Fields and Metadata

The WordPress REST API is a powerful tool that allows developers to interact with site data remotely. By default, it exposes core data such as posts, pages, and media. However, to make the API more useful, especially for custom themes and plugins, you often need to extend it with custom fields and metadata.

Understanding Custom Fields and Metadata

Custom fields are additional pieces of information attached to posts, pages, or custom post types. Metadata refers to data about data, often stored in custom database tables or as post meta. Extending the REST API involves making this custom data accessible via API endpoints.

Registering Custom Fields for REST API

WordPress provides the register_rest_field function to add custom fields to REST API responses. Here is a basic example:

add_action( 'rest_api_init', function () {
    register_rest_field( 'post', 'custom_field', [
        'get_callback' => function ( $post ) {
            return get_post_meta( $post['id'], 'custom_field_key', true );
        },
        'schema' => [
            'type' => 'string',
            'description' => 'Custom field data',
        ],
    ] );
} );

This code adds a custom field called custom_field to the post endpoint. When you fetch posts via the REST API, this field will be included in the response.

Adding Custom Metadata

For custom metadata stored in custom tables or other locations, you can create custom REST endpoints or extend existing ones. Using register_rest_route, you can define new endpoints that return your custom data.

Creating a Custom Endpoint

Here’s an example of registering a custom REST API endpoint:

add_action( 'rest_api_init', function () {
    register_rest_route( 'custom/v1', '/metadata/(?P\d+)', [
        'methods' => 'GET',
        'callback' => 'get_custom_metadata',
    ] );
} );

function get_custom_metadata( $request ) {
    $post_id = $request->get_param( 'id' );
    // Fetch custom metadata from your custom source
    $metadata = fetch_custom_metadata( $post_id );
    return rest_ensure_response( $metadata );
}

function fetch_custom_metadata( $post_id ) {
    // Replace with actual data retrieval logic
    return [
        'meta_key_1' => 'value1',
        'meta_key_2' => 'value2',
    ];
}

This approach allows you to expose any custom data via REST API endpoints tailored to your needs.

Best Practices and Tips

  • Always sanitize and validate data before exposing it through the API.
  • Use schema definitions to improve API documentation and client integration.
  • Consider using existing plugins like Advanced Custom Fields (ACF) for easier management of custom fields.
  • Cache custom API responses where possible to improve performance.

Extending the WordPress REST API with custom fields and metadata enhances your site’s flexibility and allows for richer integrations with external applications. With these techniques, you can tailor the API to meet your specific project requirements.