Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active August 29, 2015 14:14
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/1717cadbb3ef581291ba to your computer and use it in GitHub Desktop.
Save westonruter/1717cadbb3ef581291ba to your computer and use it in GitHub Desktop.
<?php
$foo_color_default = '#f00';
$foo_text_default = 'Lorem';
// These add_option() eliminate the need for supplying default values for settings,
// and it eliminates the need to supply the 2nd $default arg to get_option()
add_option( 'foo_color', $foo_color_default );
add_option( 'foo_text', $foo_text_default );
add_action( 'customize_register', function() use ( $foo_color_default, $foo_text_default ) {
global $wp_customize;
$wp_customize->add_panel( 'foo', array(
'title' => 'Foo',
'capability' => 'edit_theme_options',
) );
$wp_customize->add_section( 'bar', array(
'title' => 'Bar',
'capability' => 'edit_theme_options',
'panel' => 'foo'
) );
$wp_customize->add_setting( 'foo_text', array(
'default' => $foo_text_default, // not really needed because add_option() call used with default supplied
'capability' => 'edit_posts',
'type' => 'option',
) );
$wp_customize->add_control( 'foo_text', array(
'label' => 'Foo & bar',
'section' => 'bar',
'type' => 'textarea',
) );
$wp_customize->add_setting( 'foo_color', array(
'default' => $foo_color_default, // not really needed because add_option() call used with default supplied
'capability' => 'edit_theme_options',
'type' => 'option',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'foo_color', array(
'label' => 'Color',
'section' => 'bar',
) ) );
});
add_action( 'wp_head', function() use ( $foo_color_default, $foo_text_default ) {
var_dump( get_option( 'foo_color', $foo_color_default ) ); // second argument not needed since add_option() call used
var_dump( get_option( 'foo_text', $foo_text_default ) ); // second argument not needed since add_option() call used
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment