I have a PHP file that I call with Cron Job and I need it separately from the rest of code. So, I have a button that call AJAX request from my dashboard. The Ajax function in my Wordpress plugin includes the file (process.php), this in turn, includes another file (snippets.php).
If I call process.php directly, it works perfectly, but not from Ajax call.
Folder structure
plugin.php
plugin.js
snippets.php
processes/process.php
plugin.js
jQuery.ajax({
type: 'GET',
url: ajaxurl,
data : {
action : 'process',
}
})
plugin.php
function process(){
include_once( 'processes/process.php' );
}
add_action( 'wp_ajax_process', 'process' );
process.php
include_once('../snippets.php')
//here not includes 'snippets.php'
You should use relative includes from their own file, otherwise, it will run from the current path. To force this you can use __DIR__
and start with a slash
plugin.php
function process(){
include_once(__DIR__.'/processes/process.php' );
}
add_action('wp_ajax_process', 'process' );
process.php
include_once(__DIR__.'/../snippets.php');
//here not includes 'snippets.php'
Also, you forgot the semicolumn ;
in process.php