0

We use a plugin called Recent Posts Widget Extended (RPWE) to nicely format categorized lists of posts on our company’s support website (WordPress, naturally). It works great, but more importantly, it'd be too much work to take it out at this point.

I recently started implementing a third-party tool that calls the WP API to search for posts based on a (string) search term. The query looks like this: https://example.com/wp-json/wp/v2/posts?search=asdf And the result is supposed to be a nice json array of posts.

However (and this is the strange part)... Somehow, RPWE is getting in the way and outputting a chunk of CSS into the result, right at the top. Since the caller is expecting json only, it breaks our integration. Why the heck would plugin code be called during a call to the REST engine?

I’ve tried playing around in the RPWE plugin code–I see the exact line where it inserts that CSS, and I added some “if” logic to only output the CSS if a certain shortcode attribute were present. The results are strange: if I call the API directly, right after loading a WP page with RPWE on it, it still fails (as if RPWE remembers being loaded from the UI). (I also tried just taking out the line where it inserts the CSS. That fixes the API issues, but then the styling on our site where we actually use the plugins, goes away.)

Am I missing something obvious? I’m not a WP expert (C# and javascript is my trade), so maybe there’s just some setting I need to know about? Mystery #1 is, why the heck is this plugin code being called at all on a pure REST call?

Many thanks!!

  • 1
    Sounds like a bug with the RPWE plugin - have you tried contacting the plugin author? – birgire Mar 22 at 16:45
  • 1
    For your information, third party plugin question are considered off topic as per community guidelines – see the help center for more information. Additionally, without any specific information, hooks used, code to look at, this is pretty much unanswerable, or forces people to look up the code themselves. With more, complete information, so the question being of good quality, you might even get an answer, but generally this a question that should be asked directly to the plugin developer. – Nicolai Mar 22 at 16:45
  • WP simply executes all plugins when loaded, because it doesn't know if they do something you want. You could, for instance, have a plugin that adds some specific css when there's an API-call. It's the plugin's responsibility to do nothing stupid. – cjbj Mar 22 at 17:28
2

Basically what you would want is to disable the RPWE widget whenever a request is made through the API. I haven't tested it, but this might work:

 add_action ('widgets_init', 'wpse332374_remove_rpwe', 0, 1000) // make sure this is the last action
 function wpse332374_remove_rpwe() {
   if (is_rest()) unregister_widget ('recent_post_widget_extended'); // or whatever the handle is
   }

Note that is_rest() is not a standard WP function. It's a solution proposed for another question about selective functionality for API calls.

  • oh awesome! The is_rest() function you linked me to is just what I needed. I added it directly to the plugin, where it adds the CSS chunk -- now only if is_rest()==false. It works. Thank you! – dizzwave Mar 22 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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