Trick for wordpress developers | Web Dance Development
Trick for wordpress developers

Trick for wordpress developers

Published: 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.

New web is waiting for you

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