我创建了一个管理页面,在该页面中,我使用WP\\u list\\u Table显示MySQL表的列表。在这个表中,我需要能够在需要时删除记录。这就是我的问题所在。
要删除记录,我设置了以下代码。
class Genres_List extends WP_List_Table {
[...]
public static function delete_genre( $id ) {
global $wpdb;
$wpdb->delete(
"{$wpdb->prefix}genres",
[ \'id\' => $id ],
[ \'%d\' ]
);
}
function column_cb( $item ) {
return sprintf(\'<input type="checkbox" name="id[]" value="%s" />\', $item[\'id\']);
}
function column_name( $item ) {
$delete_nonce = wp_create_nonce( \'sp_delete_genre\' );
$title = \'<strong>\' . stripslashes($item[\'name\']) . \'</strong>\';
$actions = [
\'edit\' => sprintf( \'<a href="?page=%s&action=%s&id=%s">Bewerken</a>\', esc_attr( $_REQUEST[\'page\'] ), \'edit\', absint( $item[\'id\'] ) ),
\'delete\' => sprintf( \'<a href="?page=%s&action=delete&id=%s&_wpnonce=%s">Verwijderen</a>\', esc_attr( $_REQUEST[\'page\'] ), absint( $item[\'id\'] ), $delete_nonce )
];
return \'<a href="?page=genres&action=edit&id=\' . $item[\'id\'] . \'"><strong>\' . stripslashes($item[\'name\']) . \'</strong>\' . $this->row_actions( $actions );
}
function get_columns() {
$columns = [
\'cb\' => \'<input type="checkbox" />\',
\'name\' => \'Naam\'
];
return $columns;
}
public function get_bulk_actions() {
$actions = [
\'delete\' => \'Verwijderen\'
];
return $actions;
}
public function process_bulk_action() {
if ( $this->current_action() === \'delete\' ) {
if ( ! wp_verify_nonce( $_REQUEST[\'_wpnonce\'], \'sp_delete_genre\' ) ) {
die(\'This is a secure website. Your nonce did not verify. Go get a coffee.\');
} else {
self::delete_genre( absint( $_GET[\'id\'] ) );
wp_redirect( esc_url( add_query_arg() ) );
exit;
}
if ( $_POST[\'action\'] === \'delete\' || $_POST[\'action2\'] === \'delete\') {
$delete_ids = esc_sql( $_POST[\'id\'] );
foreach ( $delete_ids as $id ) { self::delete_genre( $id ); }
wp_redirect( esc_url( add_query_arg() ) );
exit;
}
}
}
}
问题是wp_verify_nonce( $_REQUEST[\'_wpnonce\'], \'sp_delete_genre\' )
尝试删除记录时返回false。我似乎找不到我做错了什么,因为我完全遵循Wordpress Codex:使用创建noncewp_create_nonce( \'sp_delete_genre\' )
._wpnonce= 参数wp_verify_nonce( $_REQUEST[\'_wpnonce\'], \'sp_delete_genre\' )