2

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>.com/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?
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 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 2

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.

4
  • Yes exactly, I am wondering how to get the data from that html into my android app. And sorry for not stating what i want exactly, its just that I want all of the info that is , as you said "somewhere in the content html. I thought asking a generic question would give me a generic answer to retrieve any kind of info. And about your 3rd point, I was expecting the api to give me a json in the content key too, like "content" :{ "title" :"something", "abc" : "xyz",...} etc. but i guess that's not the case. Commented Aug 9, 2020 at 16:05
  • Also, thank you for taking time to understand my question,I have accepted your response. I guess i have to parse the html somehow to retrieve important info ( btw , do you know of a way to do it without me pulling out my hair :( ? ) Commented Aug 9, 2020 at 16:06
  • Not quite, the fields returned are part of a schema, you can add post meta values via register_meta, but since it's a shortcode we don't know where the shortcode gets its data to generate HTML markup, it's a mystery especially since nobody here knows what the shortcode is. It might get its data from post meta, it might get it from options, or even a custom table, it's unknown to people reading your question. As for extracting information from a HTML string, that's not a WP question but an Android app dev question. Just know that there is no generic answer other than that the Q is too generic
    – Tom J Nowell
    Commented Aug 9, 2020 at 17:00
  • As an aside, context is super useful even if you're asking for the generic case, you shouldn't hide information as it might lead to answers that don't apply to you, or generic answers that don't fit well
    – Tom J Nowell
    Commented Aug 9, 2020 at 17:03
3

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.

1
  • Hey. Your answer helped me a lot in solving my problem, thanks! Commented Aug 12, 2020 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.