Trick for wordpress developers

Trick for wordpress developers

Published On: January 27, 2025

How to Add Custom Fields to REST API

If you’re a WordPress developer using Custom Post Type (CPT), you’ve probably encountered the need to extend the REST API with additional data. The easiest way to do this is using register_rest_field(). In this article, you’ll learn how to quickly and easily add custom fields to the WordPress REST API.


πŸ’‘ How to Add Custom Fields to WordPress REST API

Let’s say you have a CPT “apartments” and want to add the field “apartment_price” to the REST API response. You can do this as follows:

add_action('rest_api_init', function () {
    register_rest_field('apartments', 'apartment_price', array(
        'get_callback'    => function($post) {
            return get_post_meta($post['id'], 'apartment_price', true);
        },
        'update_callback' => null, // Enable if you want updates via API
        'schema'          => array(
            'description' => 'Apartment price',
            'type'        => 'string',
            'context'     => array('view', 'edit')
        ),
    ));
});

βœ… Why use register_rest_field()?

βœ”οΈ A simpler way to add custom fields.
βœ”οΈ Automatically applies to REST API responses.
βœ”οΈ Modular approach – easily add multiple fields without additional filters.
βœ”οΈ Can be used for integration with Gutenberg blocks, WP admin extensions, or headless WordPress solutions.


πŸ”„ WP Admin Extensions: How to Use REST API in the Admin Panel

If you want WordPress Admin to display “apartment_price” in the post list, use the following code:

// Add a column to WP Admin
function add_price_column($columns) {
    $columns['apartment_price'] = 'Apartment Price';
    return $columns;
}
add_filter('manage_apartments_posts_columns', 'add_price_column');

// Populate the column with data
function show_price_column_data($column, $post_id) {
    if ($column === 'apartment_price') {
        echo get_post_meta($post_id, 'apartment_price', true) . ' €';
    }
}
add_action('manage_apartments_posts_custom_column', 'show_price_column_data', 10, 2);

This trick makes it easier to enter and review data directly in WordPress Admin.


🌐 Headless WordPress: REST API in Frontend

Headless WordPress means using WordPress only as a backend, while the frontend can be built with React, Vue.js, Next.js, or another modern technology.

If a frontend application wants to retrieve apartment data via REST API, it can use the following JavaScript code:

fetch('https://example.com/wp-json/wp/v2/apartments')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

βœ… Benefits of Headless WordPress

βœ”οΈ Faster frontend (SPA or static site generators like Next.js).
βœ”οΈ Flexibility – WordPress as a CMS, frontend in any modern technology.
βœ”οΈ Integration with mobile applications.


πŸ”Ž Conclusion

Whether you’re extending WordPress Admin, working on REST API integrations, or developing headless WordPress solutions, register_rest_field() is a powerful tool that will make your job easier.

Contact

We turn your website visitors into euros. Contact us and increase your profits today!