Modify category listing API response

I have an existing site that uses a WordPress backend for publishing along with a custom Rails front end that displays the WP content (among other things).

One of the things I’m doing with the WordPress API is modifying the API responses for posts in order to add additional data and strip out the attributes I don’t need on the front end. This is working great.

Now I’m trying to do something similar for categories. I’m building a page that will show WP categories and I’d like to modify the API response to include a category image URL (with category image functionality provided by the WPCustom Category Image plugin).

I found the existence of rest_prepare_taxonomy but it doesn’t seem to behave the same way that rest_prepare_post does.

This is how I’m modifying my post queries (where modify_api_post_response() is the function I wrote to modify the response):

add_filter(‘rest_prepare_post’, ‘modify_api_post_response’, 12, 3);

Doing the same with rest_prepare_taxonomy and another custom function has no effect on the response data.

add_filter(‘rest_prepare_taxonomy’, ‘modify_api_taxonomy_response’);

I’ve tried a variety of priority values with the same result (ie, no change in what I get back from WP).

Any thoughts on what I’m doing wrong?

Read more here:: Modify category listing API response

Leave a Reply

Your email address will not be published. Required fields are marked *