最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest api - Accessing WordPress data via JSON gives HTML data for certain keys

programmeradmin2浏览0评论

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>&nbsp;</p>\r\n\r\n<p>The next class for &#8220;MICROVITA&#8221; will be on &#8220;Aug 07th, 2020 at 8:00 PM IST / 10:30 AM EST on &#8220;MicroVita Science(माइक्रोवाइटा विषय समापन )&#8221; 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:

  1. which WordPress API could give me a list of all the articles as JSON?
  2. I found a particular API doing the above same task as point (1.). is it a standard one?
  3. 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>&nbsp;</p>\r\n\r\n<p>The next class for &#8220;MICROVITA&#8221; will be on &#8220;Aug 07th, 2020 at 8:00 PM IST / 10:30 AM EST on &#8220;MicroVita Science(माइक्रोवाइटा विषय समापन )&#8221; 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:

  1. which WordPress API could give me a list of all the articles as JSON?
  2. I found a particular API doing the above same task as point (1.). is it a standard one?
  3. Is it a standard behavior of WordPress APIs to have HTML content directly inside their JSON key-value pairs?
Share Improve this question edited Aug 16, 2020 at 20:43 ansh sachdeva asked Aug 8, 2020 at 21:37 ansh sachdevaansh sachdeva 1231 silver badge6 bronze badges 3
  • 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
Add a comment  | 

2 Answers 2

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

  1. 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

  1. 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.

发布评论

评论列表(0)

  1. 暂无评论