开发者

Strip WordPress display (header, footer and everything) to display a json string

开发者 https://www.devze.com 2023-03-03 19:04 出处:网络
I 开发者_如何学编程am trying to have a WordPress plugin returning some custom json data, when a specific parameter is passed through the URL. The problem is that I cannot find a way to prevent it to d

I 开发者_如何学编程am trying to have a WordPress plugin returning some custom json data, when a specific parameter is passed through the URL. The problem is that I cannot find a way to prevent it to display anything. I want to strip any display and only display the json data my function returns.

Ex: "http://mywebsite.com/mypage.php" would return my regular WordPress page.

But "http://mywebsite.com/mypage.php?api=1" would just returns the json data returned from a triggered function (and removing any default display).

Is there any way to do that?

My purpose is to have that plugin returning some json data, and acting like an API, living within my plugin.

Thanks


Make a template called jsonpage.php Use this template for your page

if ($_REQUEST['api']) {
  $data = array();
  header('Content-type: application/json');
  return json_encode($data);
} else {
  // show header, page content, etc.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消