: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Replaces a row in the table or inserts it if it does not exist, based on a PRIMARY KEY or a UNIQUE index.
* A REPLACE works exactly like an INSERT, except that if an old row in the table has the same value as a new row
* for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
* @see wpdb::$field_types
* @see wp_set_wpdb_vars()
* @param string $table Table name.
* @param array $data Data to insert (in column => value pairs).
* Both `$data` columns and `$data` values should be "raw" (neither should be SQL escaped).
* A primary key or unique index is required to perform a replace operation.
* Sending a null value will cause the column to be set to NULL - the corresponding
* format is ignored in this case.
* @param string[]|string $format Optional. An array of formats to be mapped to each of the value in `$data`.
* If string, that format will be used for all of the values in `$data`.
* A format is one of '%d', '%f', '%s' (integer, float, string).
* If omitted, all values in `$data` will be treated as strings unless otherwise
* specified in wpdb::$field_types. Default null.
* @return int|false The number of rows affected, or false on error.
public function replace( $table, $data, $format = null ) {
return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
* Helper function for insert and replace.
* Runs an insert or replace query based on `$type` argument.
* @see wpdb::$field_types
* @see wp_set_wpdb_vars()
* @param string $table Table name.
* @param array $data Data to insert (in column => value pairs).
* Both `$data` columns and `$data` values should be "raw" (neither should be SQL escaped).
* Sending a null value will cause the column to be set to NULL - the corresponding
* format is ignored in this case.
* @param string[]|string $format Optional. An array of formats to be mapped to each of the value in `$data`.
* If string, that format will be used for all of the values in `$data`.
* A format is one of '%d', '%f', '%s' (integer, float, string).
* If omitted, all values in `$data` will be treated as strings unless otherwise
* specified in wpdb::$field_types. Default null.
* @param string $type Optional. Type of operation. Either 'INSERT' or 'REPLACE'.
* @return int|false The number of rows affected, or false on error.
public function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ), true ) ) {
$data = $this->process_fields( $table, $data, $format );
foreach ( $data as $value ) {
if ( is_null( $value['value'] ) ) {
$formats[] = $value['format'];
$values[] = $value['value'];
$fields = '`' . implode( '`, `', array_keys( $data ) ) . '`';
$formats = implode( ', ', $formats );
$sql = "$type INTO `$table` ($fields) VALUES ($formats)";
$this->check_current_query = false;
return $this->query( $this->prepare( $sql, $values ) );
* Updates a row in the table.
* @see wpdb::$field_types
* @see wp_set_wpdb_vars()
* @param string $table Table name.
* @param array $data Data to update (in column => value pairs).
* Both $data columns and $data values should be "raw" (neither should be SQL escaped).
* Sending a null value will cause the column to be set to NULL - the corresponding
* format is ignored in this case.
* @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".
* Sending a null value will create an IS NULL comparison - the corresponding
* format will be ignored in this case.
* @param string[]|string $format Optional. An array of formats to be mapped to each of the values in $data.
* If string, that format will be used for all of the values in $data.
* A format is one of '%d', '%f', '%s' (integer, float, string).
* If omitted, all values in $data will be treated as strings unless otherwise
* specified in wpdb::$field_types. Default null.
* @param string[]|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. Default null.
* @return int|false The number of rows updated, or false on error.
public function update( $table, $data, $where, $format = null, $where_format = null ) {
if ( ! is_array( $data ) || ! is_array( $where ) ) {
$data = $this->process_fields( $table, $data, $format );
$where = $this->process_fields( $table, $where, $where_format );
if ( false === $where ) {
foreach ( $data as $field => $value ) {
if ( is_null( $value['value'] ) ) {
$fields[] = "`$field` = NULL";
$fields[] = "`$field` = " . $value['format'];
$values[] = $value['value'];
foreach ( $where as $field => $value ) {
if ( is_null( $value['value'] ) ) {
$conditions[] = "`$field` IS NULL";
$conditions[] = "`$field` = " . $value['format'];
$values[] = $value['value'];
$fields = implode( ', ', $fields );
$conditions = implode( ' AND ', $conditions );
$sql = "UPDATE `$table` SET $fields WHERE $conditions";
$this->check_current_query = false;
return $this->query( $this->prepare( $sql, $values ) );
* Deletes a row in the table.
* @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".
* Sending a null value will create an IS NULL comparison - the corresponding
* format will be ignored in this case.
* @param string[]|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 $data will be treated as strings unless otherwise
* specified in wpdb::$field_types. Default null.
* @return int|false The number of rows deleted, or false on error.
public function delete( $table, $where, $where_format = null ) {
if ( ! is_array( $where ) ) {
$where = $this->process_fields( $table, $where, $where_format );
if ( false === $where ) {
foreach ( $where as $field => $value ) {
if ( is_null( $value['value'] ) ) {
$conditions[] = "`$field` IS NULL";
$conditions[] = "`$field` = " . $value['format'];
$values[] = $value['value'];
$conditions = implode( ' AND ', $conditions );
$sql = "DELETE FROM `$table` WHERE $conditions";
$this->check_current_query = false;
return $this->query( $this->prepare( $sql, $values ) );
* Processes arrays of field/value pairs and field formats.
* This is a helper method for wpdb's CRUD methods, which take field/value pairs
* for inserts, updates, and where clauses. This method first pairs each value
* with a format. Then it determines the charset of that field, using that
* to determine if any invalid text would be stripped. If text is stripped,
* then field processing is rejected and the query fails.
* @param string $table Table name.
* @param array $data Array of values keyed by their field names.
* @param string[]|string $format Formats or format to be mapped to the values in the data.
* @return array|false An array of fields that contain paired value and formats.
* False for invalid values.
protected function process_fields( $table, $data, $format ) {
$data = $this->process_field_formats( $data, $format );
$data = $this->process_field_charsets( $data, $table );
$data = $this->process_field_lengths( $data, $table );
$converted_data = $this->strip_invalid_text( $data );
if ( $data !== $converted_data ) {
$problem_fields = array();
foreach ( $data as $field => $value ) {
if ( $value !== $converted_data[ $field ] ) {
$problem_fields[] = $field;
wp_load_translations_early();
if ( 1 === count( $problem_fields ) ) {
$this->last_error = sprintf(
/* translators: %s: Database field where the error occurred. */
__( 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.' ),
$this->last_error = sprintf(
/* translators: %s: Database fields where the error occurred. */
__( 'WordPress database error: Processing the values for the following fields failed: %s. The supplied values may be too long or contain invalid data.' ),
implode( ', ', $problem_fields )
* Prepares arrays of value/format pairs as passed to wpdb CRUD methods.
* @param array $data Array of values keyed by their field names.
* @param string[]|string $format Formats or format to be mapped to the values in the data.
* Array of values and formats keyed by their field names.
* @type mixed $value The value to be formatted.
* @type string $format The format to be mapped to the value.
protected function process_field_formats( $data, $format ) {
$formats = (array) $format;
$original_formats = $formats;
foreach ( $data as $field => $value ) {
if ( ! empty( $format ) ) {
$value['format'] = array_shift( $formats );
if ( ! $value['format'] ) {
$value['format'] = reset( $original_formats );
} elseif ( isset( $this->field_types[ $field ] ) ) {
$value['format'] = $this->field_types[ $field ];
$data[ $field ] = $value;
* Adds field charsets to field/value/format arrays generated by wpdb::process_field_formats().
* Array of values and formats keyed by their field names,
* as it comes from the wpdb::process_field_formats() method.
* Value and format for this field.
* @type mixed $value The value to be formatted.
* @type string $format The format to be mapped to the value.
* @param string $table Table name.
* The same array of data with additional 'charset' keys, or false if
* the charset for the table cannot be found.
* Value, format, and charset for this field.
* @type mixed $value The value to be formatted.
* @type string $format The format to be mapped to the value.
* @type string|false $charset The charset to be used for the value.
protected function process_field_charsets( $data, $table ) {
foreach ( $data as $field => $value ) {
if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
* We can skip this field if we know it isn't a string.
* This checks %d/%f versus ! %s because its sprintf() could take more.
$value['charset'] = false;
$value['charset'] = $this->get_col_charset( $table, $field );
if ( is_wp_error( $value['charset'] ) ) {
$data[ $field ] = $value;
* For string fields, records the maximum string length that field can safely save.
* Array of values, formats, and charsets keyed by their field names,
* as it comes from the wpdb::process_field_charsets() method.
* Value, format, and charset for this field.
* @type mixed $value The value to be formatted.
* @type string $format The format to be mapped to the value.
* @type string|false $charset The charset to be used for the value.
* @param string $table Table name.
* The same array of data with additional 'length' keys, or false if
* information for the table cannot be found.
* Value, format, charset, and length for this field.
* @type mixed $value The value to be formatted.
* @type string $format The format to be mapped to the value.
* @type string|false $charset The charset to be used for the value.
* @type array|false $length {
* Information about the maximum length of the value.
* False if the column has no length.
* @type string $type One of 'byte' or 'char'.
* @type int $length The column length.
protected function process_field_lengths( $data, $table ) {
foreach ( $data as $field => $value ) {
if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
* We can skip this field if we know it isn't a string.
* This checks %d/%f versus ! %s because its sprintf() could take more.
$value['length'] = false;
$value['length'] = $this->get_col_length( $table, $field );
if ( is_wp_error( $value['length'] ) ) {
$data[ $field ] = $value;