W3cubDocs

/WordPress

build_dropdown_script_block_core_categories( string $dropdown_id )

Generates the inline script for a categories dropdown field.

Description

Parameters

$dropdown_id

(string) (Required) ID of the dropdown field.

Return

(string) Returns the dropdown onChange redirection script.

Source

File: wp-includes/blocks/categories.php

function build_dropdown_script_block_core_categories( $dropdown_id ) {
	ob_start();
	?>
	<script type='text/javascript'>
	/* <![CDATA[ */
	( function() {
		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.onchange = onCatChange;
	})();
	/* ]]> */
	</script>
	<?php
	return ob_get_clean();
}

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/build_dropdown_script_block_core_categories