Skip to content

Instantly share code, notes, and snippets.

@ocean90
Created June 15, 2013 14:33
Show Gist options
  • Select an option

  • Save ocean90/5788339 to your computer and use it in GitHub Desktop.

Select an option

Save ocean90/5788339 to your computer and use it in GitHub Desktop.
When you have a project which still runs on WordPress 3.3 (Oh boy..) and want to use the $wpdb::delete() method you can easily extend the wpdb class by dropping this db.php file into WP_CONTENT_DIR. Thanks @toscho for the hint.
<?php
/**
* This class provides the $wpdb::delete() method which was introduced in WordPress 3.4.
*
* @link https://core.trac.wordpress.org/ticket/18948
*/
class DS_wpdb extends wpdb {
/**
* Delete a row in the table
*
* <code>
* wpdb::delete( 'table', array( 'ID' => 1 ) )
* wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
* </code>
*
* @since 3.4.0
* @see wpdb::prepare()
* @see wpdb::$field_types
* @see wp_set_wpdb_vars()
*
* @param string $table table name
* @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
* @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types.
* @return int|false The number of rows updated, or false on error.
*/
function delete( $table, $where, $where_format = null ) {
if ( ! is_array( $where ) )
return false;
$bits = $wheres = array();
$where_formats = $where_format = (array) $where_format;
foreach ( array_keys( $where ) as $field ) {
if ( !empty( $where_format ) ) {
$form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
} elseif ( isset( $this->field_types[ $field ] ) ) {
$form = $this->field_types[ $field ];
} else {
$form = '%s';
}
$wheres[] = "$field = $form";
}
$sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres );
return $this->query( $this->prepare( $sql, $where ) );
}
}
$wpdb = new DS_wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment