【问题标题】:default value not getting displayed in input field wordpress默认值未显示在输入字段 wordpress 中
【发布时间】:2014-06-12 07:19:47
【问题描述】:

我已经编写了用于创建主题选项的代码 但默认值未显示在文本字段中 设置保存的消息也没有显示 谢谢

    <?php

// Default options values
$xyz_options = array(
        'footer_copyright' => '&copy; ' . date('Y') . ' ' . get_bloginfo('name'),
        'facebook'=>'http://www.facebook.com/'
    );

if ( is_admin() ) : // Load only if we are viewing an admin page

    function xyz_register_settings() {
    // Register settings and call xyz initiation functions
        register_setting( 'xyz_theme_options', 'xyz_options', 'xyz_validate_options' );
    }

    add_action( 'admin_init', 'xyz_register_settings' );

    function xyz_theme_options() {
    // Add theme options page to the admin menu
        add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'xyz_theme_options_page' );
    }

    add_action( 'admin_menu', 'xyz_theme_options' );

    // Function to generate options page
    function xyz_theme_options_page() {
        global $xyz_options;

        if ( ! isset( $_REQUEST['updated'] ) )
            $_REQUEST['updated'] = false; // This checks whether the form has just been submitted. ?>

        <div class="wrap">

            <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Options' ) . "</h2>";
            // This shows the page's name and an icon if one has been provided ?>

            <?php if ( false !== $_REQUEST['updated'] ) : ?>
            <div class="updated fade"><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
            <?php endif; // If the form has just been submitted, this shows the notification ?>

            <form method="post" action="options.php">

                <?php $settings = get_option( 'xyz_options', $xyz_options ); ?>

                <?php settings_fields( 'xyz_theme_options' );
            /* This function outputs some hidden fields required by the form,
            including a nonce, a unique number used to ensure the form has been submitted from the admin page
            and not somewhere else, very important for security */ ?>

                <table class="form-table">

                    <tr valign="top">
                        <th scope="row">
                            <label for="facebook">Facebook url</label>
                        </th>
                        <td>
                            <input id="facebook" name="xyz_options[facebook]" type="text" value="<?php esc_attr_e($settings['facebook']); ?>" />
                        </td>
                    </tr>

                    <tr valign="top">
                        <th scope="row">
                            <label for="footer_copyright">Footer Text 1</label>
                        </th>
                        <td>
                            <input id="footer_copyright" name="xyz_options[footer_copyright]" type="text" value="<?php  esc_attr_e($settings['footer_copyright']); ?>" />
                        </td>
                    </tr>
                </table>
                <p class="submit">
                    <input type="submit" class="button-primary" value="Save Options" />
                </p>
            </form>

        </div>

    <?php
    }

    function xyz_validate_options( $input ) {
    global $xyz_options;

    $settings = get_option( 'xyz_options', $xyz_options );

    }

endif;  // EndIf is_admin()

我已经编写了上面的代码。 请帮忙 谢谢

【问题讨论】:

标签: php html wordpress wordpress-theming


【解决方案1】:

我测试了您的代码,并且默认值正在正确显示
执行var_dump($_REQUEST); 显示更新消息有什么问题,我们必须检查$_REQUEST['settings-updated'] 的simpy。

您可以删除 is_admin() 检查,因为 admin_initadmin_menu 仅在管理员上运行。

验证函数应该是这样的,验证每个输入字段并返回一个带有净化值的干净数组:

function xyz_validate_options( $input ) {
    $new_input = array();
    if( isset( $input['facebook'] ) )
        $new_input['facebook'] = esc_url( $input['facebook'] );
    if( isset( $input['footer_copyright'] ) )
        $new_input['footer_copyright'] = esc_attr( $input['footer_copyright'] );
    return $new_input;
}

【讨论】:

    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多