Delete data from database using row action

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

 function column_name( $item ) {
    $delete_nonce = wp_create_nonce();
    $title = 'strong' . $item['article_name'] . '/strong';
    $actions = [
        'edit' = sprintf('a href=?page=%sid=%sEdit/a', 'edytuj-artykul', $item['article_id']),
        'delete' = sprintf('a href=?page=%saction=%sarticle_id=%s_wpnonce=%sDelete/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=Artykulyaction=deletearticle_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

@EDIT

SOLVED

My mistake was put delete_article() method to my custom list class. When I put my method to functions.php, everything works! :)

Topic row-actions actions plugin-development database Wordpress

Category Web


The "admin_action_{$_REQUEST['action']}" hook does not pass any arguments. You would need to get the article_id from the $_REQUEST array. There are also a few other concerns such as:

  1. You're not assigning an action when you call wp_create_nonce().
  2. As Mafnah suggets in their ansewr, you should use $wpdb::delete() as a shortcut for using $wpdb::prepare().

Here's how you could rewrite this:

function column_name( $item ) {
    $delete_nonce = wp_create_nonce( 'delete_article' );
    $actions = [
        'delete' => add_query_arg( array(
            'action'        => 'delete',
            'article_id'    => $item['article_id'],
            '_wpnonce'      => $delete_nonce,
        ), admin_url( 'admin.php' ) ),
    ];
    /* Etc... */
}

function delete_article() {

    global $wpdb;

    // Ensure we can verify our nonce.
    if( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'delete_article' ) ) {
        wp_die( esc_html__( 'Could not verify request. Please try again.' ) );

    // Ensure we have an article ID provided.
    } else if( ! isset( $_REQUEST['article_id'] ) ) {
        wp_die( esc_html__( 'Could not find provided article ID. Please try again.' ) );

    // Ensure we're able to delete the actual article.
    } else if( false === $wpdb->delete( 'wp_copywriter_articles', array( 'article_id' => $_REQUEST['article_id'] ) ) ) {
        wp_die( esc_html__( 'Could not delete the provided article. Please try again.' ) );
    }

    // Redirect back to original page with 'success' $_GET
    wp_safe_redirect( add_query_arg( array(
        'page' => ( isset( $_REQUEST['page'] ) ) ? $_REQUEST['page'] : '404',
        'success' => 1,
    ), admin_url( 'admin.php' ) ) );
    exit();

}

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 ) );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.