WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom interface that uses 30+ ajax files while running...
some files are only used in category.php while othere's are only
used in page.php...

i include the ajax loader php files in my functions.php file


include TEMPLATEPATH . '/ajaxLoops/ajax-open_client_editform.php';
include TEMPLATEPATH . '/ajaxLoops/ajax-submit_client_editform.php';

It all works great but when i tried to conditionally load them
using is_page or is_category directly in my functions.php the ajax
functions stop working.


if(is_page()) {
    include TEMPLATEPATH . '/ajaxLoops/ajax-open_client_editform.php';
    include TEMPLATEPATH . '/ajaxLoops/ajax-submit_client_editform.php';

i guess this is a matter of loading order or some other issues i am
unaware of.... Could you help me understand the problem with my approach
or an alternative way of conditionally loading this files?...



wp_enqueue_script( 'ajax-simple-example', get_stylesheet_directory_uri().'/ajaxLoops/ajax-simple_example.js', array('jquery'), 1.0 ); // jQuery will be included automatically
wp_localize_script( 'ajax-simple-example', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); // setting ajaxurl

add_action( 'wp_ajax_action_simple_example', 'ajax_simple_example' ); // ajax for logged in users
function ajax_simple_example() {

    // just an empty example for my question
    // in stackexchange

    echo '<div id="success">'.$successMsg.'</div>';

    die(); // stop executing script
share|improve this question
Are you using wp_enqueue_scripts to load your .js files? – AlxVallejo Oct 31 '12 at 15:07
Yes... i have pasted and example in my question – Sagive SEO Oct 31 '12 at 16:51
Try putting a die() inside of the condition. That will show you whether the if condition is being met. – Eric Holmes Oct 31 '12 at 16:55
oh.. the condition is met... i echoed inside the condition to check that it working... the condition is being checked inside functions.php and not in my ajax loader file - the condition works but the ajax file doesent work. i guess its due to the load order meaning whne script are enqued and when wordpress can check if(is_page) – Sagive SEO Oct 31 '12 at 17:16
i also tried to hook the if(is_page()) to init, wp_head and a few more which made no difference.. – Sagive SEO Oct 31 '12 at 17:17
up vote 2 down vote accepted

When you call admin-ajax.php no query is being produced so is_page() or is_category() or any query based conditional tag will never return true. A better way would be to include your files inside the ajax callback, meaning something like this:


function PAGE_ONLY_Function(){
    include TEMPLATEPATH . '/ajaxLoops/ajax-open_client_editform.php';
     * do your page only ajax 


function CATEGORY_ONLY_Function(){
    include TEMPLATEPATH . '/ajaxLoops/ajax-submit_client_editform.php';
     * do your Category only ajax 

So this way you add your action hook all the time but include your files only when needed.

share|improve this answer
should the localize & enqueue commands stay in those ajax file? ... well, going to try it - thanks for the quick response – Sagive SEO Nov 1 '12 at 1:14
The localize & enqueue should and can be done inside the conditional tags and that way they are only enqueued when needed. – Bainternet Nov 1 '12 at 7:31
thanks a lot mate - sorry for the delay in my response - was called by the army to the Reserve :( ... works like a charm - thanks again – Sagive SEO Nov 13 '12 at 15:06
Hard times, I am on my way there myself :) – Bainternet Nov 13 '12 at 16:10
take your own coffe - new age coffee sux ;) – Sagive SEO Nov 14 '12 at 12:09

Your Answer


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.