WP_Http::buildCookieHeader() WordPress Method

The WP_Http::buildCookieHeader() function builds a cookie header based on an array of cookie data. The array is expected to have the following format: array( 'name' => 'value', 'name2' => 'value2' ) The function will return a string containing the cookie header.

WP_Http::buildCookieHeader( array $r ) #

Takes the arguments for a ::request() and checks for the cookie array.


Description

If it’s found, then it upgrades any basic name => value pairs to WP_Http_Cookie instances, which are each parsed into strings and added to the Cookie: header (within the arguments array). Edits the array by reference.


Top ↑

Parameters

$r

(array)(Required)Full array of args passed into ::request()


Top ↑

Source

File: wp-includes/class-wp-http.php

	public static function buildCookieHeader( &$r ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
		if ( ! empty( $r['cookies'] ) ) {
			// Upgrade any name => value cookie pairs to WP_HTTP_Cookie instances.
			foreach ( $r['cookies'] as $name => $value ) {
				if ( ! is_object( $value ) ) {
					$r['cookies'][ $name ] = new WP_Http_Cookie(
						array(
							'name'  => $name,
							'value' => $value,
						)
					);
				}
			}

			$cookies_header = '';
			foreach ( (array) $r['cookies'] as $cookie ) {
				$cookies_header .= $cookie->getHeaderValue() . '; ';
			}

			$cookies_header         = substr( $cookies_header, 0, -2 );
			$r['headers']['cookie'] = $cookies_header;
		}
	}


Top ↑

Changelog

Changelog
VersionDescription
2.8.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.