Resolving “Error: invalid nonce” – How to Fix CSRF Nonce Issues

If you encounter the error message “Error: invalid nonce. Please reload the page and try again. If the problem persists, please contact our support team.” while using Donation Platform for WooCommerce, it typically indicates an issue with nonce verification. Nonces are security tokens used to prevent CSRF (Cross-Site Request Forgery) attacks and ensure the integrity of requests made to the server. The following tips not only address this issue for Donation Platform for WooCommerce but are also applicable to other CSRF nonce issues you may encounter in various contexts, where the error message might be different.

To avoid nonce verification issues, it is recommended to disable caching on pages that include a donation form.

Understanding the CSRF Nonce Issue

In WordPress, nonces have a default lifetime of one day. When caching plugins are enabled, they may cache HTML pages for longer durations, causing nonces to expire before users complete their actions. As a result, users may encounter the “invalid nonce” error when attempting to perform tasks such as making a donation, submitting a form, or any other nonce-protected action.

Troubleshooting Steps

1. Use a Shorter Cache Time

Configure your caching plugin to use a shorter cache time, ideally around 4 hours. This adjustment prevents nonces from expiring too quickly and helps avoid nonce-related errors during the donation process.

2. Disable Cache for Affected Pages

If the “invalid nonce” error occurs on specific pages, consider disabling caching for those pages. Most caching plugins offer options to exclude certain URLs or pages from caching. By excluding donation-related pages from caching, you ensure that nonces remain valid and donations process smoothly.

3. Consider a Different Caching Plugin

If issues persist despite adjusting cache settings, try using a different caching plugin. Not all caching plugins behave the same way, and switching to one that is more compatible with WooCommerce and Donation Platform for WooCommerce may resolve nonce verification problems.

4. Use Cache Purging

Implement cache purging options provided by your caching plugin. This allows you to manually or automatically clear the cache when specific events occur, such as product updates or new donations. By keeping the cache up-to-date, nonce issues are less likely to occur.

5. Disable Caching for Logged In Users

Nonces are specific per user. If you have activated caching for logged in users, deactivate the cache for users who are currently logged in.

6. Disable Nonce Verification for the Add to Cart Endpoint

If you continue to experience nonce-related issues, you can disable nonce verification by using the wcdp_skip_nonce_validation filter. Ensure that you have at least version 1.3.4 installed before applying this change. You can use the free Code Snippets plugin to add this custom code:

add_filter('wcdp_skip_nonce_validation', '__return_true');

⚠️ Note: Disable nonce verification for the Donation Platform for WooCommerce cart endpoint only if absolutely necessary, as it can impact your site’s security. However, the risk is generally low, as tricking users into adding items to their cart is typically considered a minor concern.