Site icon Hip-Hop Website Design and Development

Sending easy variable on single web page to WP JSON

I prefer to ship particular knowledge from a publish to my JSON response in WordPress. I prefer to ship the output of the variable "hatus" to the JSON response, but it surely doesn’t appear to work.

    <?php 
$hatus = "some information here";
echo wp_kses_post($hatus); ?>

And right here is the code I’m making an attempt to ship it to:

<?php

// Export API Information to JSON, one other methodology
add_action('publish_post', operate ($ID, $publish) {

    $wp_uri = get_site_url();
    $customApiEndpoint = '/wp-json/wp/v2/posts'; // or your customized endpoint

    $url = $wp_uri . $customApiEndpoint; // outputs https://your-site.com/wp-json/wp/v2/posts

    $response = wp_remote_get($url);
    $responseData = json_encode($hatus); // saved below the wp root set up, will be custom-made to any folder

    file_put_contents('your_api_data_backup.json', $responseData);

}, 10, 2);
?>

What am I doing unsuitable right here? The one factor I would like is to indicate that ‘hatus’ variable consequence in my JSON response. Are you able to assist?