• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Can't delete reported post

saranganime

Member
Dec 23, 2019
34
8
8
Screenshot_1.png

I made a plugin for report posts on WordPress, the report post input was successful. but when I tried to delete the report post data on the admin page it didn't work. sorry if my english is bad.

report-post-viewer.php

https://pastebin.com/0Cxrqz96

report-post.js

JavaScript:
jQuery(document).ready(function($) {
    // Handler for deleting reported posts
    $('.delete-post').on('click', function() {
        var postId = $(this).data('post-id');

        // Confirm the deletion
        if (confirm('Are you sure you want to delete this data?')) {
            // AJAX request to delete the reported data
            $.ajax({
                type: 'POST',
                url: ajaxurl,
                data: {
                    action: 'delete_reported_data', // Action to delete data
                    nonce: report_post_vars.nonce,
                    post_id: postId // Send the post ID to be deleted
                },
                success: function(response) {
                    if (response.success) {
                        // Remove the deleted data row from the table
                        $('tr[data-post-id="' + postId + '"]').remove();
                        alert(response.data.message);
                    } else {
                        alert('Failed to delete data: ' + response.data.message);
                    }
                },
                error: function(error) {
                    alert('Failed to delete data.');
                    console.log(error);
                }
            });
        }
    });
});
 
thank you for answer, I have tried adding as above but it still doesn't work.


PHP:
// Enqueue JavaScript for admin
function enqueue_report_post_js_admin() {
    wp_enqueue_script('report-post-js', plugin_dir_url(__FILE__) . 'js/report-post.js', array('jquery'), '1.0', true);

    // Define and localize variables for JavaScript
    $report_post_vars = array(
        'nonce' => wp_create_nonce('report_post_nonce'),
    );
    wp_localize_script('report-post-js', 'report_post_vars', $report_post_vars);

    // Create a nonce and pass it to the JavaScript
    wp_localize_script('report-post', 'report_post_vars', array(
        'nonce' => wp_create_nonce('report_post_nonce'),
    ));
}
add_action('admin_enqueue_scripts', 'enqueue_report_post_js_admin');


// AJAX handler for deleting reported data
add_action('wp_ajax_delete_reported_data', 'delete_reported_data_callback');
add_action('wp_ajax_nopriv_delete_reported_data', 'delete_reported_data_callback'); // Handle for non-logged-in users

function delete_reported_data_callback() {
    check_ajax_referer('report_post_nonce', 'nonce');

    // Get the post ID from the AJAX request
    $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;

    // Check user permissions or any other validation
    if (current_user_can('manage_options')) {
        global $wpdb;
        $table_name = $wpdb->prefix . 'reported_posts';

        // Delete data from the reported_posts table based on ID
        $result = $wpdb->delete($table_name, 'id = ' . $post_id);

        if ($result !== false) {
            wp_send_json_success(array('message' => 'Data deleted successfully.'));
        } else {
            wp_send_json_error(array('message' => 'Failed to delete data.'));
        }
    } else {
        wp_send_json_error(array('message' => 'Access denied.'));
    }
}

in js

JavaScript:
jQuery(document).ready(function($) {
    // Handler for deleting reported posts
    $('.delete-post').on('click', function() {
        var postId = $(this).data('post-id');

        // Confirm the deletion
        if (confirm('Are you sure you want to delete this data?')) {
            // AJAX request to delete the reported data
            $.ajax({
                type: 'POST',
                url: ajaxurl,
                data: {
                    action: 'delete_reported_data', // Action to delete data
                    nonce: report_post_vars.nonce, // Include the nonce
                    post_id: postId // Send the post ID to be deleted
                },
                success: function(response) {
                    if (response.success) {
                        // Remove the deleted data row from the table
                        $('tr[data-post-id="' + postId + '"]').remove();
                        alert(response.data.message);
                    } else {
                        alert('Failed to delete data: ' + response.data.message);
                    }
                },
                error: function(error) {
                    alert('Failed to delete data.');
                    console.log(error);
                }
            });
        }
    });
});
 
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock