
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!