0

I have a shopping cart which uses AJAX to add/remove items to the cart from an <a> styled as a button which links to an action like /wp-admin/admin-ajax.php?action=shopping_cart&id=16&nonce=123456&type=add.

These links keep getting crawled by search engines - however, since the nonce is different each time, the list of crawl errors keeps growing.

What's the most effective method to prevent search engines from trying to follow the link? I understand that a simple nofollow may not be appropriate for internal links. Alternatively, I am misusing the <a> tag and would another element and a bit of jQuery to follow the admin-ajax.php link be better?

1 Answer 1

0

If an ajax call is indexed it compromises the whole purpose of the application. The cause here is that you are making your call using GET instead of POST as suggested: https://codex.wordpress.org/AJAX_in_Plugins

The right way should be along something like :

<?php
add_action( 'admin_footer', 'my_SHOP_javascript' ); // Write our JS below here

function my_SHOP_javascript() { ?>
    <script type="text/javascript" >
    jQuery(document).ready(function($) {

        var data = {
            'action': 'shopping_cart',
            'id': 16,
            'nonce': '12345',
            'type' : 'add'
        };

        jQuery.post(ajaxurl, data, function(response) {
            //YOUR CODE AFTER SUCCESS
        });
    });
    </script> <?php
}
1
  • The server returns a 403 if the nonce isn't valid so the ajax calls aren't getting indexed, but I'd still like a method to prevent them attempting to crawl. The ajax call is initiated by jQuery with the link as a fallback. Commented Jan 24, 2017 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.