Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
Aware that I could use $_SERVER to detect the page but if the URL changes then it's case of diving back into the code to fix it. –  WolfieZero Apr 26 '12 at 10:56
2  
I am not sure what you are trying to achieve. Just to clarify: add_action('wp_ajax_<action>', 'do_my_action'); followed by a do_action('wp_ajax_<action>'); is same as calling do_my_action. Do you really need ajax for this purpose. –  KDM Jun 12 '12 at 8:53

1 Answer 1

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.

share|improve this answer
    
The issue is this method doesn't work with the flow of the project. Though, working through the problem I'm thinking this might be required, just annoying with the overheads of adding another plugin and to what level the separation needs to be done keep in mind this AJAX is core to the theme. Thanks for the suggestion though :) –  WolfieZero Apr 27 '12 at 9:25
    
Note #1: I assume, that you´re simply doing AJAX in a non-WP fashion. Which is not recommended. Just read through ajax to see if you´re doing it right. Note #2: In WP there´s no actual "file workflow". You have hooks which are guiding your workflow. Use a plugin, load your dependencies, hook where you need it, use conditional tags to route on public and use globals and hooks on admin UI pages to determine if you´re where you want to be. –  kaiser Apr 27 '12 at 11:24
    
It's difficult to give full details on the project so it's a little confusing to explain what the flow is I'm talking about (more internal policy rather than WP). But this is using the correct AJAX method unless the method I described in the question is incorrect (wp_ajax and wp_localize_script) - I've done the whole ../../wp_load.php before and it's too buggy. I've been evaluating and debugging it a bit more and some other spanners have hit the works so it looks as if I have to do it as a plugin that's dependent on a theme. :/ –  WolfieZero Apr 27 '12 at 14:50
2  
Plugin/MU-Plugin/Theme makes exactly no difference. The only difference is the place where you save it inside the WP dir struct. 1st) Go and use admin-ajax.php and the default way of using ajax 2nd) Get it running. 3rd) Take a look at this answer, when you got a working solution. This might help you to shorten the init time, depending on what you need from the WP env. –  kaiser Apr 27 '12 at 16:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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