WordPress.org

Ready to get started?Download WordPress

Forums

Meta Box
Feature Request: Select List - OPTION GROUP (OPTGROUP) (2 posts)

  1. vmodha
    Member
    Posted 1 year ago #

    Can I suggest that you incorporate an option to allow the use of the 'optgroup' tag in SELECT lists.

    Great plugin btw, it's saved me a lot of time.

    http://wordpress.org/extend/plugins/meta-box/

  2. vmodha
    Member
    Posted 1 year ago #

    I've put a solution to this problem in place, you may think of a better one:

    $html	 = "<select class='rwmb-select'{$name}{$id}{$disabled}>";
    
    			$current_optgroup = "";
    			foreach ( $field['options'] as $key => $value )
    			{
    				if( $value == "optgroup" )
    				{
    					if( ($current_optgroup != $key) && (!empty($current_optgroup)) )
    					{
    						//vmodha: If there was a previously opened optgroup then close it before opening a new one.
    						$html		.= '</optgroup>';
    					}
    					$current_optgroup = $key;
    					$html		.= '<optgroup label="' . $key  . '">';
    					continue;
    				}
    
    				$selected	 = selected( in_array( $key, $meta ), true, false );
    				$html		.= "<option value='{$key}'{$selected}>{$value}</option>";
    			}
    
    			if( !empty($current_optgroup) )
    			{
    				$html		.= '</optgroup>';
    			}
    
    			$html	.= "</select>";

    You simply need to provide the optgroup title in the array KEY and set the VALUE of the array as 'optgroup'.

    'Brands And Stuff' => 'optgroup',
    							'Brands' => 'Brands',
    				'Agency Partners' => 'Agency Partners',
    				'Second Group' => 'optgroup',
    				'Production Partners' => 'Production Partners',
    				'DMS' => 'DMS'

    Simple but you may think up something better.

    Cheers
    vmodha

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.