Skip to content

Commit

Permalink
Widgets: Add Customizer preview support to the Category widget when n…
Browse files Browse the repository at this point in the history
…avigating 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
  • Loading branch information
westonruter committed Sep 20, 2017
1 parent 187f352 commit b7c70ca
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/wp-includes/widgets/class-wp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public function widget( $args, $instance ) {
$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;

Expand All @@ -81,6 +82,8 @@ public function widget( $args, $instance ) {
* @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'>
Expand All @@ -89,7 +92,7 @@ public function widget( $args, $instance ) {
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;
Expand Down

0 comments on commit b7c70ca

Please sign in to comment.