duanji2002 2016-05-26 14:30
浏览 73
已采纳

通过按钮单击调用时功能无法正常执行

I have an ajax request that looks like this:

  $('#clear_cache').click(function (event) {
      event.preventDefault();
      var ajaxurl = "clearcache.php";
      $.post(ajaxurl, function () {
          $('<p>The cache was cleared successfully.</p>').insertAfter('#clear_cache');
      });
  });

The clearcache.php file looks like this:

do_action( 'clear_terms', 'clear_transients' );

function clear_transients() {
global $wpdb;
$sql = "delete from {$wpdb->options}
where option_name like '_transient_cc%'";
return $wpdb->query($sql);
}

I know the ajax request is linking ok to the php file because when I clear out the php file and click on my button the request is made. I feel like there's something wrong with my PHP file, like the function hasn't been setup correctly or I am making the wrong request type. However when I tested out this php code out in the same file as where my button is located it works ok (but this was using a query string method). Your help is much appreciated.

  • 写回答

2条回答 默认 最新

  • dqf98772 2016-05-26 15:03
    关注

    I see a couple things wrong here. If you are using ajax with WordPress you should be running it through the admin ajax url.

    You aren't using do_action properly. The first parameter is the name of the action and the second should be the arguments for the action. I think you are confusing do_action with add_action.

    It's best practice to use die() or better yet wp_die() when you finish your ajax handler. Alternatively if you are returning some output you can use wp_send_json()

    Your ajaxurl should always point to the admin-ajax.php. If you are doing this on the admin side ajaxurl is already setup for you. If you are doing this on the frontend you should have a snippet like the following in your functions.php

    add_action( 'wp_enqueue_scripts', 'my_scripts' );
    
    function my_scripts() {
        // the file where your javascript is located    
        wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/script.js', array('jquery') );
    
        // this makes the admin url available to your javascript
        wp_localize_script( 'ajax-script', 'ajax_object',
                array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    }
    

    You can then use ajax_object.ajaxurl in your javascript to get the right URL.

    Your javascript may now look something like this:

    $('#clear_cache').click(function (event) {
          event.preventDefault();
          var data = { action: 'clear_transients' }
          $.post( ajax_object.ajaxurl, data, function () {
              $('<p>The cache was cleared successfully.</p>').insertAfter('#clear_cache');
          });
      });
    

    If you are running this in the admin just replace ajax_object.ajaxurl with ajaxurl. Notice the addition of the data variable. This will tell WordPress which action it should be running.

    For your actions you should be using the wp_ajax_(action) for admin side or the wp_ajax_nopriv_(action) for the frontend.

    This would change your ajax handler function to something like this:

    // run on the admin side
    add_action( 'wp_ajax_clear_transients', 'clear_transients' );
    
    // or run on the frontend
    add_action( 'wp_ajax_nopriv_clear_transients', 'clear_transients' );
    
    function clear_transients() {
        global $wpdb;
        $sql = "DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_cc%'";
        $wpdb->query($sql);
        wp_die();
    }
    

    Your ajax handler can also be placed in the functions.php.

    One thing I did not mention is nonces. You should be using nonces for security. Nonces are used with check_ajax_referer to make sure ajax requests are originating from the correct source.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果