robobobobo
robobobobo

Reputation: 759

How To Tell When A JSON Request Is Received In Wordpress?

I am using a JSON API plugin for wordpress to allow me to work with the sites content in a phonegap application I'm building.

However due to the complexity of some of the content on the site (caused by shortcodes outputting graphs, sliders etc..) these aren't suitable to be displayed in the mobile app. I need to remove the shortcodes from the JSON output.

I have found that I can hook into the_content filter in wordpress and use remove_shortcode to take out the necessary shortcodes. But the problem is I can only do this when I access the json url via my browser.

For example, I may use http://example.com?json=1 to return recent posts. If I type this in my url bar I can parse the url, determine that json=1 is there and strip the shortcodes.

However when I am doing an ajax (JSONP) request from my mobile application, it doesn't appear to be able to check the url for the json parameter, thus my shortcodes are not being stripped. I can't even pass in any headers either as they won't make it because of the nature of JSONP requests I believe.

Has anyone got any ideas as to how I can figure out when a JSON request from my mobile application is received, so that I can then remove the shortcodes?

Something like

if(is_json()){
//remove shortcodes
}

And before it's brought up, I have asked this on the Wordpress Stackexchange but to no avail

Update: Here is the code I use for the ajax request from the mobile app

  $.ajax({
                    url: "http://www.example.com/?json=1",
                    dataType: "jsonp",
                    async: true,
                    success: function(result) {
                        app.populate(result)
                    },
                    error: function(request, error) {
                        alert('Network error has occurred please try again!');
                    }
                });

Upvotes: 0

Views: 231

Answers (2)

robobobobo
robobobobo

Reputation: 759

Prompted by one of the comments, I found what I needed in the JSON-API plugin files.

If you look in json-api/models/post.php there's a function set_content_value() which shows where the plugin is pulling in the content. Here you can modify it as needed, in my case I used it to remove certain shortcodes with the Wordpress remove_shortcode() function

Upvotes: 1

Nathan Peixoto
Nathan Peixoto

Reputation: 328

Can't you just use the remove_shortcode function anytime your plugin serves content to a client?

Could you also give us the name / url of your plugin? Maybe a bit of code woudln't hurt either. Would you mind giving use your phonegap application's API request code snippet?

Thanks.

Upvotes: 0

Related Questions