Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress wp_ajax public url?

Tags:

ajax

wordpress

I would like to do an ajax request in my theme.

If I am login to the Back Office, the request does, but if I'm not logged in, the returns is null... What's the solution please ?

In my view :

$.ajax({
  type: "POST",
  url: 'http://www.mysite.com/wp-admin/admin-ajax.php',
  data: $('#EventForm').serialize()+'&action=event_form',
  success: function(response){
    if(response == 1){
      alert('ok');
    } else {
      alert('no ok');
  }
});

In the functions.php (works only if I am log in back Office)

add_action('wp_ajax_event_form', 'ajax_event_form');
function ajax_event_form(){
  global $wpdb; 
  ...
  echo true;
  die;
}
like image 799
jlafforgue Avatar asked Dec 03 '22 23:12

jlafforgue


1 Answers

From the Codex: wp_ajax_nopriv_(action) executes for users that are not logged in. So, if you want it to fire on the front-end for both visitors and logged-in users, you can do this:

add_action('wp_ajax_my_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
like image 199
diggy Avatar answered Dec 11 '22 17:12

diggy