May i suggest:
$('tr td .widget-remove a').click(function(){
var toremove = $(this).attr('rel');
// Out of ideas - you shouldn't really be for we are here!
$.ajax({
type: 'POST', // use the method you want
dataType: 'json',
url: ajaxurl, // this is the url to the admin ajax script
data: {
nonce : your_script_data_object.nonce,
toremove: toremove
},
complete: function( object ){
// do what you want here when the ajax requests completes
}
});
});
Now in your plugin somewhere you will have to have a function that intercepts the data that your are sending. You should just make sure you are adding your ajax intercept function to the right action hook in wordpress. Here’s how you can do it.
Here is the non-oop way.
add_action('wp_ajax_my_ajax_action', 'my_ajax_function' );
And here the oop way.
add_action('wp_ajax_my_ajax_action', array( $this, 'my_ajax_function' ) );
Here is the function:
function my_ajax_function(){
// do what you want here - the world is open to you
$toremove = $_POST['toremove'];
// Any other thing you want to do
wp_send_json_success( array( 'AnyDataYouWantToSend' => $your_data ) );
}
If you encounter any error in my_ajax_function
then use wp_send_json_error
in place of
wp_send_json_success
.
I also noted one unusual thing. Why are you using wp_register_script
and wp_enqueue_script
on the same script. Here is something you can do. Remove the wp_register_script
line and let your function look like this:
function ndw_js_init(){
wp_register_script( 'ndw_js', plugin_dir_url( __FILE__ ) . '/ndw_js.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'ndw_js' );
wp_localize_script( 'ndw_js', 'toremove' );
}
add_action('admin_init', 'ndw_js_init');
You don’t need the $scriptdata
variable you were using since as i told you the ajaxurl
javascript variable already gives you access to the admin ajax url. That is only if you are in the admin. On the frontend make sure your $scriptdata
var is present as the parameter for wp_localize_script
You don’t need this line too:
wp_enqueue_script('jquery');
This is because you are already telling WordPress that your script requires jquery
in the dependencies array parameter array( 'jquery' )
.
Hope that helps.