函数的WordPress AJAX URL。php

时间:2016-10-11 作者:LeeTee

我有一个桌面应用程序,我使用$.getJSON 要使用插件从我的wordpress站点获取数据WP REST API. 这很好,因此我假设我也可以对函数中的函数进行Ajax调用。php?我该怎么做?我所看到的示例都没有提供ajaxURL。我怎么知道这是什么?

我有以下代码:

Functions.php (in wordpress site on server)

//Called by Ajax from App - list the contents of the folder so they can be downloaded and stored locally
function folder_contents() {

    $folderPath = $_POST[\'path\'] ;
    $files = array_diff(scandir($path), array(\'.\', \'..\'));
    print_r($files);
    return $files;

    die(); 
}

add_action(\'wp_ajax_my_action\', \'folder_contents\');

app.js (run using node webkit locally from machine)

//ajax call 
var data = {
    action: \'path\',
    path: datafolder;
};
var ajaxurl = baseurl + \'\';  //WHAT IS THIS?!?!
jQuery.post(ajaxurl, data, function(response) {
    alert(\'Got this from the server: \' + response);
    console.log(response);
});

-------------------EDIT----------------------

在回答了下面的问题后,我根据建议尝试了下面的代码,但我得到了

“index.html:324未捕获引用错误:未定义ajax\\u参数”

Functions.php

//add code to use ajax
function add_ajax_script() {

    wp_localize_script( \'ajax-js\', \'ajax_params\', array( \'ajax_url\' => admin_url( \'admin-ajax.php\' ) ) );

}

//Called by Ajax from App - list the contents of the folder so they can be downloaded and stored locally

function folder_contents() {

    $folderPath = $_POST[\'path\'] ;
    $files = array_diff(scandir($path), array(\'.\', \'..\'));
    wp_send_json($files);

    die(); 
}

add_action(\'wp_ajax_folder_contents\', \'folder_contents\');
add_action(\'wp_ajax_nopriv_folder_contents\', \'folder_contents\');


add_action( \'wp_enqueue_scripts\', \'add_ajax_script\' );

App.js

//ajax call 
        var data = {action: \'powerpoint_folder_contents\',path: datafolder};
        var ajaxurl =  ajax_params.ajax_url;
        console.log(ajaxurl);
        jQuery.post(ajaxurl, data, function(response) {
            alert(\'Got this from the server: \' + response);
            console.log(response);
        });
如果我删除add_ajax_script() 函数,只需使用下面的返回0。

var ajaxurl=baseurl+\'wp admin/admin ajax。php’;

请帮助。。。

3 个回复
最合适的回答,由SO网友:Steve 整理而成

//ajax call 
var data = {
    action: \'folder_contents\',
    path: datafolder
};
var ajaxurl = baseurl + \'\';  //WHAT IS THIS?!?!
jQuery.post(ajaxurl, data, function(response) {
    alert(\'Got this from the server: \' + response);
    console.log(response);
});
以及函数中的PHP。php

//Called by Ajax from App - list the contents of the folder so they can be downloaded and stored locally
function folder_contents() {

$folderPath = $_POST[\'path\'] ;
$files = array_diff(scandir($path), array(\'.\', \'..\'));
print_r($files);
return $files;

die(); 
}

add_action(\'wp_ajax_folder_contents\', \'folder_contents\');
add_action(\'wp_ajax_nopriv_folder_contents\', \'folder_contents\');
以上将您的函数添加为ajax操作。然后在AJAX中调用函数名。

看见https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

SO网友:SdeWijs

在您的代码中,我看到您本地化了“ajax js”脚本,但脚本本身是否已排队?几周前,我不得不使用wp\\u本地化脚本,并且我不得不将我的JS脚本放入名为wp\\u本地化脚本的函数中。例如:

add_action( \'wp_enqueue_scripts\', \'google_js_script\' );

function google_js_script()
{
  // Enqueue the js script     
  wp_enqueue_script(\'google-js\', GOOGLE_PLG_URL . \'assets/js/google-places.js\', array(\'jquery\'), true);

  // Localize the enqueued JS script
  wp_localize_script( \'google-js\', \'ajax_object\',
    array( \'ajax_url\' => admin_url( \'admin-ajax.php\' ), \'place\' => \'\' ) );
}
希望这有助于调试问题。

SO网友:scott

这个WP Codex 声明可以使用全局变量ajaxurl 没有先声明。换句话说,注释掉这一行:

var ajaxurl = ajax_params.ajax_url;

...看看这是否有效。