0

I like to send specific data from a post to my JSON response in Wordpress. I like to send the output of the variable "hatus" to the JSON response, but it does not seem to work.

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

And here is the code I am trying to send it to:

<?php

// Export API Data to JSON, another method
add_action('publish_post', function ($ID, $post) {

    $wp_uri = get_site_url();
    $customApiEndpoint = '/wp-json/wp/v2/posts'; // or your custom 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 under the wp root installation, can be customized to any folder

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

}, 10, 2);
?>

What am I doing wrong here? The only thing I want is to show that 'hatus' variable outcome in my JSON response. Can you help?

2
  • What is $hatus. You’re not defining it. I also don’t understand what you mean by “send the output of the variable to the JSON response”? Commented Oct 24, 2021 at 13:37
  • Hatus is a string. I have updated my question. What I mean is that I want to show that string in my JSON response.
    – Johan
    Commented Oct 24, 2021 at 13:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.