WordPress CMB2 - 获取选项值

标签 wordpress cmb2

我正在使用 CMB2 片段库中的示例在 WordPress 中添加主题选项页面

/**
 * Hook in and register a metabox to handle a theme options page and adds a menu item.
 */
function yourprefix_register_main_options_metabox() {

    /**
     * Registers main options page menu item and form.
     */
    $args = array(
        'id'           => 'yourprefix_main_options_page',
        'title'        => 'Main Options',
        'object_types' => array( 'options-page' ),
        'option_key'   => 'yourprefix_main_options',
        'tab_group'    => 'yourprefix_main_options',
        'tab_title'    => 'Main',
    );

    // 'tab_group' property is supported in > 2.4.0.
    if ( version_compare( CMB2_VERSION, '2.4.0' ) ) {
        $args['display_cb'] = 'yourprefix_options_display_with_tabs';
    }

    $main_options = new_cmb2_box( $args );

    /**
     * Options fields ids only need
     * to be unique within this box.
     * Prefix is not needed.
     */
    $main_options->add_field( array(
        'name'    => 'Site Background Color',
        'desc'    => 'field description (optional)',
        'id'      => 'bg_color',
        'type'    => 'colorpicker',
        'default' => '#ffffff',
    ) );

    /**
     * Registers secondary options page, and set main item as parent.
     */
    $args = array(
        'id'           => 'yourprefix_secondary_options_page',
        'menu_title'   => 'Secondary Options', // Use menu title, & not title to hide main h2.
        'object_types' => array( 'options-page' ),
        'option_key'   => 'yourprefix_secondary_options',
        'parent_slug'  => 'yourprefix_main_options',
        'tab_group'    => 'yourprefix_main_options',
        'tab_title'    => 'Secondary',
    );

    // 'tab_group' property is supported in > 2.4.0.
    if ( version_compare( CMB2_VERSION, '2.4.0' ) ) {
        $args['display_cb'] = 'yourprefix_options_display_with_tabs';
    }

    $secondary_options = new_cmb2_box( $args );

    $secondary_options->add_field( array(
        'name'    => 'Test Radio',
        'desc'    => 'field description (optional)',
        'id'      => 'radio',
        'type'    => 'radio',
        'options' => array(
            'option1' => 'Option One',
            'option2' => 'Option Two',
            'option3' => 'Option Three',
        ),
    ) );

    /**
     * Registers tertiary options page, and set main item as parent.
     */
    $args = array(
        'id'           => 'yourprefix_tertiary_options_page',
        'menu_title'   => 'Tertiary Options', // Use menu title, & not title to hide main h2.
        'object_types' => array( 'options-page' ),
        'option_key'   => 'yourprefix_tertiary_options',
        'parent_slug'  => 'yourprefix_main_options',
        'tab_group'    => 'yourprefix_main_options',
        'tab_title'    => 'Tertiary',
    );

    // 'tab_group' property is supported in > 2.4.0.
    if ( version_compare( CMB2_VERSION, '2.4.0' ) ) {
        $args['display_cb'] = 'yourprefix_options_display_with_tabs';
    }

    $tertiary_options = new_cmb2_box( $args );

    $tertiary_options->add_field( array(
        'name' => 'Test Text Area for Code',
        'desc' => 'field description (optional)',
        'id'   => 'textarea_code',
        'type' => 'textarea_code',
    ) );

}
add_action( 'cmb2_admin_init', 'yourprefix_register_main_options_metabox' );

/**
 * A CMB2 options-page display callback override which adds tab navigation among
 * CMB2 options pages which share this same display callback.
 *
 * @param CMB2_Options_Hookup $cmb_options The CMB2_Options_Hookup object.
 */
function yourprefix_options_display_with_tabs( $cmb_options ) {
    $tabs = yourprefix_options_page_tabs( $cmb_options );
    ?>
    <div class="wrap cmb2-options-page option-<?php echo $cmb_options->option_key; ?>">
        <?php if ( get_admin_page_title() ) : ?>
            <h2><?php echo wp_kses_post( get_admin_page_title() ); ?></h2>
        <?php endif; ?>
        <h2 class="nav-tab-wrapper">
            <?php foreach ( $tabs as $option_key => $tab_title ) : ?>
                <a class="nav-tab<?php if ( isset( $_GET['page'] ) && $option_key === $_GET['page'] ) : ?> nav-tab-active<?php endif; ?>" href="<?php menu_page_url( $option_key ); ?>"><?php echo wp_kses_post( $tab_title ); ?></a>
            <?php endforeach; ?>
        </h2>
        <form class="cmb-form" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="POST" id="<?php echo $cmb_options->cmb->cmb_id; ?>" enctype="multipart/form-data" encoding="multipart/form-data">
            <input type="hidden" name="action" value="<?php echo esc_attr( $cmb_options->option_key ); ?>">
            <?php $cmb_options->options_page_metabox(); ?>
            <?php submit_button( esc_attr( $cmb_options->cmb->prop( 'save_button' ) ), 'primary', 'submit-cmb' ); ?>
        </form>
    </div>
    <?php
}

/**
 * Gets navigation tabs array for CMB2 options pages which share the given
 * display_cb param.
 *
 * @param CMB2_Options_Hookup $cmb_options The CMB2_Options_Hookup object.
 *
 * @return array Array of tab information.
 */
function yourprefix_options_page_tabs( $cmb_options ) {
    $tab_group = $cmb_options->cmb->prop( 'tab_group' );
    $tabs      = array();

    foreach ( CMB2_Boxes::get_all() as $cmb_id => $cmb ) {
        if ( $tab_group === $cmb->prop( 'tab_group' ) ) {
            $tabs[ $cmb->options_page_keys()[0] ] = $cmb->prop( 'tab_title' )
                ? $cmb->prop( 'tab_title' )
                : $cmb->prop( 'title' );
        }
    }

    return $tabs;
}

这很好用,但我不知道如何实际获取这些值之一并将其显示在主题中。谁有例子?

最佳答案

您可以使用 cmb2_get_option()

它需要 3 个参数:cmb2_get_option( $option_key, $field_id, $default );

所以在你的情况下:

$option_key = yourprefix_main_options, 
$field_id = bg_color, 
$default = the default value (in case you set it).

它应该是这样的:

cmb2_get_option( 'yourprefix_main_options', 'bg_color' );
cmb2_get_option( 'yourprefix_secondary_options', 'radio' );

等等..

此外,您可以将自己的函数与回退一起使用:

/**
 * Wrapper function around cmb2_get_option
 * @since  0.1.0
 * @param  string $key     Options array key
 * @param  mixed  $default Optional default value
 * @return mixed           Option value
 */
function myprefix_get_option( $key = '', $default = false ) {
    if ( function_exists( 'cmb2_get_option' ) ) {
        // Use cmb2_get_option as it passes through some key filters.
        return cmb2_get_option( 'yourprefix_main_options', $key, $default );
    }

    // Fallback to get_option if CMB2 is not loaded yet.
    $opts = get_option( 'yourprefix_main_options', $default );

    $val = $default;

    if ( 'all' == $key ) {
        $val = $opts;
    } elseif ( is_array( $opts ) && array_key_exists( $key, $opts ) && false !== $opts[ $key ] ) {
        $val = $opts[ $key ];
    }

    return $val;
}

像这样使用它:

 myprefix_get_option( bg_color )

关于WordPress CMB2 - 获取选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49887946/

相关文章:

php - 如何根据元值查看帖子是否存在

wordpress - 推送到 git 存储库子文件夹上的远程源?

php - CMB2 选项页面参数

php - 多层次类别未在 wordpress 分类法中显示第三级层次结构

php - 保存所有发布数据和发布元数据后会触发哪个 WordPress Hook ?

php - 在 Wordpress 中获取未注册的图像大小

php - 根据尺寸计算自定义 Woocommerce 产品重量

php - Wordpress:用png图像更改图标[进入功能]