wc_query_string_form_fields( string|array $values = null, array $exclude = array(), string $current_key = '', bool $return = false )
Outputs hidden form inputs for each query string variable.
Description Description
Parameters Parameters
- $values
-
(Optional) Name value pairs, or a URL to parse.
Default value: null
- $exclude
-
(Optional) Keys to exclude.
Default value: array()
- $current_key
-
(Optional) Current key we are outputting.
Default value: ''
- $return
-
(Optional) Whether to return.
Default value: false
Return Return
(string)
Source Source
File: includes/wc-template-functions.php
function wc_query_string_form_fields( $values = null, $exclude = array(), $current_key = '', $return = false ) {
if ( is_null( $values ) ) {
$values = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
} elseif ( is_string( $values ) ) {
$url_parts = wp_parse_url( $values );
$values = array();
if ( ! empty( $url_parts['query'] ) ) {
// This is to preserve full-stops, pluses and spaces in the query string when ran through parse_str.
$replace_chars = array(
'.' => '{dot}',
'+' => '{plus}',
);
$query_string = str_replace( array_keys( $replace_chars ), array_values( $replace_chars ), $url_parts['query'] );
// Parse the string.
parse_str( $query_string, $parsed_query_string );
// Convert the full-stops, pluses and spaces back and add to values array.
foreach ( $parsed_query_string as $key => $value ) {
$new_key = str_replace( array_values( $replace_chars ), array_keys( $replace_chars ), $key );
$new_value = str_replace( array_values( $replace_chars ), array_keys( $replace_chars ), $value );
$values[ $new_key ] = $new_value;
}
}
}
$html = '';
foreach ( $values as $key => $value ) {
if ( in_array( $key, $exclude, true ) ) {
continue;
}
if ( $current_key ) {
$key = $current_key . '[' . $key . ']';
}
if ( is_array( $value ) ) {
$html .= wc_query_string_form_fields( $value, $exclude, $key, true );
} else {
$html .= '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( wp_unslash( $value ) ) . '" />';
}
}
if ( $return ) {
return $html;
}
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
Changelog Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |