默认值未显示在输入字段wordpress中

时间:2014-04-26 14:39:46

标签: php html wordpress wordpress-theming

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

    <?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()

我已经对上面的代码进行了修改。 请帮忙 谢谢

1 个答案:

答案 0 :(得分:1)

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

您可以删除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;
}