How do I add an action using namespaces, and file separation?
I'm working on a plugin, and I'm trying to get to grips with namespaces and how they work with actions. In a separate file, I have a single function with a namespace declared at the top:
namespace verism\ImageUpload;
function image_upload() {
...
echo 'function successfully called';
wp_die();
}
In the main plugin php file I'm trying to add an action as follows:
namespace verism;
use function verism\ImageUpload\image_upload as image_upload;
add_action( 'wp_ajax_set_image_from_url', 'image_upload' );
add_action( 'wp_ajax_nopriv_set_image_from_url', 'image_upload' );
I have also tried:
namespace verism;
use function verism\ImageUpload\image_upload as image_upload;
add_action( 'wp_ajax_set_image_from_url', '\verism\ImageUpload\image_upload' );
add_action( 'wp_ajax_nopriv_set_image_from_url', '\verism\ImageUpload\image_upload' );
However, none of these approaches work - when I log the AJAX result to the console, I always get 0.
Note: If I copy the image_upload
function into the main file, I can see the correct result, the issue seems to only be when it's located in a separate file.
Can anyone shed some light?
EDIT: This question was deemed too similar to an existing one, and so has been closed, so I cannot accept an answer. However, this question deals with a different issue, namely that of requiring—rather than using—a separate file. User Rup was kind enough to offer the correct answer to the problem, in the comments.