Skip to content

Instantly share code, notes, and snippets.

@westonruter
Created November 20, 2017 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/dcdef485f91b5782b30521009b071078 to your computer and use it in GitHub Desktop.
Save westonruter/dcdef485f91b5782b30521009b071078 to your computer and use it in GitHub Desktop.
<?php
// @todo The following does not work.
add_action( 'customize_register', function( WP_Customize_Manager $wp_customize ) {
$choices = array(
'foo' => __( 'Foo' ),
'bar' => __( 'Bar' ),
'baz' => __( 'Baz' ),
);
$wp_customize->add_setting( 'favorite_meta_vars', array(
'sanitize_callback' => function( $values ) use ( $choices ) {
if ( ! is_array( $choices ) ) {
return new WP_Error( 'invalid_value_type', __( 'Invalid value type.', '...' ) );
}
$allowed_options = array_keys( $choices );
$values = array_values( $values );
foreach ( $values as $value ) {
if ( in_array( $value, $allowed_options, true ) ) {
return new WP_Error( 'illegal_value', __( 'Illegal value.', '...' ) );
}
}
return $values;
},
) );
$wp_customize->add_control( 'favorite_meta_vars', array(
'label' => __( 'Favorite meta var(s)', '...' ),
'type' => 'select',
'choices' => $choices,
'setting' => 'favorite_meta_vars',
'section' => 'title_tagline',
'input_attrs' => array( // @todo The select control doesn't read from input_attrs.
'multiple' => true,
),
'multiple' => true, // @todo Support this.
) );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment