Widgets: Add Customizer preview support to the Category widget when navigating to a category via dropdown.

Switch to using a `form` that is submitted as opposed to making a direct JavaScript call to change the `window.location`. This ensures the Customizer is able to inject the customized state into the request.

Fixes #41671.


git-svn-id: https://develop.svn.wordpress.org/trunk@41547 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2017-09-20 03:15:19 +00:00
parent 187f352e9d
commit b7c70cad14
1 changed files with 5 additions and 2 deletions

View File

@ -59,10 +59,11 @@ class WP_Widget_Categories extends WP_Widget {
$cat_args = array(
'orderby' => 'name',
'show_count' => $c,
'hierarchical' => $h
'hierarchical' => $h,
);
if ( $d ) {
echo sprintf( '<form action="%s" method="get">', esc_url( home_url() ) );
$dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
$first_dropdown = false;
@ -81,6 +82,8 @@ class WP_Widget_Categories extends WP_Widget {
* @param array $cat_args An array of Categories widget drop-down arguments.
*/
wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args ) );
echo '</form>';
?>
<script type='text/javascript'>
@ -89,7 +92,7 @@ class WP_Widget_Categories extends WP_Widget {
var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
function onCatChange() {
if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value;
dropdown.parentNode.submit();
}
}
dropdown.onchange = onCatChange;