0

I'm new to WP and php, but am very experienced with AJAX. I'm trying to use jQuery to call a php function, which utilizes the WP Twilio Core plugin. I have the twilio.php file in the custom theme's directory, along with the custom jQuery.

Some stackoverflow answers implied you must make all AJAX calls to admin_ajax.php, which then delegates to the php function for you. I receive a 404 every time I try to reach it. Thanks.

HTML

<!-- IN HEADER FILE -->
<?php
  add_action('wp_ajax_mycustomfunc', 'twilioSMS');
?>

<form>
  <input size="50" type="text" name="number_to" placeholder="+16175551212" id="number_to" class="regular-text" />

  <script type="text/javascript">
    var ajax_url = "<?php echo admin_url('admin_ajax.php'); ?>"
  </script>

  <?php wp_nonce_field( 'twl-test' ); ?>
  <p><input id="twilio_button" type="submit" class="button-primary" value="Send Message" /></p>
</form>

JS

$("#twilio_button").click(function(e) {
    e.preventDefault();
    var number_to = $("#number_to").val();
    var data = {
        "action": "twilioSMS",
        "number": number_to
    };

    $.post(ajax_url, data)
    .done(function(res) {
        console.log("RES", res);
    })
    .fail(function(err) {
        console.log("ERR", err);
    });
});

PHP (Not even getting here)

<?php
  echo "<script type='text/javascript'>console.log('yo');</script>";

  if(isset($_POST["number_to"])) {
    $args = array(
      "number_to" => $_POST["number_to"],
      "message" => "Hello",
    );

  echo twl_send_sms($args);
  exit;
   }
?>
  • Ajax actions need to be added on ajax requests, your header file won't be loaded on an ajax request. – Milo Nov 9 '16 at 15:56
  • @Milo The header file should load when the page loads. Wouldn't it add the action then? How can you add an action on an ajax request? – Denis Wells Nov 9 '16 at 16:10
  • 2
    The page is where you are making the request from, the ajax request is a separate request and actions don't persist beyond the request they are added on. Actions need to be added in a file that loads on every request, like your theme's functions.php, or a plugin file. – Milo Nov 9 '16 at 16:18
  • @Milo My header.php does load with every request. But, I moved the add_action and the twilioSMS function to functions.php. I'm still getting a 404 for admin_ajax.php. Is that where I should send my POST request to? – Denis Wells Nov 9 '16 at 16:34
  • 1
    Your header won't load for an AJAX request – TheDeadMedic Nov 9 '16 at 16:45
0

When I was new to WP and PHP, a revelation occurred when I realized what PHP stands for. "PHP Hypertext Preprocessor." Sure, the recursive acronym is cool, but the "preprocessor" was what rung my bell. In other words, when a page is served, the PHP runs first, generating HTML, which is then interpreted by the browser. So you can't load a PHP file into your HTML doc and expect any interaction. It's already done its work and gone to the bar by the time your page is loaded.

The solution? AJAX (but you know this). And the best way to get started with WP and AJAX is to read the Codex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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