Hook Into the_content Filter For JSON API Only [closed]

I’m trying to figure out a way to remove the shortcodes from my JSON output which I’m using the WordPress JSON API for.

I need to remove the shortcodes from the content before do_shortcode is applied which will cause the shortcodes to render. The reason being that they will break in my mobile app (they reference various js etc.. not in the app)

I’ve found a Strip Shortcodes function which I can hook into the_content filter as these filters are applied by the plugin before it encodes it to JSON. But how can I only do this when I am working with a request for json information as I don’t want to remove the shortcodes on the general site.

I know I can check for things like is_single() or is_home() but I can’t see anything similar which would tell me if it’s for a JSON output.

Is there anyway I can tell if it is or not so I can choose when to use the strip shortcodes function?

One thing I thought about was examining the url to see if json is in it, but I was hoping there could be something more robust?

2 Answers
2

Your problem is not as much filtering content, as content assuming too much about context and doing things that content shouldn’t really. You don’t read the book where text decides to color itself, it’s decided for it.

I would suggest to consider decoupling content from active context-specific functionality. You can still use shortcodes, but instead of them actively spitting out JavaScript to a page there should be more flexible and do things like setting some “flags” for JS to be output in footer and so on.

Leave a Comment