I am creating an Android app for a client whose site is in WordPress. I want to access the posts, but I don't know which API to hit. One of the APIs I found after some searching is https://www.<mysite>
/wp-json/wp/v2/posts. This gives a ton of key value pairs, So I was tying to access this site via this URL. But for a particular content
key, this is giving the following output:
"content": {
"rendered": "<p> </p>\r\n\r\n<p>The next class for “MICROVITA” will be on “Aug 07th, 2020 at 8:00 PM IST / 10:30 AM EST on “MicroVita Science(माइक्रोवाइटा विषय समापन )” by Ac.Vimalananda Avt. </p>\r\n\r\n\r\n\r\n\n<div class=\"dpn-zvc-shortcode-op-wrapper\">\n\t <table class=\"vczapi-shortcode-meeting-table\">\n <tr class=\"vczapi-shortcode-meeting-table--row1\">\n <td>Meeting ID</td>\n <td>000000000</td>\n </tr>\n <tr class=\"vczapi-shortcode-meeting-table--row2\">..."
}
I am not sure how to retrieve the important information from such HTML content. Maybe that's a question better suited for other stack communities, but what I want to know is that if it is the expected behavior from a WordPress site API? Or is there some other API or plugin which might give me a better response?
To summarize:
- which WordPress API could give me a list of all the articles as JSON?
- I found a particular API doing the above same task as point (1.). is it a standard one?
- Is it a standard behavior of WordPress APIs to have HTML content directly inside their JSON key-value pairs?
I am creating an Android app for a client whose site is in WordPress. I want to access the posts, but I don't know which API to hit. One of the APIs I found after some searching is https://www.<mysite>
/wp-json/wp/v2/posts. This gives a ton of key value pairs, So I was tying to access this site via this URL. But for a particular content
key, this is giving the following output:
"content": {
"rendered": "<p> </p>\r\n\r\n<p>The next class for “MICROVITA” will be on “Aug 07th, 2020 at 8:00 PM IST / 10:30 AM EST on “MicroVita Science(माइक्रोवाइटा विषय समापन )” by Ac.Vimalananda Avt. </p>\r\n\r\n\r\n\r\n\n<div class=\"dpn-zvc-shortcode-op-wrapper\">\n\t <table class=\"vczapi-shortcode-meeting-table\">\n <tr class=\"vczapi-shortcode-meeting-table--row1\">\n <td>Meeting ID</td>\n <td>000000000</td>\n </tr>\n <tr class=\"vczapi-shortcode-meeting-table--row2\">..."
}
I am not sure how to retrieve the important information from such HTML content. Maybe that's a question better suited for other stack communities, but what I want to know is that if it is the expected behavior from a WordPress site API? Or is there some other API or plugin which might give me a better response?
To summarize:
- which WordPress API could give me a list of all the articles as JSON?
- I found a particular API doing the above same task as point (1.). is it a standard one?
- Is it a standard behavior of WordPress APIs to have HTML content directly inside their JSON key-value pairs?
- 1 What are you trying to retrieve? You didn't mention what the data you needed was in your question, I'm not sure what the problem is or what the question is. Are you asking how to acquire a particular piece of data? If so which? Or are you asking what that key is? It's unclear what your question is, use the edit link to add context and information – Tom J Nowell ♦ Commented Aug 9, 2020 at 0:04
- @TomJNowell thankyou for letting me know. I have updated the question with more relevant sources and data. kindly check and let me know if its clear now – ansh sachdeva Commented Aug 9, 2020 at 10:00
- Thanks for the update, but you still haven't shared what you're trying to retrieve, just that it's somewhere in the HTML content. What is the information you need? It is not clear or obvious. – Tom J Nowell ♦ Commented Aug 9, 2020 at 14:49
2 Answers
Reset to default 1
- which wordpress api could give me a list of all the articles as json?
You're already using the right API, just remember it's paginated so you need to request the second/third page/etc in a separate request. It'll pass how many apges there are in the http header
- I found a particular api doing the above same task as point (1.). is it a standard one?
You list posts via the REST API by querying wp-json/wp/v2/posts
. Other endpoints exist for other post types such as pages
etc
- Is it a standard behavior of wordpress apis to have html content directly inside their json key-value pairs?
Yes, if you wanted to display the post you would need the post content. Otherwise how would you display the post?
I think what we're seeing is a misunderstanding. You never stated what it was that you wanted, just that it's somewhere in the content HTML. So I think this is what has happened:
- The post uses a shortcode to display data taken from elsewhere
- Your app is pulling in the post via the REST API
- You're looking at the shortcodes output and wondering how to extract the data in your Android app.
You could parse the HTML using an android library of sorts, assuming it's consistent, or, you could look at where the shortcode gets its information from and access it directly.
Sadly, you never actually said what the data you wanted was, and we don't know what the shortcode that generates that output is. Without access to the implementation of the shortcode it's not possible to say how to expose the information. A shortcode is a PHP function that's registered under a name, and swaps a string such as [shortcode]
with HTML tags. That PHP function might draw the data from post meta, or it might access a custom database table, or even make a HTTP request to a remote API, so no generic solution is possible.
dpn-zvc-shortcode-op-wrapper
gives us a clue as to what the shortcode might be, but you would need to look at how the shortcode works to know that, and you didn't mentiion the shortcodes name. A quick google shows that HTML class appears in a support request for a Zoom plugin, so you would need to go via their support route as 3rd party plugin support is offtopic here.
This is expected WordPress behavior. "content" data is WordPress post content data and stored with all the HTML tags that where added in the post.
I'm not sure what pieces of information you are trying to retrieve, but I think the HTML tags could work to your advantage.
If you were to retrieve the meeting ids then you could filter all the "tr" elements and always pick the second "td" that is inside.
<table class=\"vczapi-shortcode-meeting-table\">
<tr class=\"vczapi-shortcode-meeting-table--row1\">
<td>Meeting ID</td>
<td>000000000</td>
</tr>
</table>
It looks like a WordPress plugin is adding this data inside the post content via a shortcode. If you are lucky, all that data is stored separately in the database in a structured way. If so, you might be able to expose that data to the WordPress API and then access it directly instead of filtering through HTML tags.