【问题标题】:How to get input text from theme options on wordpress?如何从 wordpress 的主题选项中获取输入文本?
【发布时间】:2013-03-10 06:51:49
【问题描述】:

我刚刚在我的theme-options.php文件中设置了一个新的输入文本选项,类似于二十一主题代码。

以下是关于输入的部分我的 theme-options.php 代码,我正在寻找一种方法来获取用户输入到“事实”文本中的输入并将其显示在 index.php 页面上:

function themename_theme_options_init() {

register_setting(
    'themename_options',       // Options group, see settings_fields() call in themename_theme_options_render_page()
    'themename_theme_options', // Database option, see themename_get_theme_options()
    'themename_theme_options_validate' // The sanitization callback, see themename_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 themename_theme_options_add_page()
);

// Register our individual settings fields
add_settings_field( 'facts', __( 'Facts',     'themename' ), 'themename_settings_field_facts', 'theme_options', 'general' );
add_settings_field( 'link_color', __( 'Link Color',     'themename' ), 'themename_settings_field_link_color', 'theme_options', 'general' );
}
add_action( 'admin_init', 'themename_theme_options_init' );

根据配色方案返回主题名称的默认事实:

function themename_get_default_facts( $color_scheme = null ) {
if ( null === $color_scheme ) {
    $options = themename_get_theme_options();
    $color_scheme = $options['color_scheme'];
}

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

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

呈现 Facts 设置字段。

function themename_settings_field_facts() {
$options = themename_get_theme_options();
?>
<input type="text" name="themename_theme_options[facts]" id="facts" value="<?php echo esc_attr( $options['facts'] ); ?>" />
<br />
<span><?php printf( __( 'Default facts: %s', 'themename' ), '<span id="default-facts">' . themename_get_default_facts ( $options['color_scheme'] ) . '</span>' ); ?></span>
<?php
}

清理和验证表单输入:

function themename_theme_options_validate( $input ) {
$output = $defaults = themename_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'], themename_color_schemes() ) )
    $output['color_scheme'] = $input['color_scheme'];

// Facts must be characters.
if ( isset( $input['facts'] ) )
    $output['facts'] = '' . ( ltrim( $input['facts'], '' ) );

// Our defaults for the link color may have changed, based on the color scheme.
$output['link_color'] = $defaults['link_color'] = themename_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'], '#' ) );

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

在主题定制器中实现主题选项:

function themename_customize_register( $wp_customize ) {
$wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
$wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';

$options  = themename_get_theme_options();
$defaults = themename_get_default_theme_options();

$wp_customize->add_setting( 'themename_theme_options[facts]', array(
    'default'           => themename_get_default_facts( $options['color_scheme'] ),
    'type'              => 'option',
    'sanitize_callback' => 'sanitize_text_field',
    'capability'        => 'edit_theme_options',
) );

}
add_action( 'customize_register', 'themename_customize_register' );

感谢任何可以提供帮助的人 :) 谢谢!我得到了它的工作:D

【问题讨论】:

    标签: php wordpress themes textinput


    【解决方案1】:
    ----------
    <?php
    /**
     * Theme Options
     *
     * @package WordPress
     * 
     */
    
    
    /* Write the name and the variable_name in which you want to store the data using theme options in the array $options_array */
    $options_array = array(
                'Facebook URL'=>'facebook_url',
            );
    
    
    function my_theme_options_init() {
    
        // If we have no options in the database, let's add them now.
        if ( false === my_theme_options() )
            add_option( 'my_theme_options', my_default_theme_options() );
    
        register_setting(
            'my_options',       // Options group, see settings_fields() call in theme_options_render_page()
            'my_theme_options' // Database option, see my_theme_options()
        );
        global $options_array;
        foreach ($options_array as $key=>$option ){
            register_setting( 'my_options', $option);
        }
    
    }
    
    
    add_action( 'admin_init', 'my_theme_options_init' );
    
    
    
    
    
    
    function my_theme_options_add_page() {
        $theme_page = add_theme_page(
            'myers themeing',   // Name of page
            'myers themeing',   // Label in menu
            'edit_theme_options',                    // Capability required
            'my_options',                         // Menu slug, used to uniquely identify the page
            'my_theme_options_render_page' // Function that renders the options page
        );
    
        if ( ! $theme_page )
            return;
    
    
    }
    add_action( 'admin_menu', 'my_theme_options_add_page' );
    
    
    
    function my_default_schemes() {
        $default_array = array('value' => 'Default_theme',
                'label' => __( 'Default_theme', 'my' ),
                'thumbnail' => get_template_directory_uri() . '/inc/images/my.png' 
                );
        global $options_array;
        foreach ($options_array as $key=>$option ){
            $default_array[$option] =' ';
        }
    
        $default_scheme_options = array(
            'Default_theme' => $default_array,
        );
    
        return apply_filters( 'my_default_schemes', $default_scheme_options );
    }
    
    
    
    function my_default_theme_options() {
        $default_theme_options = array( 'default_scheme' => 'Default_theme' );
        global $options_array;  
        foreach ($options_array as $key=>$option ){
            $default_theme_options[$option] = my_default( $option,'Default_theme' );
        }   
    
        return apply_filters( 'my_default_theme_options', $default_theme_options );
    }
    
    
    
    function my_default( $option ,$default_scheme = null ) {
        if ( null === $default_scheme ) {
            $options = my_theme_options();
            $default_scheme = $options['default_scheme'];
        }
    
        $default_schemes = my_default_schemes();
        if ( ! isset( $default_schemes[ $default_scheme ] ) )
            return false;
    
        return $default_schemes[ $default_scheme ][$option];
    }
    
    
    
    function my_theme_options() {
        return get_option( 'my_theme_options', my_default_theme_options() );
    }
    
    function my_theme_options_render_page() {
        ?>
        <div class="wrap">
            <?php screen_icon(); ?>
            <h2><?php printf( __( '%s Theme Options', 'my' ), get_current_theme() ); ?></h2>
            <?php settings_errors(); ?>
            <hr>
    
            <div id="theme_option_main">
    
                <h2>myers themeing</h2>
                <form method="post" enctype="multipart/form-data" action="options.php">
                <?php
    
                    settings_fields( 'my_options' );
                    $options = my_theme_options();
                    $default_options = my_default_theme_options();
                    global $options_array;  
                    foreach ($options_array as $key=>$option ){
                        do_settings_sections($option);
                    }   
    
                ?>
    
                    <table class="form-table">
                        <tr>
                            <th scope="row"><?php _e( "(Use ' http:// ' for Hyperlinks)", 'my' ); ?></th>
                            <td>
                                <fieldset>
                                </fieldset>
                            </td>
                        </tr>
                        <?php   
                                foreach ($options_array as $key=>$option ){
    
                             ?>
                            <tr>
                                <th scope="row"><?php _e( $key, 'my' ); ?></th>
                                <td>
                                    <fieldset>
                                        <legend class="screen-reader-text"><span><?php _e( $key, 'my' ); ?></span></legend>
                                        <input type="text" name="<?php echo $option ?>" id="<?php echo $option ?>" class="large-text" value="<?php echo get_option($option); ?>"></input>
                                    </fieldset>
                                </td>
                            </tr>
    
                        <?php } ?>
    
    
                    </table>
                    <?php submit_button(); ?>
                </form>
    
    
    
            </div>
        </div>  
    
        <?php
    }
    ?>
    

    复制代码并创建一个新的主题选项文件。在文件中的 options 数组中添加名称和 variable_name 即可。

    不要忘记在你的functions.php中链接这个文件 要链接它,只需在您的 functions.php 中添加以下代码

    require( dirname( __FILE__ ) . '/inc/your_file_name.php' );
    

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多