Wordpress主题选项将无法保存

时间:2012-11-21 18:36:10

标签: php wordpress themes wordpress-theming options

我创建了一个主要基于Twenty Eleven主题的自定义主题。我正在尝试添加主题选项。在主题选项页面上,我为“Header Font Family”和“General Font Family”创建了一个文本字段,其中的想法是用户应该能够用他们喜欢的任何字体系列替换默认字体来更改其中的字体。主题。默认设置有效(将CSS代码添加到站点),但如果我尝试更改文本框中的文本,则新设置将不会保存,并且会在选项页面的文本框中重新加载默认值。更改后,其他非文本框选项将正确更新。

以下是theme-options.php的代码。非常感谢任何帮助,如果需要,我可以提供更多代码(options.php?functions.php?)。

    <?php
/**
 * Somewhere In Between Theme Options
 *
 * @package WordPress
 * @subpackage Somewhere_In_Between
 * @since Somewhere In Between 1.0
 */

/**
 * Properly enqueue styles and scripts for our theme options page.
 *
 * This function is attached to the admin_enqueue_scripts action hook.
 *
 * @since Somewhere In Between 1.0
 *
 */
function somewhereinbetween_admin_enqueue_scripts( $hook_suffix ) {
    wp_enqueue_style( 'somewhereinbetween-theme-options', get_template_directory_uri() . '/inc/theme-options.css', false, '2011-04-28' );
    wp_enqueue_script( 'somewhereinbetween-theme-options', get_template_directory_uri() . '/inc/theme-options.js', array( 'farbtastic' ), '2011-06-10' );
    wp_enqueue_style( 'farbtastic' );
}
add_action( 'admin_print_styles-appearance_page_theme_options', 'somewhereinbetween_admin_enqueue_scripts' );

/**
 * Register the form setting for our somewhereinbetween_options array.
 *
 * This function is attached to the admin_init action hook.
 *
 * This call to register_setting() registers a validation callback, somewhereinbetween_theme_options_validate(),
 * which is used when the option is saved, to ensure that our option values are complete, properly
 * formatted, and safe.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_theme_options_init() {

    register_setting(
        'somewhereinbetween_options',       // Options group, see settings_fields() call in somewhereinbetween_theme_options_render_page()
        'somewhereinbetween_theme_options', // Database option, see somewhereinbetween_get_theme_options()
        'somewhereinbetween_theme_options_validate' // The sanitization callback, see somewhereinbetween_theme_options_validate()
    );

    // Register our settings field group
    add_settings_section(
        'general', // Unique identifier for the settings section
        '', // Section title (we don't want one)
        '__return_false', // Section callback (we don't want anything)
        'theme_options' // Menu slug, used to uniquely identify the page; see somewhereinbetween_theme_options_add_page()
    );

    // Register our individual settings fields
    add_settings_field( 'header_font_family', __( 'Header Font Family',     'somewhereinbetween' ), 'somewhereinbetween_settings_field_header_font_family', 'theme_options', 'general' );
    add_settings_field( 'general_font_family', __( 'General Font Family',     'somewhereinbetween' ), 'somewhereinbetween_settings_field_general_font_family', 'theme_options', 'general' );
    add_settings_field( 'link_color', __( 'Link Color',     'somewhereinbetween' ), 'somewhereinbetween_settings_field_link_color', 'theme_options', 'general' );
    add_settings_field( 'layout',     __( 'Default Layout', 'somewhereinbetween' ), 'somewhereinbetween_settings_field_layout',     'theme_options', 'general' );
}
add_action( 'admin_init', 'somewhereinbetween_theme_options_init' );

/**
 * Change the capability required to save the 'somewhereinbetween_options' options group.
 *
 * @see somewhereinbetween_theme_options_init() First parameter to register_setting() is the name of the options group.
 * @see somewhereinbetween_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
 *
 * By default, the options groups for all registered settings require the manage_options capability.
 * This filter is required to change our theme options page to edit_theme_options instead.
 * By default, only administrators have either of these capabilities, but the desire here is
 * to allow for finer-grained control for roles and users.
 *
 * @param string $capability The capability used for the page, which is manage_options by default.
 * @return string The capability to actually use.
 */
function somewhereinbetween_option_page_capability( $capability ) {
    return 'edit_theme_options';
}
add_filter( 'option_page_capability_somewhereinbetween_options', 'somewhereinbetween_option_page_capability' );

/**
 * Add our theme options page to the admin menu, including some help documentation.
 *
 * This function is attached to the admin_menu action hook.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_theme_options_add_page() {
    $theme_page = add_theme_page(
        __( 'Theme Options', 'somewhereinbetween' ),   // Name of page
        __( 'Theme Options', 'somewhereinbetween' ),   // Label in menu
        'edit_theme_options',                    // Capability required
        'theme_options',                         // Menu slug, used to uniquely identify the page
        'somewhereinbetween_theme_options_render_page' // Function that renders the options page
    );

    if ( ! $theme_page )
        return;

    add_action( "load-$theme_page", 'somewhereinbetween_theme_options_help' );
}
add_action( 'admin_menu', 'somewhereinbetween_theme_options_add_page' );

function somewhereinbetween_theme_options_help() {

    $help = '<p>' . __( 'Some themes provide customization options that are grouped together on a Theme Options screen. If you change themes, options may change or disappear, as they are theme-specific. Your current theme, Somewhere In Between, provides the following Theme Options:', 'somewhereinbetween' ) . '</p>' .
            '<ol>' .
                '<li>' . __( '<strong>Link Color</strong>: You can choose the color used for text links on your site. You can enter the HTML color or hex code, or you can choose visually by clicking the "Select a Color" button to pick from a color wheel.', 'somewhereinbetween' ) . '</li>' .
                '<li>' . __( '<strong>Default Layout</strong>: You can choose if you want your site&#8217;s default layout to have a sidebar on the left or the right.', 'somewhereinbetween' ) . '</li>' .
            '</ol>' .
            '<p>' . __( 'Remember to click "Save Changes" to save any changes you have made to the theme options.', 'somewhereinbetween' ) . '</p>';

    $sidebar = '<p><strong>' . __( 'For more information:', 'somewhereinbetween' ) . '</strong></p>' .
        '<p>' . __( '<a href="http://codex.wordpress.org/Appearance_Theme_Options_Screen" target="_blank">Documentation on Theme Options</a>', 'somewhereinbetween' ) . '</p>' .
        '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>', 'somewhereinbetween' ) . '</p>';

    $screen = get_current_screen();

    if ( method_exists( $screen, 'add_help_tab' ) ) {
        // WordPress 3.3
        $screen->add_help_tab( array(
            'title' => __( 'Overview', 'somewhereinbetween' ),
            'id' => 'theme-options-help',
            'content' => $help,
            )
        );

        $screen->set_help_sidebar( $sidebar );
    } else {
        // WordPress 3.2
        add_contextual_help( $screen, $help . $sidebar );
    }
}

/**
 * Returns an array of color schemes registered for Somewhere In Between.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_color_schemes() {
    $color_scheme_options = array(
        'light' => array(
            'value' => 'light',
            'label' => __( 'Light', 'somewhereinbetween' ),
            'thumbnail' => get_template_directory_uri() . '/inc/images/light.png',
            'default_link_color' => '#1b8be0',
            'default_header_font_family' => 'Futura, "Trebuchet MS", Arial, sans-serif',
            'default_general_font_family' => 'Verdana, Arial, Helvetica, sans-serif',
        ),
        'dark' => array(
            'value' => 'dark',
            'label' => __( 'Dark', 'somewhereinbetween' ),
            'thumbnail' => get_template_directory_uri() . '/inc/images/dark.png',
            'default_link_color' => '#e4741f',
            'default_header_font_family' => 'Futura, "Trebuchet MS", Arial, sans-serif',
            'default_general_font_family' => 'Verdana, Arial, Helvetica, sans-serif',
        ),
    );

    return apply_filters( 'somewhereinbetween_color_schemes', $color_scheme_options );
}

/**
 * Returns an array of layout options registered for Somewhere In Between.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_layouts() {
    $layout_options = array(
        'content-sidebar' => array(
            'value' => 'content-sidebar',
            'label' => __( 'Content on left', 'somewhereinbetween' ),
            'thumbnail' => get_template_directory_uri() . '/inc/images/content-sidebar.png',
        ),
        'sidebar-content' => array(
            'value' => 'sidebar-content',
            'label' => __( 'Content on right', 'somewhereinbetween' ),
            'thumbnail' => get_template_directory_uri() . '/inc/images/sidebar-content.png',
        ),
    );

    return apply_filters( 'somewhereinbetween_layouts', $layout_options );
}

/**
 * Returns the default options for Somewhere In Between.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_get_default_theme_options() {
    $default_theme_options = array(
        'color_scheme' => 'light',
        'link_color'   => somewhereinbetween_get_default_link_color( 'light' ),
        'header_font_family'   => somewhereinbetween_get_default_header_font_family( 'light' ),
        'general_font_family'   => somewhereinbetween_get_default_general_font_family( 'light' ),
        'theme_layout' => 'content-sidebar',
    );

    if ( is_rtl() )
        $default_theme_options['theme_layout'] = 'sidebar-content';

    return apply_filters( 'somewhereinbetween_default_theme_options', $default_theme_options );
}

/**
 * Returns the default link color for Somewhere In Between, based on color scheme.
 *
 * @since Somewhere In Between 1.0
 *
 * @param $string $color_scheme Color scheme. Defaults to the active color scheme.
 * @return $string Color.
*/
function somewhereinbetween_get_default_link_color( $color_scheme = null ) {
    if ( null === $color_scheme ) {
        $options = somewhereinbetween_get_theme_options();
        $color_scheme = $options['color_scheme'];
    }

    $color_schemes = somewhereinbetween_color_schemes();
    if ( ! isset( $color_schemes[ $color_scheme ] ) )
        return false;

    return $color_schemes[ $color_scheme ]['default_link_color'];
}

/**
 * Returns the default header font family for Somewhere In Between, based on color scheme.
 *
 * @since Somewhere In Between 1.0
 *
 * @param $string $color_scheme Color scheme. Defaults to the active color scheme.
 * @return $string Color.
*/
function somewhereinbetween_get_default_header_font_family( $color_scheme = null ) {
    if ( null === $color_scheme ) {
        $options = somewhereinbetween_get_theme_options();
        $color_scheme = $options['color_scheme'];
    }

    $color_schemes = somewhereinbetween_color_schemes();
    if ( ! isset( $color_schemes[ $color_scheme ] ) )
        return false;

    return $color_schemes[ $color_scheme ]['default_header_font_family'];
}

/**
 * Returns the default general font family for Somewhere In Between, based on color scheme.
 *
 * @since Somewhere In Between 1.0
 *
 * @param $string $color_scheme Color scheme. Defaults to the active color scheme.
 * @return $string Color.
*/
function somewhereinbetween_get_default_general_font_family( $color_scheme = null ) {
    if ( null === $color_scheme ) {
        $options = somewhereinbetween_get_theme_options();
        $color_scheme = $options['color_scheme'];
    }

    $color_schemes = somewhereinbetween_color_schemes();
    if ( ! isset( $color_schemes[ $color_scheme ] ) )
        return false;

    return $color_schemes[ $color_scheme ]['default_general_font_family'];
}

/**
 * Returns the options array for Somewhere In Between.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_get_theme_options() {
    return get_option( 'somewhereinbetween_theme_options', somewhereinbetween_get_default_theme_options() );
}

/**
 * Renders the Color Scheme setting field.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_settings_field_color_scheme() {
    $options = somewhereinbetween_get_theme_options();

    foreach ( somewhereinbetween_color_schemes() as $scheme ) {
    ?>
    <div class="layout image-radio-option color-scheme">
    <label class="description">
        <input type="radio" name="somewhereinbetween_theme_options[color_scheme]" value="<?php echo esc_attr( $scheme['value'] ); ?>" <?php checked( $options['color_scheme'], $scheme['value'] ); ?> />
        <input type="hidden" id="default-color-<?php echo esc_attr( $scheme['value'] ); ?>" value="<?php echo esc_attr( $scheme['default_link_color'] ); ?>" />
        <input type="hidden" id="default-header-font-family-<?php echo esc_attr( $scheme['value'] ); ?>" value="<?php echo esc_attr( $scheme['default_header_font_family'] ); ?>" />
        <input type="hidden" id="default-general-font-family-<?php echo esc_attr( $scheme['value'] ); ?>" value="<?php echo esc_attr( $scheme['default_general_font_family'] ); ?>" />
        <span>
            <img src="<?php echo esc_url( $scheme['thumbnail'] ); ?>" width="136" height="122" alt="" />
            <?php echo $scheme['label']; ?>
        </span>
    </label>
    </div>
    <?php
    }
}

/**
 * Renders the Link Color setting field.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_settings_field_link_color() {
    $options = somewhereinbetween_get_theme_options();
    ?>
    <input type="text" name="somewhereinbetween_theme_options[link_color]" id="link-color" value="<?php echo esc_attr( $options['link_color'] ); ?>" />
    <a href="#" class="pickcolor hide-if-no-js" id="link-color-example"></a>
    <input type="button" class="pickcolor button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color', 'somewhereinbetween' ); ?>" />
    <div id="colorPickerDiv" style="z-index: 100; background:#eee; border:1px solid #ccc; position:absolute; display:none;"></div>
    <br />
    <span><?php printf( __( 'Default color: %s', 'somewhereinbetween' ), '<span id="default-color">' . somewhereinbetween_get_default_link_color( $options['color_scheme'] ) . '</span>' ); ?></span>
    <?php
}

/**
 * Renders the Header Font Family setting field.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_settings_field_header_font_family() {
    $options = somewhereinbetween_get_theme_options();
    ?>
    <input type="text" name="somewhereinbetween_theme_options[header_font_family]" id="header-font-family" value="<?php echo esc_attr( $options['header_font_family'] ); ?>" />
    <br />
    <span><?php printf( __( 'Default header font family: %s', 'somewhereinbetween' ), '<span id="default-header-font-family">' . somewhereinbetween_get_default_header_font_family ( $options['color_scheme'] ) . '</span>' ); ?></span>
    <?php
}

/**
 * Renders the General Font Family setting field.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_settings_field_general_font_family() {
    $options = somewhereinbetween_get_theme_options();
    ?>
    <input type="text" name="somewhereinbetween_theme_options[general_font_family]" id="general-font-family" value="<?php echo esc_attr( $options['general_font_family'] ); ?>" />
    <br />
    <span><?php printf( __( 'Default general font family: %s', 'somewhereinbetween' ), '<span id="default-general-font-family">' . somewhereinbetween_get_default_general_font_family ( $options['color_scheme'] ) . '</span>' ); ?></span>
    <?php
}

/**
 * Renders the Layout setting field.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_settings_field_layout() {
    $options = somewhereinbetween_get_theme_options();
    foreach ( somewhereinbetween_layouts() as $layout ) {
        ?>
        <div class="layout image-radio-option theme-layout">
        <label class="description">
            <input type="radio" name="somewhereinbetween_theme_options[theme_layout]" value="<?php echo esc_attr( $layout['value'] ); ?>" <?php checked( $options['theme_layout'], $layout['value'] ); ?> />
            <span>
                <img src="<?php echo esc_url( $layout['thumbnail'] ); ?>" width="136" height="122" alt="" />
                <?php echo $layout['label']; ?>
            </span>
        </label>
        </div>
        <?php
    }
}

/**
 * Returns the options array for Somewhere In Between.
 *
 * @since Somewhere In Between 1.2
 */
function somewhereinbetween_theme_options_render_page() {
    ?>
    <div class="wrap">
        <?php screen_icon(); ?>
        <?php $theme_name = function_exists( 'wp_get_theme' ) ? wp_get_theme() : get_current_theme(); ?>
        <h2><?php printf( __( '%s Theme Options', 'somewhereinbetween' ), $theme_name ); ?></h2>
        <?php settings_errors(); ?>

        <form method="post" action="options.php">
            <?php
                settings_fields( 'somewhereinbetween_options' );
                do_settings_sections( 'theme_options' );
                submit_button();
            ?>
        </form>
    </div>
    <?php
}

/**
 * Sanitize and validate form input. Accepts an array, return a sanitized array.
 *
 * @see somewhereinbetween_theme_options_init()
 * @todo set up Reset Options action
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_theme_options_validate( $input ) {
    $output = $defaults = somewhereinbetween_get_default_theme_options();

    // Color scheme must be in our array of color scheme options
    if ( isset( $input['color_scheme'] ) && array_key_exists( $input['color_scheme'], somewhereinbetween_color_schemes() ) )
        $output['color_scheme'] = $input['color_scheme'];

    // Our defaults for the header/general font families andlink color may have changed, based on the color scheme.
    $output['link_color'] = $defaults['link_color'] = somewhereinbetween_get_default_link_color( $output['color_scheme'] );

    // Link color must be 3 or 6 hexadecimal characters
    if ( isset( $input['link_color'] ) && preg_match( '/^#?([a-f0-9]{3}){1,2}$/i', $input['link_color'] ) )
        $output['link_color'] = '#' . strtolower( ltrim( $input['link_color'], '#' ) );

    // Theme layout must be in our array of theme layout options
    if ( isset( $input['theme_layout'] ) && array_key_exists( $input['theme_layout'], somewhereinbetween_layouts() ) )
        $output['theme_layout'] = $input['theme_layout'];

    return apply_filters( 'somewhereinbetween_theme_options_validate', $output, $input, $defaults );
}

/**
 * Enqueue the styles for the current color scheme.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_enqueue_color_scheme() {
    $options = somewhereinbetween_get_theme_options();
    $color_scheme = $options['color_scheme'];

    if ( 'dark' == $color_scheme )
        wp_enqueue_style( 'dark', get_template_directory_uri() . '/colors/dark.css', array(), null );

    do_action( 'somewhereinbetween_enqueue_color_scheme', $color_scheme );
}
add_action( 'wp_enqueue_scripts', 'somewhereinbetween_enqueue_color_scheme' );

/**
 * Add a style block to the theme for the current link color.
 *
 * This function is attached to the wp_head action hook.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_print_link_color_style() {
    $options = somewhereinbetween_get_theme_options();
    $link_color = $options['link_color'];

    $default_options = somewhereinbetween_get_default_theme_options();

    // Don't do anything if the current link color is the default.
    if ( $default_options['link_color'] == $link_color )
        return;
?>
    <style>
        /* Link color */
        a,
        #site-title a:focus,
        #site-title a:hover,
        #site-title a:active,
        .entry-title a:hover,
        .entry-title a:focus,
        .entry-title a:active,
        .widget_somewhereinbetween_ephemera .comments-link a:hover,
        section.recent-posts .other-recent-posts a[rel="bookmark"]:hover,
        section.recent-posts .other-recent-posts .comments-link a:hover,
        .format-image footer.entry-meta a:hover,
        #site-generator a:hover, #access ul a:hover
        { color: <?php echo $link_color; ?>; }
        section.recent-posts .other-recent-posts .comments-link a:hover {
            border-color: <?php echo $link_color; ?>;
        }
        article.feature-image.small .entry-summary p a:hover,
        .entry-header .comments-link a:hover,
        .entry-header .comments-link a:focus,
        .entry-header .comments-link a:active,
        .feature-slider a.active {
            background-color: <?php echo $link_color; ?>;
        }
    </style>
<?php
}
add_action( 'wp_head', 'somewhereinbetween_print_link_color_style' );

/**
 * Add a style block to the theme for the current header font family.
 *
 * This function is attached to the wp_head action hook.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_print_header_font_family() {
    $options = somewhereinbetween_get_theme_options();
    $header_font_family = $options['header_font_family'];

    $default_options = somewhereinbetween_get_default_theme_options();

?>
    <style>
        /* Header font family */
        h1,h2,h3,h4,h5,h6, #access,
        #site-title,
        .entry-title,
        { font-family: <?php echo $header_font_family; ?>; }
    </style>
<?php
}
add_action( 'wp_head', 'somewhereinbetween_print_header_font_family' );

/**
 * Add a style block to the theme for the current general font family.
 *
 * This function is attached to the wp_head action hook.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_print_general_font_family() {
    $options = somewhereinbetween_get_theme_options();
    $general_font_family = $options['general_font_family'];

    $default_options = somewhereinbetween_get_default_theme_options();

?>
    <style>
        /* General font family */
        #page
        { font-family: <?php echo $general_font_family; ?>; }
    </style>
<?php
}
add_action( 'wp_head', 'somewhereinbetween_print_general_font_family' );

/**
 * Adds Somewhere In Between layout classes to the array of body classes.
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_layout_classes( $existing_classes ) {
    $options = somewhereinbetween_get_theme_options();
    $current_layout = $options['theme_layout'];

    if ( in_array( $current_layout, array( 'content-sidebar', 'sidebar-content' ) ) )
        $classes = array( 'two-column' );
    else
        $classes = array( 'one-column' );

    if ( 'content-sidebar' == $current_layout )
        $classes[] = 'right-sidebar';
    elseif ( 'sidebar-content' == $current_layout )
        $classes[] = 'left-sidebar';
    else
        $classes[] = $current_layout;

    $classes = apply_filters( 'somewhereinbetween_layout_classes', $classes, $current_layout );

    return array_merge( $existing_classes, $classes );
}
add_filter( 'body_class', 'somewhereinbetween_layout_classes' );

/**
 * Implements Somewhere In Between theme options into Theme Customizer
 *
 * @param $wp_customize Theme Customizer object
 * @return void
 *
 * @since Somewhere In Between 1.0
 */
function somewhereinbetween_customize_register( $wp_customize ) {
    $wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
    $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';

    $options  = somewhereinbetween_get_theme_options();
    $defaults = somewhereinbetween_get_default_theme_options();

    $wp_customize->add_setting( 'somewhereinbetween_theme_options[color_scheme]', array(
        'default'    => $defaults['color_scheme'],
        'type'       => 'option',
        'capability' => 'edit_theme_options',
    ) );

    $schemes = somewhereinbetween_color_schemes();
    $choices = array();
    foreach ( $schemes as $scheme ) {
        $choices[ $scheme['value'] ] = $scheme['label'];
    }

    // Link Color (added to Color Scheme section in Theme Customizer)
    $wp_customize->add_setting( 'somewhereinbetween_theme_options[link_color]', array(
        'default'           => somewhereinbetween_get_default_link_color( $options['color_scheme'] ),
        'type'              => 'option',
        'sanitize_callback' => 'sanitize_hex_color',
        'capability'        => 'edit_theme_options',
    ) );

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link_color', array(
        'label'    => __( 'Link Color', 'somewhereinbetween' ),
        'section'  => 'colors',
        'settings' => 'somewhereinbetween_theme_options[link_color]',
    ) ) );

    $wp_customize->add_setting( 'somewhereinbetween_theme_options[header_font_family]', array(
        'default'           => somewhereinbetween_get_default_header_font_family( $options['color_scheme'] ),
        'type'              => 'option',
        'sanitize_callback' => 'sanitize_key',
        'capability'        => 'edit_theme_options',
    ) );

    $wp_customize->add_setting( 'somewhereinbetween_theme_options[general_font_family]', array(
        'default'           => somewhereinbetween_get_default_general_font_family( $options['color_scheme'] ),
        'type'              => 'option',
        'sanitize_callback' => 'sanitize_key',
        'capability'        => 'edit_theme_options',
    ) );

    // Default Layout
    $wp_customize->add_section( 'somewhereinbetween_layout', array(
        'title'    => __( 'Layout', 'somewhereinbetween' ),
        'priority' => 50,
    ) );

    $wp_customize->add_setting( 'somewhereinbetween_theme_options[theme_layout]', array(
        'type'              => 'option',
        'default'           => $defaults['theme_layout'],
        'sanitize_callback' => 'sanitize_key',
    ) );

    $layouts = somewhereinbetween_layouts();
    $choices = array();
    foreach ( $layouts as $layout ) {
        $choices[$layout['value']] = $layout['label'];
    }

    $wp_customize->add_control( 'somewhereinbetween_theme_options[theme_layout]', array(
        'section'    => 'somewhereinbetween_layout',
        'type'       => 'radio',
        'choices'    => $choices,
    ) );
}
add_action( 'customize_register', 'somewhereinbetween_customize_register' );

/**
 * Bind JS handlers to make Theme Customizer preview reload changes asynchronously.
 * Used with blogname and blogdescription.
 *
 * @since Somewhere In Between 1.3
 */
function somewhereinbetween_customize_preview_js() {
    wp_enqueue_script( 'somewhereinbetween-customizer', get_template_directory_uri() . '/inc/theme-customizer.js', array( 'customize-preview' ), '20120523', true );
}
add_action( 'customize_preview_init', 'somewhereinbetween_customize_preview_js' );

1 个答案:

答案 0 :(得分:1)

看看你的register_setting()消毒回调;您似乎没有添加选项:

function somewhereinbetween_theme_options_validate( $input ) {
    $output = $defaults = somewhereinbetween_get_default_theme_options();

    // Color scheme must be in our array of color scheme options
    if ( isset( $input['color_scheme'] ) && array_key_exists( $input['color_scheme'], somewhereinbetween_color_schemes() ) )
        $output['color_scheme'] = $input['color_scheme'];

    // Our defaults for the header/general font families andlink color may have changed, based on the color scheme.
    $output['link_color'] = $defaults['link_color'] = somewhereinbetween_get_default_link_color( $output['color_scheme'] );

    // Link color must be 3 or 6 hexadecimal characters
    if ( isset( $input['link_color'] ) && preg_match( '/^#?([a-f0-9]{3}){1,2}$/i', $input['link_color'] ) )
        $output['link_color'] = '#' . strtolower( ltrim( $input['link_color'], '#' ) );

    // Theme layout must be in our array of theme layout options
    if ( isset( $input['theme_layout'] ) && array_key_exists( $input['theme_layout'], somewhereinbetween_layouts() ) )
        $output['theme_layout'] = $input['theme_layout'];

    return apply_filters( 'somewhereinbetween_theme_options_validate', $output, $input, $defaults );
}

使用设置API时,通过register_setting(),所有设置必须通过清理回调列入白名单。因此,您需要为$input['header-font-family']$input['general-font-family']添加处理。

如果您允许输入文字,那么您需要进行清理(例如,通过wp_filter_nohtml_kses()),但您还需要验证(即某种方式)确保输入的文本是有效的字体系列。)