wpdb::process_field_formats() WordPress Method

The wpdb::process_field_formats() method is a handy tool for processing data from WordPress forms. It can be used to sanitize data, validate data, and convert data from one format to another. This method is especially useful for processing data from custom fields.

wpdb::process_field_formats( array $data, mixed $format ) #

Prepares arrays of value/format pairs as passed to wpdb CRUD methods.


Parameters

$data

(array)(Required)Array of fields to values.

$format

(mixed)(Required)Formats to be mapped to the values in $data.


Top ↑

Return

(array) Array, keyed by field names with values being an array of 'value' and 'format' keys.


Top ↑

Source

File: wp-includes/wp-db.php

	protected function process_field_formats( $data, $format ) {
		$formats          = (array) $format;
		$original_formats = $formats;

		foreach ( $data as $field => $value ) {
			$value = array(
				'value'  => $value,
				'format' => '%s',
			);

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

		return $data;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.2.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.