WordPress Ajax URL for function in functions.php

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

Leave a Comment