I have a couple AJAX requests being used in a theme options page. Both worked just fine before, but now the first works and the second fails every time – the failure looks like this in Firebug: http://cl.ly/1w5u and this is in Webkit: cl.ly/1wYn. I’m not getting ANY response of any kind. The thing is, the first one works just fine.
Here’s the JS (jQuery) that submits:
jQuery('.cropimage').click(function(){
var pid = jQuery('#tump_header_img').val();
var path = jQuery('#header_img_edit .container img').attr('src');
var dimensions = jQuery('#tump_header_img_position').val();
var security = jQuery('#_ajax_nonce_crop_apply_image').val();
jQuery.post(ajaxurl, {pid: pid, action: 'crop_apply_header_image', path: path, dimensions: dimensions, security: security}, function(response) {
console.log(response);
});
});
ajaxurl is correct, it’s the exact same URL used in the request that does work. I’ve tried stripping out everything and just trying to get a response, to no avail.
The good stuff in functions.php:
add_action('wp_ajax_crop_apply_header_image', 'crop_apply_header_image');
function crop_apply_header_image() {
check_ajax_referer('crop_apply_header_image', 'security');
$data = $_POST;
unset($data['security'], $data['action']);
$dimensions = explode(',',$data['dimensions']);
$extension_pos = strrpos($data['path'], '/'); // find position of the last dot, so where the extension starts
$newpath = substr($data['path'], 0, ($extension_pos +1)) . 'cropped-' . substr($data['path'], ($extension_pos + 1));
update_option( 'tump_header_img_path', $newpath );
die( wp_crop_image($data['pid'],$dimensions[0],$dimensions[1],$dimensions[2],$dimensions[3],940,200) );
}
Anyways – it gets to none of this as far as I can tell. I don’t know what’s wrong, any help is greatly appreciated!
If
wp_crop_image
would give an error, would you see it? Have you enabled debugging? Do you see something when you place avar_dump($data)
there?Try intercepting the control flow where it hits your server, at
admin-ajax.php
. Place avar_dump
that only triggers when your code is called:If you see this, the error happens somewhere in the WordPress code, but there isn’t too much of it between that point and your handler function. Your action should trigger at the end of the switch statement, in the
default
part:Try printing something from the
do_action
function (but only if$_REQUEST['action'] == 'crop_apply_header_image'
), and go down from there.If you have XDebug or the Zend Debugger set up (worth the time and troubles), you can step through the code without all these print statements. But for a simple debug like this, it should work.