WordPress API variable respons

WordPress API variable respons

  

Hi All,


I've been running into the following problem, and hop someone can help. 

We have a laert system for one of the sites that gets it's content from WordPress. In wordpress we created a separate entity (this was programmed into WP for us by an external party) for these alerts. Under this entitiy we post items that may contain a "news item". This however is optional. We generated the relevant structs with a completely filled alert. this works like a charm. However. Here you see the JSON that is generated from an entry WITH and one WITHOUT the attached news item:

..........

WITH:

"em_application_tags": [
      340
    ],
    "acf": {
      "em_news_item": {
        "ID": 3802,
        "post_author": "20",
        "post_date": "2016-09-16 10:24:11",
        "post_date_gmt": "2016-09-16 10:24:11",
        "post_content": ".....CONTENT",
        "post_title": "....TITLE",
        "post_excerpt": "",
        "post_status": "publish",
        "comment_status": "closed",
        "ping_status": "closed",
        "post_password": "",
        "post_name": "....NAME",
        "to_ping": "",
        "pinged": "",
        "post_modified": "2016-09-16 10:28:54",
        "post_modified_gmt": "2016-09-16 10:28:54",
        "post_content_filtered": "",
        "post_parent": 0,
        "guid": "LINK",
        "menu_order": 0,
        "post_type": "em-news",
        "post_mime_type": "",
        "comment_count": "0",
        "filter": "raw"
      }
    },
    "_links": {


WITHOUT:

"em_application_tags": [
      340
    ],
    "acf": {
      "em_news_item": false
    },
    "_links": {


As you can see, the json has one fundemental difference... An object is changed to an attribtue.

If a news item exists this changes the look and feel of the alert (a "read more" link is added)

We have 3 solutions for this and i am desparately searching for a way NOT tot use option 1):

1) Make a dummy news item and hard-code this into OS (if (title="dummy", DO NOTHING, DO SOMETHING)) . Instruct users to always fill in the news item, if needed with dummy

2) Contact the external party to change this code and deliver a correct object (may take too long)

3) Figure out how to create a dynamic structure for this AND how to get OS to use this...


Any hints appreciated...


Greetings,

Alexander


Solution

Hi Alexander, 


I'm assuming you are using a REST API to get this content,  right?

If so, add a OnAfterResponse callback to it and use a text regex in the ResponseText to modified it to a compatible format. 

A simple regex like """em_news_item""\s*:\s*false" should do the trick. You can either choose to remove it or just replace the false by {}.


Regards, 

João Rosado 

Solution

Great one João. SHould have thought of that! 


Regards,
Alexander