6

I have a desktop app and I use $.getJSON to get data from my wordpress site using the plugin the WP REST API. This works fine and so I assume I can also make an Ajax call to a function in the functions.php? How do I do this? None of the examples I have seen supply the ajaxURL. How do I find out what this is?

I have the following code:

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----------------------

After the answer below I have tried the below code based on the advice but I get

"index.html:324 Uncaught ReferenceError: ajax_params is not defined"

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);
        });

If I remove the add_ajax_script() function and simply use the below it returns 0.

var ajaxurl = baseurl + 'wp-admin/admin-ajax.php';

Please help...

3 Answers 3

8
//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);
});

And your PHP in functions.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');

The above add your function as an ajax action. You then call the function name in your AJAX.

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

10
1

In your code I see you localize the 'ajax-js' script, but is the script itself enqueued? I had to use wp_localize scripts a few weeks ago and I had to enqueue my JS script inside the function that called wp_localize script. For example:

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' => '' ) );
}

Hope this helps to debug the issue.

1
  • The JS is not used on the site itself. He is using the JS inside a desktop application.
    – Tex0gen
    Commented Oct 12, 2016 at 11:26
0

The WP Codex states that you can use the global variable ajaxurl without declaring it first. In other words, comment out this line:

var ajaxurl = ajax_params.ajax_url;

...and see if that works.

1
  • If you read that notice in its entirety you also see this: "This is true on the administration side only. If you are using AJAX on the front-end, you need to make your JavaScript aware of the admin-ajax.php url. A best practice is documented in the fourth example, below using wp_localize_script()."
    – brett
    Commented Aug 26, 2022 at 17:41

Your Answer

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

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