I am trying to create a REST API
which generate json
data for wordpress
posts. The code below is in functions.php file:
add_action('wp_enqueue_scripts', 'create_json_data', 0);
function create_json_data(){
ob_start();
if (isset($_GET['getjson']) && $_GET['getjson'] == true) {
if (have_posts()) {
$data = array();
while (have_posts()) {
the_post();
$new_data = array(
"title" => get_the_title(),
"content" => get_the_excerpt(),
"image" => get_the_post_thumbnail_url(),
"hyperlink" => get_the_permalink()
);
array_push($data, $new_data);
}
}
header('Content-type: application/json');
echo json_encode($data);
exit();
}
ob_end_flush();
}
It runs very well in newly wordpress
installation. But when I implement it in my online website it gives me error
Warning: Cannot modify header information - headers already sent by (output started at /home1/public_html/blogsite/wp-content/themes/webtheme/header.php:10) in /home1/public_html/website-theme/wp-content/themes/webtheme/functions.php on line 159
at line 10 in header.php
is wp_head()
I don't know how to get ride of this error to generate json
data only.