Prevent plugin from intruding on wp-json posts api

We use a plugin called Recent Posts Widget Extended (RPWE) to nicely format categorized lists of posts on our company’s

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: 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!!

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/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!!

Share Improve this question asked Mar 22, 2019 at 16:35 dizzwavedizzwave 1033 bronze badges 3
  • 1 Sounds like a bug with the RPWE plugin - have you tried contacting the plugin author? – birgire Commented Mar 22, 2019 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 Grossherr Commented Mar 22, 2019 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 Commented Mar 22, 2019 at 17:28
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745664982a4639068.html

相关推荐

  • Prevent plugin from intruding on wp-json posts api

    We use a plugin called Recent Posts Widget Extended (RPWE) to nicely format categorized lists of posts on our company’s

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信