Fixing the 'discard_content' JavaScript Error in XML Sitemap Generator for Google
Content
Users of the 'XML Sitemap Generator for Google' plugin have reported a recurring JavaScript error in their WordPress admin area. This error can cause other custom scripts to malfunction and creates a poor user experience. This guide explains the cause and provides the steps to resolve it.
Understanding the Error
The error message in the browser's console typically appears as:
Uncaught TypeError: Cannot read properties of null (reading 'classList')
Uncaught TypeError: document.getElementById(...) is null
This error occurs because the plugin's JavaScript code is trying to manipulate an HTML element with the ID discard_content. However, recent updates to WordPress or WooCommerce have removed this element from the page. When the code runs, it cannot find the element, resulting in a null error that stops script execution.
Why This Happens
The error originates from code related to the plugin's beta program notification system. The JavaScript is designed to add or remove CSS classes from the notification's dismiss button (discard_content). If this element does not exist on the page—which can happen after core WordPress updates or on certain admin screens—the script fails.
How to Fix the 'discard_content' Error
Solution 1: Update the Plugin (Recommended)
The most straightforward solution is to ensure you are running the latest version of the plugin. The 'XML Sitemap Generator for Google' team has acknowledged this issue in their support threads and has stated that fixes for these JavaScript errors are included in subsequent updates. Always update your plugins to benefit from bug fixes and security patches.
- Navigate to your WordPress Dashboard.
- Go to Plugins → Installed Plugins.
- Find "XML Sitemap Generator for Google" in the list.
- If an update is available, you will see a link. Click Update Now.
- After updating, clear your browser cache and check if the console error is gone.
Solution 2: Apply a Temporary Code Fix (For Developers)
If you cannot update immediately or are experiencing the issue on a current version, a temporary workaround is to add a null check to the plugin's JavaScript code. This modification ensures the code only runs if the discard_content element exists.
Warning: This involves editing plugin files. Any changes you make will be overwritten the next time the plugin is updated. Always create a full backup of your site before proceeding.
- Access your website's files via FTP or your hosting provider's file manager.
- Navigate to the plugin directory:
/wp-content/plugins/google-sitemap-generator/. - You will need to locate the JavaScript file containing the problematic code. Based on error reports, it may be enqueued in a PHP file or within an inline script. Searching the codebase for "discard_content" can help you find it.
- Once located, find the offending lines of code. They will look similar to this:
document.getElementById("discard_content").classList.add("discard_button_outside_settings"); - Modify the code to include a conditional check before execution:
if (document.getElementById('discard_content')) { document.getElementById('discard_content').classList.add('discard_button_outside_settings'); } - Save the file and clear any caching mechanisms on your site (server, plugin, or browser cache).
Solution 3: Disable the Plugin
If the error is causing critical issues and other solutions are not viable, you can temporarily disable the "XML Sitemap Generator for Google" plugin. This will immediately stop the JavaScript error. Remember, this will also disable your XML sitemap functionality until the plugin is reactivated after an update.
Conclusion
The 'discard_content' null error is a known JavaScript conflict that arises from changes in the WordPress environment. The preferred solution is to update the plugin to its latest version. For those who need an immediate fix or are comfortable with code, adding a simple null check can resolve the conflict. As always, maintaining regular backups before making changes is crucial for site stability.
Related Support Threads Support
-
‘discard_content’ does not existhttps://wordpress.org/support/topic/discard_content-does-not-exist/
-
Cookie banners? For what?https://wordpress.org/support/topic/cookie-banners-for-what/
-
wp role accessing the settingshttps://wordpress.org/support/topic/wp-role-accessing-the-settings/
-
Beta testing programhttps://wordpress.org/support/topic/beta-testing-program/
-
Invasive ad (beta programme) causes content shift in the site editorhttps://wordpress.org/support/topic/current-version-breaks-site-editor/
-
XML Sitemaps Cookie consenthttps://wordpress.org/support/topic/xml-sitemaps-cookie-consent/
-
JS Error by ID “discard_content”https://wordpress.org/support/topic/js-error-by-id-discard_content/
-
Cannot dismiss survey prompthttps://wordpress.org/support/topic/cannot-dismiss-survey-prompt/
-
Plugin only available to administratorhttps://wordpress.org/support/topic/plugin-only-available-to-administrator/
-
Data collection/GDPRhttps://wordpress.org/support/topic/data-collection-gdpr/
-
CSS trafic script hackhttps://wordpress.org/support/topic/css-trafic-script-hack/
-
Unwanted Popuphttps://wordpress.org/support/topic/unwanted-popup-3/
-
How to fix Eliminate render-blocking JavaScript and CSS in above-the-fold contehttps://wordpress.org/support/topic/how-to-fix-eliminate-render-blocking-javascript-and-css-in-above-the-fold-conte/
-
CSS Traffic codehttps://wordpress.org/support/topic/css-traffic-code/
-
Cookie “rcksid”https://wordpress.org/support/topic/cookie-rcksid/