0

I'm trying to pass some data from js to a PHP function in a WordPress plugin I'm currently developing, but I'm not sure if this is the way of doing it. Essentially what I want to achieve is as follows:

  1. Send a post or get via js when clicking a button.
  2. In PHP, if that post/get has no error, call a function. and show an "OK"-response message.
  3. It has error show an "error"-response message.

As it works now is that it always returns 0 and I don't know how to access the response correctly. Can anyone help me here? All of this is happening in the wp-admin area if that matters. Below is the code.

functions.php:

add_action('wp_ajax_post_type_search_callback', 'my_callback');

function my_callback() {
  $data= $_POST['variable'];

  $output= 'i was returned with ajax';
  //need to echo output and exit here ?
  echo $output;
  exit();
}

JS:

$('#import_posts').on('click', function(e) {
  $.ajax({
    type: "POST",
    url: "/wp-admin/admin-ajax.php",
    data: {
      action: 'my_callback',
      variable: 45
    },
    success: function (output) {
     $('.response').html(output);
    }
  });
});

HTML:

  <button id="import_posts" class="button button-primary button-large">Submit</button>
  <div class="response">
    response
  </div>
10
  • 1
    //do i need this? – yes, you do - but with different values, because right now the function/method name you are specifying there, does not match that of your actual function. If you don’t know how the WordPress hook / filter system works to begin with, you should go and properly read up on that first. Commented Mar 16, 2020 at 13:20
  • To be slightly more direct than the comment above, there is nothing currently in your PHP code which would cause the my_callback() function to be executed. Therefore no output will be created. Commented Mar 16, 2020 at 13:21
  • Hi, i have now added the correct action. I do know the action/filter concept. Just was not unsure wheter i needed to use it or not. Code updated. When running it now i get the error: "400. bad request" when calling admin-ajax.php @ADyson Commented Mar 16, 2020 at 14:35
  • array($this, 'my_callback') is wrong, you are not trying to call a static method of some class here, you have just a plain function. So this needs to be just 'my_callback' - no $this, no array. Commented Mar 16, 2020 at 14:41
  • Thanks, changed. I still get "400 (Bad Request)" on admin-ajax.php tho. Commented Mar 16, 2020 at 14:44

1 Answer 1

1

I hope this will work for you.

You passes action in JS file "my_callback" but in PHP file there is not such a action is available. You need to change this line.

add_action('wp_ajax_search_callback', array($this, 'my_action_post_type_search_callback'));

Also if possible than pass dynamic path of admin-ajax.php file so you can use this file from any of you site's page. Current integration will only work for home page. You can get the path of the admin-ajax.php via wp's default function called <?php echo admin_url('admin-ajax.php'); ?>

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, i have updated the code with the corect action. But when running it i get the error: "400. bad request" when calling admin-ajax.php. Can you elaborate your answer slightly ? Edited my code.
In wordpress we needs to use add_action('wp_ajax_nopriv_search_callback', array($this, 'my_action_post_type_search_callback')); this action also if you are trying to use your ajax call without user login. So, please try this. If this also not works then please provide your source code.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.