我尝试让optgroup带有一些选项,但是我只是一个初级PHP / WordPress开发人员。我是一个开源项目的维护者:https://github.com/Remzi1993/jquery-manager
以下代码来自我目前正在致力于的optgroup分支 请参阅下面的代码:
public function get_settings_fields() {
$settings_fields = array(
// jQuery settings
'wp_jquery_manager_plugin_jquery_settings' => array(
array(
'name' => 'jquery',
'label' => __( 'jQuery', $this->text_domain ),
'desc' => __( 'On / Off', $this->text_domain ),
'default' => 'on',
'type' => 'checkbox'
),
array(
'name' => 'jquery_version',
'label' => __( 'jQuery version', $this->text_domain ),
'desc' => __( 'Select a particular jQuery version', $this->text_domain ),
'type' => 'select',
'default' => 'jquery_3x_min',
'options_groups' => array(
'jquery_3x_group' => 'Example 1',
'jquery_3x_slim_group' => 'Example 2'
// 'jquery_2x_group' => 'Swedish Cars 3',
// 'jquery_1x_group' => 'Swedish Cars 4'
),
'options' => array(
'jquery_3x_min' => $this->jquery_3x . '.min.js (default)',
'jquery_3x' => $this->jquery_3x . '.js',
'jquery_3x_slim_min' => $this->jquery_3x . '.slim.min.js (excludes ajax & effects modules, minified)',
'jquery_3x_slim' => $this->jquery_3x . '.slim.js (excludes the ajax and effects modules)',
'jquery_2x_min' => $this->jquery_2x . '.min.js',
'jquery_2x' => $this->jquery_2x . '.js',
'jquery_1x_min' => $this->jquery_1x . '.min.js',
'jquery_1x' => $this->jquery_1x . '.js'
)
),
array(
'name' => 'jquery_execution',
'label' => __( 'jQuery execution', $this->text_domain ),
'desc' => __( 'Experimental! Some plugins and/or themes may not support this. <strong>Broken for now, does nothing.</strong> See: https://github.com/Remzi1993/jquery-manager/issues/8', $this->text_domain ),
'type' => 'radio',
'default' => 'default',
'options' => array(
'default' => 'Default / Normal',
'async' => 'Async',
'defer' => 'Defer'
)
),
array(
'name' => 'debug_mode',
'label' => __( 'Debug mode', $this->text_domain ),
'desc' => __( 'On / Off', $this->text_domain ),
'default' => 'off',
'type' => 'checkbox'
)
), // End jQuery settings
// jQuery Migrate settings
'wp_jquery_manager_plugin_jquery_migrate_settings' => array(
array(
'name' => 'jquery_migrate',
'label' => __( 'jQuery Migrate', $this->text_domain ),
'desc' => __( 'On / Off', $this->text_domain ),
'default' => 'on',
'type' => 'checkbox'
),
array(
'name' => 'jquery_migrate_version',
'label' => __( 'jQuery Migrate version', $this->text_domain ),
'desc' => __( 'Select a particular jQuery Migrate version', $this->text_domain ),
'type' => 'select',
'default' => 'jquery_migrate_3x_min',
'options' => array(
'jquery_migrate_3x_min' => $this->jquery_migrate_3x . '.min.js (default)',
'jquery_migrate_3x' => $this->jquery_migrate_3x . '.js',
'jquery_migrate_1x_min' => $this->jquery_migrate_1x . '.min.js',
'jquery_migrate_1x' => $this->jquery_migrate_1x . '.js'
)
),
array(
'name' => 'jquery_migrate_head_body',
'label' => __( 'jQuery Migrate code', $this->text_domain ),
'desc' => __( 'Choose where to put jQuery Migrate in the <strong><head></strong> or at the end of the <strong><body></strong> tag, just before it closes', $this->text_domain ),
'type' => 'radio',
'default' => 'head',
'options' => array(
'head' => '<head> (default)',
'body' => '<body>'
)
),
array(
'name' => 'jquery_migrate_execution',
'label' => __( 'jQuery Migrate execution', $this->text_domain ),
'desc' => __( 'Experimental! Some plugins and/or themes do not support this. <strong>Broken for now, does nothing.</strong> See: https://github.com/Remzi1993/jquery-manager/issues/8', $this->text_domain ),
'type' => 'radio',
'default' => 'default',
'options' => array(
'default' => 'Default / Normal',
'async' => 'Async',
'defer' => 'Defer'
)
)
) // End jQuery Migrate settings
); // End $settings_fields
return $settings_fields;
}
这是我尝试通过两个foreach实现这一目标的地方。我尝试了很多设置,一个数组中的一个数组。我无法正常工作。我现在很沮丧。我真的不明白如何正确地做到这一点。
function callback_select( $args ) {
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
$html = sprintf( '<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', $size, $args['section'], $args['id'] );
foreach ( $args['options_groups'] as $key_group => $label_group ) {
foreach ( $args['options'] as $key_options => $label_options ) {
$html .= sprintf( '<optgroup label="%s">', $label_group );
$selected_option = selected( $value, $key_options, false );
$html .= sprintf( '<option value="%s" %s>%s</option>', $key_options, $selected_option, $label_options );
$html .='</optgroup>';
}
}
$html .= sprintf( '</select>' );
$html .= $this->get_field_description( $args );
echo $html;
}
这是我想要实现的:
<select class="regular" id="wp_jquery_manager_plugin_jquery_settings[jquery_version]" name="wp_jquery_manager_plugin_jquery_settings[jquery_version]">
<optgroup label="jQuery 3x">
<option selected="selected" value="jquery_3x_min">
jquery-3.4.1.min.js (default)
</option>
<option value="jquery_3x">
jquery-3.4.1.js
</option>
</optgroup>
<optgroup label="jQuery 3x slim (excludes the ajax and effects modules)">
<option value="jquery_3x_slim_min">
jquery-3.4.1.slim.min.js
</option>
<option value="jquery_3x_slim">
jquery-3.4.1.slim.js
</option>
</optgroup>
<optgroup label="jQuery 2x">
<option value="jquery_2x_min">
jquery-2.2.4.min.js
</option>
<option value="jquery_2x">
jquery-2.2.4.js
</option>
</optgroup>
<optgroup label="jQuery 1x">
<option value="jquery_1x_min">
jquery-1.12.4.min.js
</option>
<option value="jquery_1x">
jquery-1.12.4.js
</option>
</optgroup>
</select>