From 2dfb411860810fbfd9dc66218780e731976111bf Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Mon, 31 Jul 2017 15:26:35 +0300 Subject: [PATCH] Add frontend scripts for the Categories block in dropdown mode --- lib/blocks/categories.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/blocks/categories.php b/lib/blocks/categories.php index 2f7fc04b5ef179..b7e0f023e915d6 100644 --- a/lib/blocks/categories.php +++ b/lib/blocks/categories.php @@ -27,9 +27,15 @@ function gutenberg_render_block_core_categories( $attributes ) { ); if ( ! empty( $attributes['displayAsDropdown'] ) ) { + $id = 'wp-block-categories-' . wp_rand(); + $args['id'] = $id; $wrapper_markup = '
%2$s
'; $items_markup = wp_dropdown_categories( $args ); $type = 'dropdown'; + + if ( ! is_admin() ) { + $wrapper_markup .= gutenberg_build_dropdown_script_block_core_categories( $id ); + } } else { $wrapper_markup = '
'; $items_markup = wp_list_categories( $args ); @@ -47,6 +53,33 @@ function gutenberg_render_block_core_categories( $attributes ) { return $block_content; } +/** + * Generates the inline script for a categories dropdown field. + * + * @param string $dropdown_id ID of the dropdown field. + * + * @return string Returns the dropdown onChange redirection script. + */ +function gutenberg_build_dropdown_script_block_core_categories( $dropdown_id ) { + ob_start(); + ?> + + 'gutenberg_render_block_core_categories', ) );