I am using a plugin which (partially) stopped working. The authors are not that responsive so I started digging myself.
I've got to the part where I think that I know where the problem is: the ajax call returns 0 all the time.
If someone can help me out here?
The ajax call from the plugin:
var $this = $( this ),
$parent = $this.parents( 'li' ),
$container = $this.closest( '.rwmb-uploaded' ),
data = {
action: 'rwmb_delete_file',
_ajax_nonce: $container.data( 'delete_nonce' ),
post_id: $( '#post_ID' ).val(),
field_id: $container.data( 'field_id' ),
attachment_id: $this.data( 'attachment_id' ),
force_delete: $container.data( 'force_delete' )
};
$.post( ajaxurl, data, function( r )
{
console.log(ajaxurl);
console.log(data);
console.log(r);
if ( !r.success )
{
alert( r.data );
return;
}
$parent.addClass( 'removed' );
// If transition events not supported
if (
!( 'ontransitionend' in window )
&& ( 'onwebkittransitionend' in window )
&& !( 'onotransitionend' in myDiv || navigator.appName == 'Opera' )
)
{
$parent.remove();
$container.trigger( 'update.rwmbFile' );
}
$( '.rwmb-uploaded' ).on( 'transitionend webkitTransitionEnd otransitionend', 'li.removed', function()
{
$( this ).remove();
$container.trigger( 'update.rwmbFile' );
} );
}, 'json' );
return false;
The console.log(r) returns 0. The other two logs are filled with the correct value.
The php code for the ajax call:
static function add_actions()
{
// Add data encoding type for file uploading
add_action( 'post_edit_form_tag', array( __CLASS__, 'post_edit_form_tag' ) );
// Delete file via Ajax
add_action( 'wp_ajax_rwmb_delete_file', 'wp_ajax_delete_file' );
}
static function wp_ajax_delete_file()
{
$post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
$field_id = isset( $_POST['field_id'] ) ? $_POST['field_id'] : 0;
$attachment_id = isset( $_POST['attachment_id'] ) ? intval( $_POST['attachment_id'] ) : 0;
$force_delete = isset( $_POST['force_delete'] ) ? intval( $_POST['force_delete'] ) : 0;
check_ajax_referer( "rwmb-delete-file_{$field_id}" );
delete_post_meta( $post_id, $field_id, $attachment_id );
$ok = $force_delete ? wp_delete_attachment( $attachment_id ) : true;
if ( $ok )
wp_send_json_success();
else
wp_send_json_error( __( 'Error: Cannot delete file', 'rwmb' ) );
}
But it seems that 'wp_ajax_delete_file' is never executed.
I'm looking at this code now for several days without finding a solution. Am not that skilled in ajax so I don't know the possibilities for a possible solution.
If you need something else, I'm glad to provide.
I think you are missing this hook. This actually for non-logged in users.
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
I did a clean install and it worked again. I also think that I found the source. I called the ajax from a if-loop. Something in there went wrong. If I called the ajax outside the loop, it worked, even for the calls inside. So now i've implemented the plugin in a different way which works for me.
Sorry I didn't post this sooner, Stackoverflow didn't let me.