New function to generate query strings

This commit is contained in:
Mike Jolley 2016-07-27 14:10:31 +01:00
parent d2adabaf89
commit a5d036d91c
2 changed files with 27 additions and 16 deletions

View File

@ -337,6 +337,31 @@ function wc_product_post_class( $classes, $class = '', $post_id = '' ) {
return $classes;
}
/**
* Outputs hidden form inputs for each query string variable.
* @param array $values name value pairs
* @param array $exclude keys to exclude
* @since 2.7.0
*/
function wc_query_string_form_fields( $values = null, $exclude = array(), $currentkey = '' ) {
if ( is_null( $values ) ) {
$values = $_GET;
}
foreach ( $values as $key => $value ) {
if ( in_array( $key, $exclude ) ) {
continue;
}
if ( $currentkey ) {
$key = $currentkey . '[' . $key . ']';
}
if ( is_array( $value ) ) {
wc_query_string_form_fields( $value, $exclude, $key );
} else {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) . '" />';
}
}
}
/** Template pages ********************************************************/
if ( ! function_exists( 'woocommerce_content' ) ) {
@ -606,7 +631,7 @@ if ( ! function_exists( 'woocommerce_product_archive_description' ) ) {
if ( is_search() ) {
return;
}
if ( is_post_type_archive( 'product' ) && 0 === absint( get_query_var( 'paged' ) ) ) {
$shop_page = get_post( wc_get_page_id( 'shop' ) );
if ( $shop_page ) {

View File

@ -27,19 +27,5 @@ if ( ! defined( 'ABSPATH' ) ) {
<option value="<?php echo esc_attr( $id ); ?>" <?php selected( $orderby, $id ); ?>><?php echo esc_html( $name ); ?></option>
<?php endforeach; ?>
</select>
<?php
// Keep query string vars intact
foreach ( $_GET as $key => $val ) {
if ( 'orderby' === $key || 'submit' === $key ) {
continue;
}
if ( is_array( $val ) ) {
foreach( $val as $innerVal ) {
echo '<input type="hidden" name="' . esc_attr( $key ) . '[]" value="' . esc_attr( $innerVal ) . '" />';
}
} else {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
}
}
?>
<?php wc_query_string_form_fields( null, array( 'orderby', 'submit' ) ); ?>
</form>