diff --git a/wp-admin/includes/bookmark.php b/wp-admin/includes/bookmark.php
index 0d2de9e3c3..17a8229aa6 100644
--- a/wp-admin/includes/bookmark.php
+++ b/wp-admin/includes/bookmark.php
@@ -84,7 +84,7 @@ function wp_delete_link( $link_id ) {
wp_delete_object_term_relationships( $link_id, 'link_category' );
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->links WHERE link_id = %d", $link_id ) );
+ $wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) );
do_action( 'deleted_link', $link_id );
diff --git a/wp-admin/includes/ms.php b/wp-admin/includes/ms.php
index 065c62620a..160faef82b 100644
--- a/wp-admin/includes/ms.php
+++ b/wp-admin/includes/ms.php
@@ -89,7 +89,8 @@ function wpmu_delete_blog( $blog_id, $drop = false ) {
$wpdb->query( "DROP TABLE IF EXISTS `$table`" );
}
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) );
+ $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) );
+
$dir = apply_filters( 'wpmu_delete_blog_upload_dir', WP_CONTENT_DIR . "/blogs.dir/{$blog_id}/files/", $blog_id );
$dir = rtrim( $dir, DIRECTORY_SEPARATOR );
$top_dir = $dir;
@@ -158,8 +159,8 @@ function wpmu_delete_user( $id ) {
}
}
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->users WHERE ID = %d", $id ) );
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
+ $wpdb->delete( $wpdb->users, array( 'ID' => $id ) );
+ $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $id ) );
clean_user_cache( $id );
diff --git a/wp-admin/includes/upgrade.php b/wp-admin/includes/upgrade.php
index 171f82c935..e3cb2d58ea 100644
--- a/wp-admin/includes/upgrade.php
+++ b/wp-admin/includes/upgrade.php
@@ -300,7 +300,7 @@ As a new WordPress user, you should go to your dashboard to d
// Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id.
if ( !is_super_admin( $user_id ) && $user_id != 1 )
- $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $wpdb->base_prefix.'1_capabilities') );
+ $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $user_id , 'meta_key' => $wpdb->base_prefix.'1_capabilities' ) );
}
}
endif;
diff --git a/wp-admin/includes/user.php b/wp-admin/includes/user.php
index e915fa819a..63384fbddb 100644
--- a/wp-admin/includes/user.php
+++ b/wp-admin/includes/user.php
@@ -266,11 +266,11 @@ function wp_delete_user( $id, $reassign = 'novalue' ) {
// FINALLY, delete user
if ( !is_multisite() ) {
- $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id) );
- $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->users WHERE ID = %d", $id) );
+ $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $id ) );
+ $wpdb->delete( $wpdb->users, array( 'ID' => $id ) );
} else {
$level_key = $wpdb->get_blog_prefix() . 'capabilities'; // wpmu site admins don't have user_levels
- $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = $id AND meta_key = '{$level_key}'");
+ $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $id , 'meta_key' => $level_key ) );
}
// allow for commit transaction
diff --git a/wp-includes/comment.php b/wp-includes/comment.php
index c522cf53ea..a27bbc37d7 100644
--- a/wp-includes/comment.php
+++ b/wp-includes/comment.php
@@ -988,7 +988,7 @@ function wp_delete_comment($comment_id, $force_delete = false) {
do_action( 'deleted_commentmeta', $meta_ids );
}
- if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) )
+ if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
return false;
do_action('deleted_comment', $comment_id);
diff --git a/wp-includes/meta.php b/wp-includes/meta.php
index 3caa808177..11bacfa587 100644
--- a/wp-includes/meta.php
+++ b/wp-includes/meta.php
@@ -489,7 +489,7 @@ function delete_metadata_by_mid( $meta_type, $meta_id ) {
do_action( 'delete_postmeta', $meta_id );
// Run the query, will return true if deleted, false otherwise
- $result = (bool) $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE $id_column = %d LIMIT 1;", $meta_id ) );
+ $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
// Clear the caches.
wp_cache_delete($object_id, $meta_type . '_meta');
diff --git a/wp-includes/ms-functions.php b/wp-includes/ms-functions.php
index cea80c3131..74c28bb9cc 100644
--- a/wp-includes/ms-functions.php
+++ b/wp-includes/ms-functions.php
@@ -496,7 +496,7 @@ function wpmu_validate_user_signup($user_name, $user_email) {
$diff = $now - $registered_at;
// If registered more than two days ago, cancel registration and let this signup go through.
if ( $diff > 172800 )
- $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_login = %s", $user_name) );
+ $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
else
$errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
@@ -509,7 +509,7 @@ function wpmu_validate_user_signup($user_name, $user_email) {
$diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
// If registered more than two days ago, cancel registration and let this signup go through.
if ( $diff > 172800 )
- $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_email = %s", $user_email) );
+ $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
else
$errors->add('user_email', __('That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.'));
}
@@ -612,7 +612,7 @@ function wpmu_validate_blog_signup($blogname, $blog_title, $user = '') {
$diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
// If registered more than two days ago, cancel registration and let this signup go through.
if ( $diff > 172800 )
- $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) );
+ $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
else
$errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
}
@@ -1160,8 +1160,9 @@ function install_blog($blog_id, $blog_title = '') {
$wpdb->update( $wpdb->options, array('option_value' => ''), array('option_name' => 'admin_email') );
// remove all perms
- $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'user_level') );
- $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'capabilities') );
+ $wpdb->delete( $wpdb->usermeta, array( 'meta_key' => $table_prefix.'user_level' ) );
+
+ $wpdb->delete( $wpdb->usermeta, array( 'meta_key' => $table_prefix.'capabilities' ) );
$wpdb->suppress_errors( false );
}
diff --git a/wp-includes/option.php b/wp-includes/option.php
index 9e4fe15f2c..2a25a27a01 100644
--- a/wp-includes/option.php
+++ b/wp-includes/option.php
@@ -367,7 +367,7 @@ function delete_option( $option ) {
if ( is_null( $row ) )
return false;
do_action( 'delete_option', $option );
- $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $option) );
+ $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
if ( ! defined( 'WP_INSTALLING' ) ) {
if ( 'yes' == $row->autoload ) {
$alloptions = wp_load_alloptions();
@@ -857,7 +857,7 @@ function delete_site_option( $option ) {
$cache_key = "{$wpdb->siteid}:$option";
wp_cache_delete( $cache_key, 'site-options' );
- $result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
+ $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
}
if ( $result ) {
diff --git a/wp-includes/post.php b/wp-includes/post.php
index ac75271156..e7e40fedd7 100644
--- a/wp-includes/post.php
+++ b/wp-includes/post.php
@@ -2053,7 +2053,7 @@ function wp_delete_post( $postid = 0, $force_delete = false ) {
}
do_action( 'delete_post', $postid );
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
+ $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
do_action( 'deleted_post', $postid );
if ( 'page' == $post->post_type ) {
@@ -3810,7 +3810,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) {
wp_delete_object_term_relationships($post_id, array('category', 'post_tag'));
wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type));
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND meta_value = %d", $post_id ));
+ $wpdb->delete( $wpdb->postmeta, array( 'meta_key' => '_thumbnail_id' , 'meta_value' => $post_id ) );
$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ));
if ( ! empty( $comment_ids ) ) {
@@ -3829,7 +3829,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) {
}
do_action( 'delete_post', $post_id );
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $post_id ));
+ $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) );
do_action( 'deleted_post', $post_id );
$uploadpath = wp_upload_dir();
diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php
index 6334ee7e7c..79f8719b75 100644
--- a/wp-includes/taxonomy.php
+++ b/wp-includes/taxonomy.php
@@ -1802,12 +1802,12 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) {
clean_object_term_cache( $objects, $object_type );
do_action( 'delete_term_taxonomy', $tt_id );
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) );
+ $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
do_action( 'deleted_term_taxonomy', $tt_id );
// Delete the term if no taxonomies use it.
if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) );
+ $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );
clean_term_cache($term, $taxonomy);
diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php
index 2347c912d2..3c1b901be1 100644
--- a/wp-includes/wp-db.php
+++ b/wp-includes/wp-db.php
@@ -404,9 +404,10 @@ class wpdb {
* Keys are column names, values are format types: 'ID' => '%d'
*
* @since 2.8.0
- * @see wpdb:prepare()
- * @see wpdb:insert()
- * @see wpdb:update()
+ * @see wpdb::prepare()
+ * @see wpdb::insert()
+ * @see wpdb::update()
+ * @see wpdb::delete()
* @see wp_set_wpdb_vars()
* @access public
* @var array
@@ -1271,6 +1272,50 @@ class wpdb {
return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
}
+ /**
+ * Delete a row in the table
+ *
+ *
+ * wpdb::delete( 'table', array( 'ID' => 1 ) )
+ * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
+ * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ), 1 )
+ *
+ *
+ * @since 2.5.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 ) );
+ }
+
+
/**
* Retrieve one variable from the database.
*