While saving the Post or whenever you tried to paste the AdSense ads code then your WordPress shows you the following message: “Updating failed. The response is not a valid JSON response”. This problem may persist because of many factors and I will be helping you to solve this error of Invalid Json Response.
The Invalid JSON Response error may occur in WordPress because of any of the following reasons:
- Plugin Conflict
- Invalid Code
- Invalid JavaScript and HTML
- Database Error
- PHP version conflict
Solve AdSense Code Snippet Causing JSON error?
So whenever you tried to paste the HTML code of AdSense Ads, the following error is shown by WordPress “Updating failed invalid JSON response“. Probably you are using WordPress Block editor and experiencing the Invalid JSON error.
This problem may be solved only if you create a Shortcode for the AdSense code snippet, rather than directly pasting the AdSense codes in your Blog post.
Note– If you don’t want to write codes and play with your main website files, then you can do this by using an amazing simple plugin, Scroll down to read more.
The Invalid JSON error in WordPress occurs only if WordPress is not able to interpret the given code correctly. The Adsense code snippet for Adsense ads is a pure HTML code, but sometimes the Block Editor of WordPress does not recognize it as a pure HTML and hence causing the issue.
In order to solve the “Google AdSense code Invalid JSON error“, just follow the following steps to create a shortcode for Adsense HTML code:
- Use the given code to make a shortcode key for the “functions.php file of WordPress”:
function add_adsense_codes_shortcode() {
$string .= ‘your adsense code here’;
return $string;
}
add_shortcode(‘show_ad_here’, ‘add_adsense_codes_shortcode’);
- Just copy the Adsense code and paste that code in the place of ‘your AdSense code here’.
- Now open your WordPress Theme File Editor, and now select your Child theme or Parent theme, and then select “functions.php”, scroll down and select the new line and paste your created code as shown above.
- Now whenever you want to place your Google Adsense Ads, just paste [show_ad_here] and it will start showing ads on your posts and pages.
Note– [show_ad_here] is your shortcode as in the above-given code, you can see that we have declared the shortcode as [show_ad_here]. In fact, you can change this line and can give any shortcode name of your own choice like ‘advertisement1’, and then use the shortcode as [advertisement].
For all your Adsense codes, you have to create shortcodes in the fucntions.php file with the same code but keep in mind to change this line “adsense_codes_shortcode”, you cannot use this shortcode declaration multiple times, but have to change its name at two places in the above-given code. You may give it any name like “adsense_codes_shortcode1”.
Solve AdSense code snippet JSON error with a Plugin
- Open WordPress and click on “Plugins”, and then click on “Add new”.
- Now in the search box, search for the “Ad inserter” plugin.
- Install and Activate the Ad Inserter plugin.
- Now in Ad Inserter plugins, you will get 16 tabs to create a shortcode for 16 advertisement codes of AdSense. Just have a look at below image:
With this amazing plugin, you can automate your AdSense ads to be automatically placed on your posts and pages, even you can use this plugin to show AdSense ads in the footer, header, comment section, and much more.
With the Ad Inserter plugin, you can decide after which paragraph and line the ads should be placed. With this plugin, you can do the Ads placement task manually, or automatically. If you want to create shortcodes with this plugin and use the shortcodes manually then you can select the manual button and it will give you the shortcodes as shown below:
How to fix WordPress Invalid JSON response?
WordPress Invalid JSON response may be triggered by any of the following reasons, so you may try the below steps which may ultimately solve the JSON error which is occurring while you are trying to save your WordPress posts.
- Purge WordPress cache
- Update WordPress if not updated
- Update plugins if any of the plugins is not updated
- Try to disable all the plugins one by one and see if the problem still persists
- If an error occurs in the WordPress block editor then try using the Classic editor.
- Make sure that there is no HTML or JavaScript code directly pasted into your post.
- Try to upgrade the PHP version of your site (carefully do this otherwise it can make your site permanently down, and you may also take a backup).
- If possible completely delete the WordPress database from Cpanel and re-install WordPress.