Opened 15 years ago
Closed 15 years ago
#1079 closed defect (bug) (fixed)
Custom AJAX action functions being called twice in /wp-admin
Reported by: | MrMaz | Owned by: | |
---|---|---|---|
Milestone: | 1.1 | Priority: | minor |
Severity: | Version: | ||
Component: | Keywords: | Ajax, core function, duplicate calls | |
Cc: | Jason_JM |
Description
If you use WordPress admin interface hooks to introduce custom AJAX functionality, the actions functions are being called twice.
The reason is because if you call the default ajaxurl (which is "http://[your-site]/wp-admin/admin-ajax.php") it provides a default switch case that calls your action function for you.
See: http://core.trac.wordpress.org/browser/tags/2.8.4/wp-admin/admin-ajax.php#L1352
Since BuddyPress adds its own AJAX hook that looks for the identical $_REQUEST variable ('action'), they BOTH execute the same action function in succession.
See: http://trac.buddypress.org/browser/trunk/bp-core.php#L1707
The only safe workaround is to set your own ajaxurl inside your custom jQuery function to something other than "/wp-admin/admin-ajax.php" You must take care to not override the JS var "ajaxurl" that is set near the top of the document. Its probably a good idea to just avoid creating a JS variable named "ajaxurl" altogether.
A possible fix to the core is to do something like below, but I am not sure if this will have any side-effects or break other code.
function bp_core_add_ajax_hook() { if ( !defined('WP_ADMIN') ) do_action( 'wp_ajax_' . $_REQUEST['action'] ); }
Andy said he's bored, so Milestone 1.1