Get Ajax to work for all Users on WordPress

some Plugins that use Ajax in WordPress only work when you are logged in as admin or added these hooks:

add_action('wp_ajax_my_action', 'my_action_callback'); 
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');

But I’m really having a hard time with getting everything to work for non-admin users and I’m wondering if there is a easy way (for js/php noobs) to tell wordpress to globally activate all ajax functions for alls users, wether logged in or not.

Read More

I know this is probably a very stupid and risky way if that is possible somehow, but please let me know!?!!?

Related posts

Leave a Reply

1 comment

  1. PHP wise, you’ve hit the nail on the head with your code above. This is required for each AJAX action, as each action will of course call a different function.

    Now, I’m making the assumption that you are using the default WordPress AJAX call –

    jQuery.post(ajax_object.ajax_url, data, function(response) {
    

    If that is indeed the case, for front end calls it is likely that ajax_object.ajax_url is not set. To set this, add the following to your functions.php file –

    <?php
    add_action('wp_head', 'plugin_set_ajax_url');
    function plugin_set_ajax_url() {
    ?>
        <script type="text/javascript">
            var ajax_object = {};
            ajax_object.ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>';
        </script>
    <?php
    }
    ?>