Ajax sends me back my home page as a response


Good morning all,

So, I try to do a request with ajax and everything is going fine up to this point:

In frontend, Ajax sends me my home page as a response when I am logged in as a subscriber, i.e. non-admin.

(everything is fine when I am logged in as admin or when I am logged out).

So I know admin-ajax.php is aptly named, but I’m sure there is a secure way to allow everyone to use the server.

Here is my code: Php code:

Function ma_fonction() {

      check_ajax_referer('my_nonce', securite) ;

      $test = inval($_POST['test']) ;

      echo ++$test;

      die() ;


add_action('wp_ajax_ma_fonction', 'ma_fonction') ;

add_action('wp_ajax_nopriv_ma_fonction', 'ma_fonction') ;

Js code:

JQuery(function() {

       var ajaxurl='<?php echo admin_url('admin-ajax.php'); ?>', 

       nonce="<?php echo wp_create_nonce('my_nonce'); ?>"; 

           action: 'ma_fonction', 
           securite: nonce, 
           test: 0

        jQuery.post(ajax_url, data,             function(reponse) {

            alert('résultat: ' +reponse) ;

         }) ;

}) ;

It is about building a plugin of course 🙂 Thank you for your time

Temoudjin 2 years 2020-09-26T03:10:18-05:00 0 Answers 12 views 0

Leave an answer