最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Error : "Updating failed: The response is not a valid JSON response" with custom shortcod

programmeradmin1浏览0评论

I am building a theme into which I have integrated data recovered by an external API.

I chose to create a plugin in which I put all my data processing, it allows me to generate a shortcode with which the user can interact, for example, to change how many items are displayed on the home page.

On that side, everything works, but my problem is with the Gutenberg editor ; when I add my shortcode to the page, and I want to save it, the following error message appears :

Updating failed: The response is not a valid JSON response.

However, the changes are saved correctly.

This message is worrying for users because :

  • At the moment of leaving, the web browser displays a JavaScript alert which says that the modifications will be lost (but this is false)

  • And each time we come back on the page edit, it ask us to recover the old versions of the page

About PHP functions of my plugin, there are some whose purpose is to return HTML and display it. I first thought the problem was coming from here, because we are on the admin side, no need to execute anything, so I tried to put my function calls in a condition that verifies that we are not on an admin page :

if(!is_admin()) {
        myFunctionDisplayHTML();
    }

But I still get this error message. Do you have any idea how to fix this problem ? Thank you in advance for your help !

My WordPress version is 5.6.2, PHP version 7.4.1 and my website runs in local environment.

I am building a theme into which I have integrated data recovered by an external API.

I chose to create a plugin in which I put all my data processing, it allows me to generate a shortcode with which the user can interact, for example, to change how many items are displayed on the home page.

On that side, everything works, but my problem is with the Gutenberg editor ; when I add my shortcode to the page, and I want to save it, the following error message appears :

Updating failed: The response is not a valid JSON response.

However, the changes are saved correctly.

This message is worrying for users because :

  • At the moment of leaving, the web browser displays a JavaScript alert which says that the modifications will be lost (but this is false)

  • And each time we come back on the page edit, it ask us to recover the old versions of the page

About PHP functions of my plugin, there are some whose purpose is to return HTML and display it. I first thought the problem was coming from here, because we are on the admin side, no need to execute anything, so I tried to put my function calls in a condition that verifies that we are not on an admin page :

if(!is_admin()) {
        myFunctionDisplayHTML();
    }

But I still get this error message. Do you have any idea how to fix this problem ? Thank you in advance for your help !

My WordPress version is 5.6.2, PHP version 7.4.1 and my website runs in local environment.

Share Improve this question asked Mar 4, 2021 at 17:36 LoragaLoraga 1011 gold badge1 silver badge10 bronze badges 3
  • I'd start by opening the Network tab of the inspector in your browser and filtering to XHR requests to see if you can isolate the request that is returning invalid JSON. If you can find that, we might be able to help you more. – phatskat Commented Mar 4, 2021 at 18:27
  • Thanks for your help! I did that. The error message appears when this request is processed. Here are screenshots of request headers, request content and here the response json file. I've sent all queries responses to a Json validator, everything seems valid... – Loraga Commented Mar 5, 2021 at 8:50
  • Update : the JSON response is clearer with Chrome network tools than Firefox, sorry ! The full JSON response can be found here, my shortcode print a lot of HTML in JSON response and I don't understand why... Do you have any idea ? – Loraga Commented Mar 5, 2021 at 9:20
Add a comment  | 

1 Answer 1

Reset to default 4

Problem solved, I replaced all function calls that return HTML with :

ob_start();
myFunctionDisplayHTML();  
return ob_get_clean();

And the JSON response is valid.

Thanks @phatskat, you got me on the right way :-) Solution find here

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论