Support

Account

Home Forums General Issues handle JSON response for shortcode

Solving

handle JSON response for shortcode

  • Hi,

    I am here for specific request to handle REST API JSON response having shortcode like below.

    "acf": {
                "components": false,
                "shortcode": "[xyz-shortcode slug=\"utoo\"]"
            },

    Now I am able to handle short code data using below code.

    add_shortcode('xyz-shortcode', array($this,'getXYZBlock')); 
    function getXYZBlock($attr)
    {
      return 'TEST DATA PRINT';
    }

    It will result

    "acf": {
                "components": false,
                "shortcode": "TEST DATA PRINT"
            },

    BUT I need to return json data into that function like below where I an having difficulty.

    add_shortcode('xyz-shortcode', array($this,'getXYZBlock')); 
    function getXYZBlock($attr)
    {
      $array=array();
      $array['code']='ok';
     return $array;
    }

    SO I need output like

    "acf": {
                "components": false,
                "shortcode": { "code" : "ok" }
            },

    How can I do that can any one help me.

    Thank You for your support.

  • 
    return json_encode($array);
    
  • Thank You For response.

    No it will not solved my problem. I have already try that json_encode($array) but it will return like

    "acf": {
                "components": false,
                "shortcode": "{\"code\":\"ok\"}"
            },

    but I need output like

    "acf": {
                "components": false,
                "shortcode": { "code" : "ok" }
            },

    Thanks.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘handle JSON response for shortcode’ is closed to new replies.