I’m doing a function which requires the use of the wp_ajax
action.
I’ve followed this guide to set it up with the differences of adding the requesting the object AJAX file in functions.php and also added
if(isset($_REQUEST['action'])) {
do_action('wp_ajax_'.$_REQUEST['action']);
do_action('wp_ajax_nopriv_'.$_REQUEST['action']);
}
to make it work.
Issue is that obviously having the file in functions.php causes every page to initialise workflow, but I only want it on one particular template file.
I could add it as a plugin I’m aware, but certain functions used are global to the theme and will never be used on any other theme so doesn’t make sense in my mind to do that.
Ideally, I would just like to do require('ajax_file.php');
in the template file, but that’s the main issue here is I don’t think I can (unless somebody can correct me). So if there is an easy way to call it from the functions.php file without every other page also calling it then that would be a massive help.
Thanks!
Just drop it in a plugin. Theme = view. Plugin = constructor. I keep most of my stuff in plugins. IMHO only stuff that belongs to the visual part should be in the Theme. And yes, you can do a simple
require( plugin_dir_path( $file_name ) );
for subfiles.