AJAX Not Working For WP-Email, WP-Polls, WP-PostRatings or WP-PostViews?

About 2 weeks ago, I release an update to r WP-Email, WP-Polls, WP-PostRatings and WP-PostViews which added some nonce check and moved the AJAX request to be handled by /wp-admin/admin-ajax.php.

3 common issues that users are facing.

“-1” or “Failed To Verify Referrer”
This means that most likely you are using a caching plugin like W3 Total Cache (W3TC) and the Javascript being minified/combined and cached is outdated. Just empty the JS/CSS cache under the “Minify” section of W3TC in WP-Admin and W3TC will regenerate it again. This will solve the problem.

Password Protected /wp-admin/ Will Not Work
If you are using .htpasswd to protect your /wp-admin/ folder, AJAX request to /wp-admin/admin-ajax.php will not work. This problem is not unique to my plugin. Any WordPress Plugins that uses the WordPress AJAX API will break. As mentioned in this Codex, Hardening WordPress:

Simply securing the wp-admin/ directory might also break some WordPress functionality, such as the AJAX handler at wp-admin/admin-ajax.php

To bypass this, check out this tutorial, Password protecting the wp-admin directory, this tutorial will teach you how to whitelist admin-ajax.php in your /wp-admin/ using .htaccess.

I still hope in the future version of WordPress, they will separate front facing AJAX requests vs backend AJAX requests.

Your WP-Admin Is HTTPS While Your Site Is Not
If your WP-Admin is behind SSL aka HTTPS and you have the following config in your wp-config.php define('FORCE_SSL_ADMIN', true);, the AJAX will fail because https://yoursite.com is different from http://yoursite.com and the browser treat it as different domain.

To solve this issue do the following:
Find:
'ajax_url' => admin_url('admin-ajax.php'),
Replace:
'ajax_url' => admin_url('admin-ajax.php', (is_ssl() ? 'https' : 'http')),

What the code does is basically forcing http to be used when calling admin-ajax.php for AJAX request. Again this is a hack, I am trying to figure a way around it.

I have pushed the code to trunk of all the respective plugins.

1 Star2 Stars3 Stars4 Stars5 Stars (411 votes, average: 4.16 out of 5)

53 thoughts on “AJAX Not Working For WP-Email, WP-Polls, WP-PostRatings or WP-PostViews?”

  1. Follow-up, forgot to provide version information. WordPress 3.4.1, wp-polls. 2.63.

  2. Can you clear your WordPress cache? It seems that your JS cache is still pointing to the old AJAX url which is wp-polls/wp-polls.php instead of wp-admin/admin-ajax.php

  3. Another one to add to the list in general would be if the site was multisite which can cause Ajax to fail. Would appreciated if you could explain a solution if you already have dealt with this before. I think it fails because of the rewrite for each network sites.

  4. Thanks for the quick reply Lester. After you mentioned the cache I did some hunting around and for some reason the ajax call to wp-polls was hardcoded in our theme header.

  5. Hi Lester, I don’t mean using one of your plugins. I mean in general. For example within a theme function. It seems to not work…Do you have an idea why that is?

  6. I have not really played with Multisite yet and hence that is why I have not added that support to my plugin yet. So I can’t answer your question, sorry about it

  7. HI Lester
    no cache plugin
    no password protected admin
    no http

    but I see the error Failed To Verify Referrer”=> since I updated WP 3.4.0 to WP 3.4.1

    is there any chance to find out whats going on

    thanks

  8. Hi Lester

    where can I find if js is cached?

    htaccess => nothing
    no cache plugin

    or how can I clean js cache if there is somewhere a js cache?

    I’ve asked I’m not allowed to show the url => is it absolut necessary?

  9. ok I found it

    there was no cache plugin but wp cache true in wp-config
    I set this to false – overwrite it
    refresh the articel
    set it to true -overwrite
    refresh the articel
    and now it works

    sorry for inconvience

  10. Hi, I got this plugin and I’m trying to get it run, working under wp 3.4.1. Basically I don’t have any error listed above… and the plugin is currently loading the css and js, but nothing is shown in the page, I don’t see the ratings at all, and everything is enabled…

  11. Hi Lester,

    In Chrome and IE 9 the whole webpage will be loaded in the results container.
    In Firefox I have no problems.
    Please see what is going on here:
    http://www.wanttoknow.nl (left side of the page)

    The backend is https and I have applied the above change:
    ‘ajax_url’ => admin_url(‘admin-ajax.php’, (is_ssl() ? ‘https’ : ‘http’)),

    Can you please let me know what is causing this behaviour as I get no errors in Firefox.

  12. I forgot to mention that the problem is only in my current theme on the Wamp and live environment. Also disabling all other plugins and javascript results in the above behavior.

  13. Solved it!

    I had a function to prevent users from accessing the backend… this also blocked your plugin to work with the wp-ajax.

    I’m so happy that I found it just before I pulled all my hair out.

  14. Hello there,

    I am using WP Post Ratings as part of my premium theme Comfy.
    But there is an issue with the plugin I can not solve without the plugin is updated.

    When I activate a debugging plugin to see error messages on the front panel, I get the following isssue in every content containing article information:

    WordPress database error: [Table ‘usr_web80_9.wp_ratings’ doesn’t exist]
    SELECT rating_username, rating_rating, rating_ip FROM wp_ratings WHERE rating_postid = 1

    I tried to reinstall the plugin, clear the cache, but the issue is the same. I think the tables for the plugin are not created correctly.

    What can I do?

  15. Hi Lester,

    Since 2 days ago I noticed that my post average rates go down from the stars already gained to almost zero, because when someone click to rate a post, the number of votes increase correctly but the average is going to almost zero, like if the calculations are getting wrong… Any idea about why is this happening?

  16. So rare… It was working fine 3 days ago… Please kindly check it out directly in my website site.

    Thank you.

  17. Hi Lester,

    I do not know if I understand well what you mean, but in my custom field I can see posts_rate_key, ratings_average, ratings_score and ratings_users. Some post had votes and users fields on them from Post Ratings plugin (digitalnature) wich I had installed before your WP-PostRatings.

    I eliminated those fields but the problem persist… 🙁

    Thank you for your attention.

  18. Might be the previous plugin leaving behind those field that is causing the conflict, can you leave “ratings_average, ratings_score and ratings_users” there and delete the rest?

Comments are closed.