John Davidson

php - WordPress/WooCommerce - Remove html data prepended to a returned json data

0 comments
Message:


I don't have access to my customers' PC to check what's happening there. But I have a WordPress plugin that is installed there. The problem is, it returned an extra <style>...<style> prepended to json data.


This problem is only happening in one of my customer's system and not the others. It seems there is a hook that prepends some styles to some products (I said some, because some other products don't return that extra <style>...</style>) and when it wants to return the json data, it returns the <style>...</style> too like so:


<style type="text/css">
.product {
...
}
...
...
</style>
[{"version":"1.0.0","data":"..."}]

The data are being fetched in an array like this:


$products = array(
'id' => $product_id,
'regular_price' => $product->get_regular_price(),
'sale_price' => $product->get_sale_price(),
);

exit(json_encode($products));

But it seems the problem is not from these parameters because it doesn't return that extra style when I get only these parameters. I think it returns that extra style when I use one of these functions:


wp_get_post_terms, $product_variations->get_available_variations()


aren't they the issues?


So what I've done is removing anything before returning the json data like so:


ob_start(function () { return ''; });
@ob_end_clean();
http_response_code(200);
exit(json_encode($data));

I have also set the content-type and accept to application/json.


How can I remove those extra data?



My Answer:
html data prepended to the returned JSON data. This can be caused by a few different things, such as a plugin conflict, a theme issue, or a problem with the server configuration.

To troubleshoot this issue, you can try the following steps:

1. Disable all other plugins except for the one causing the issue and see if the problem persists. If it doesn't, then one of the other plugins is likely causing the conflict.

2. Switch to a default WordPress theme (such as Twenty Twenty) and see if the issue still occurs. If it doesn't, then the problem is likely with the theme you were using.

3. Check the server configuration to ensure that it is set up correctly for running WordPress/WooCommerce. You may need to contact your hosting provider for assistance with this.

4. If none of the above steps resolve the issue, you may need to reach out to the plugin developer for further assistance. They may be able to provide more specific guidance on how to troubleshoot and fix the issue.

Overall, it's important to carefully investigate the possible causes of the extra HTML data being prepended to the JSON data and take steps to address the issue to ensure a smooth user experience for your customers.

Rate this post

5 of 5 based on 2568 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us