plugin development – Delete data from database using row action

Question

I have little problem how implement deleting data from database using row actions. I have my custom list which includes data from database table.
List

 function column_name( $item ) {
    $delete_nonce = wp_create_nonce();
    $title="<strong>" . $item['article_name'] . '</strong>';
    $actions = [
        'edit' => sprintf('<a href="?page=%s&id=%s">Edit</a>', 'edytuj-artykul', $item['article_id']),
        'delete' => sprintf('<a href="?page=%s&action=%s&article_id=%s&_wpnonce=%s">Delete</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint($item['article_id']), $delete_nonce)
    ];
    return $title. $this->row_actions( $actions );
}


function delete_article($item){
    $article_id = $item['article_id'];
    global $wpdb;
     $table="wp_copywriter_articles";
     $wpdb->query("DELETE FROM $table WHERE article_id = %d", $article_id);
}

In the same file I added

add_action( 'admin_action_delete', 'delete_article' );

When I click Delete action, nothing happend. I got link
http://localhost/wordpress/wp-admin/admin.php?page=Artykuly&action=delete&article_id=26&_wpnonce=60a01b82d7

but record is not deleted from database.

For testing, I added a specific ID number of article to the delete_article method in the SQL query but still not works, so this method is not used by WP, when I click delete button.

Someone can give me any tip what am I doing wrong, because I spent on this few few hours 🙂

Best Regards,
Bartek

in progress 0
BElluu 3 weeks 2021-09-04T16:49:24-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2021-09-05T15:20:05-05:00

    I think it’s better to use the $wpdb->delete() function to remove a row.

    $article_id = $item['article_id'];
    $table = 'wp_copywriter_articles';
    $wpdb->delete( $table, array( 'id' => $article_id ) );
    

Leave an answer

Browse
Browse