su_html_dropdown

The shortcodes-ultimate su html dropdown function.

Published Date - February 15, 2021

Description

Create HTML dropdown.

su_html_dropdown( $args );

Parameters

  1. $args


Usage

The following example is for adding a hook callback.

if ( !function_exists( 'su_html_dropdown' ) ) {
    require_once ABSPATH . PLUGINDIR . 'shortcodes-ultimateshortcodes-ultimate/includes/functions-html.php';
}

// The input(s). 
$args = null;

// NOTICE! Understand what this does before running.
$result = su_html_dropdown( $args );
            

Defined

The function is defined in the following location(s).

shortcodes-ultimate/includes/functions-html.php

function su_html_dropdown( $args ) {

	$args = wp_parse_args(
		$args,
		array(
			'id'       => '',
			'name'     => '',
			'class'    => '',
			'multiple' => '',
			'size'     => '',
			'disabled' => '',
			'selected' => '',
			'none'     => '',
			'options'  => array(),
			'style'    => '',
			'noselect' => '',
		)
	);

	$options = array();

	if ( ! is_array( $args['options'] ) ) {
		$args['options'] = array();
	}
	if ( $args['id'] ) {
		$args['id'] = ' id="' . $args['id'] . '"';
	}
	if ( $args['name'] ) {
		$args['name'] = ' name="' . $args['name'] . '"';
	}
	if ( $args['class'] ) {
		$args['class'] = ' class="' . $args['class'] . '"';
	}
	if ( $args['style'] ) {
		$args['style'] = ' style="' . esc_attr( $args['style'] ) . '"';
	}
	if ( $args['multiple'] ) {
		$args['multiple'] = ' multiple="multiple"';
	}
	if ( $args['disabled'] ) {
		$args['disabled'] = ' disabled="disabled"';
	}
	if ( $args['size'] ) {
		$args['size'] = ' size="' . $args['size'] . '"';
	}
	if ( $args['none'] ) {
		$args['options'][0] = $args['none'];
	}

	foreach ( $args['options'] as $id => $text ) {
		$options[] = '<option value="' . (string) $id . '">' . (string) $text . '</option>';
	}

	$options = implode( '', $options );
	$options = str_replace(
		'value="' . $args['selected'] . '"',
		'value="' . $args['selected'] . '" selected="selected"',
		$options
	);

	return $args['noselect']
		? $options :
		'<select' . $args['id'] . $args['name'] . $args['class'] . $args['multiple'] . $args['size'] . $args['disabled'] . $args['style'] . '>' . $options . '</select>';

}