My code is so simple like when admin delete a post by Wordpress fucntion (PHP) and do action with jQuery

$id = $_POST['ID'];



This is my code , what are you think about security ? this is enough ?

1 Answer 1


At first, read something about WordPress Ajax API and about Roles and Capabilities - capability to "edit_posts" has even Contributor. I would suggest to check for 'delete_others_posts' to prove the user is at least Editor. Or use capability of 'manage_options' (has Administrator, not Editor).

Further, there is a check ajax referer function for you to help prevent entries from outside your website. Read more about nonces also.

Here is an example from the codex on using nonces when in AJAX:

In your main file, set the nonce like this:

<script type="text/javascript">
    var data = {
        action: 'my_action',
        security: '<?php echo $ajax_nonce; ?>',
        my_string: 'Hello World!'
    $.post(ajaxurl, data, function(response) {
        alert("Response: " + response);

In your ajax file, check the referrer like this:

add_action( 'wp_ajax_my_action', 'my_action_function' );
function my_action_function() {
    check_ajax_referer( 'my-special-string', 'security' );
    echo $_POST['my_string'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.