Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<?php
include('../../../../../../wp-config.php');
if(current_user_can('edit_posts')){
$id = $_POST['ID'];

 wp_delete_post($id);

}else{die();}
?>

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

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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">
jQuery(document).ready(function($){
    var data = {
        action: 'my_action',
        security: '<?php echo $ajax_nonce; ?>',
        my_string: 'Hello World!'
    };
    $.post(ajaxurl, data, function(response) {
        alert("Response: " + response);
    });
});
</script>

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'];
    die;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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