2

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

example:

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.

example:

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?...

Cheers,
Sagive.

EDIT 1 (.PHP AJAX LOADER FILE):

<?php
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
}
?>
5
  • Are you using wp_enqueue_scripts to load your .js files?
    – AlxVallejo
    Commented Oct 31, 2012 at 15:07
  • Yes... i have pasted and example in my question
    – Sagive
    Commented Oct 31, 2012 at 16:51
  • Try putting a die() inside of the condition. That will show you whether the if condition is being met. Commented Oct 31, 2012 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
    Commented Oct 31, 2012 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
    Commented Oct 31, 2012 at 17:17

1 Answer 1

2

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:

add_action('wp_ajax_PAGE_ONLY_ACTION','PAGE_ONLY_Function');

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


add_action('wp_ajax_CATEGORY_ONLY_ACTION','CATEGORY_ONLY_Function');

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

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

8
  • should the localize & enqueue commands stay in those ajax file? ... well, going to try it - thanks for the quick response
    – Sagive
    Commented Nov 1, 2012 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
    Commented Nov 1, 2012 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
    Commented Nov 13, 2012 at 15:06
  • Hard times, I am on my way there myself :)
    – Bainternet
    Commented Nov 13, 2012 at 16:10
  • 1
    take your own coffe - new age coffee sux ;)
    – Sagive
    Commented Nov 14, 2012 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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