0

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?

1 Answer 1

0

If you are using the API to respond with one or more categories, you are querying for terms and not for taxonomies. So you should not be using WP_REST_Taxonomies_Controller but rather WP_REST_Terms_Controller.

Instead of hooking to the filter rest_prepare_taxonomy, try hooking to rest_prepare_{$this->taxonomy} instead. As you are working with the taxonomy "category" use it as rest_prepare_category:

add_filter( 'rest_prepare_category', 'modify_api_taxonomy_response', 10, 3 );
1
  • Perfect, thank you. Obvious in retrospect, but that had totally escaped me.
    – Kenn
    Commented Mar 6, 2018 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.