OK, I had no idea how to title this topic.
I’m trying to override the default behavior with ajax. The problem here is not actually a problem, but a wish more.
I have this url:
<a href="http://example.com/?reaction=smk_remove_post&id=1226&_nonce=7be82cd4a0" class="smk-remove-post">Remove post</a>
In the functions.php
I have this function and the call right after:
function smk_remove_post(){
if( !empty( $_GET['reaction'] ) && 'smk_remove_post' == $_GET['reaction'] && !empty( $_GET['id'] ) ){
if( current_user_can('edit_others_posts') && !empty($_GET['_nonce']) ){
if( wp_verify_nonce( esc_html( $_GET['_nonce'] ), 'smk_remove_post' ) ){
// Delete the post
wp_delete_post( absint( $_GET['id'] ), true );
}
}
}
}
smk_remove_post();
Now if I click on the link the post with ID 1226
will be removed. All is ok. The task is done successfuly and the page is reloaded.
Here is where I need the AJAX. I want to process this URL using ajax and not reload the page, and get a proper response.
I’ve added this jQuery script:
function smk_remove_post(){
$('.smk-remove-post').on( 'click', function( event ){
event.preventDefault();
var _this = $(this);
jQuery.ajax({
type: "GET",
url: _this.attr('href'),
success: function(response){
console.log(response);
_this.text('All great, the post is removed.');
}
});
});
}
smk_remove_post();
Everything works as expected. The post is removed via ajax and no need to reload the page. But I can’t get a proper response, instead I get the full HTML source of the current page.
I know that I should use admin-ajax.php
(ajaxurl), but how do I do it? I need to send the query from URL and get the response back.
Here is what I’ve tryied:
I have added this to the jQuery script above:
data: {
'action': 'smk_remove_post_ajax',
},
And this to the PHP:
function smk_remove_post_ajax(){
smk_remove_post();
die();
}
add_action('wp_ajax_smk_remove_post_ajax', 'smk_remove_post_ajax');
It does not work. It’s ignored, the previous call is executed insted, just like this would be done without ajax.
I understand that I need somehow to send the query to admin-ajax.php instead, but how?