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
}
?>
1 Answer
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.