I have written code for creating theme options but default values not getting displayed in text field Also settings saved message also not getting displayed Thank you

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

I have wriiten above code. Please help Thank you

有帮助吗?

解决方案

I tested your code and the default values are being displayed correctly.
Doing a var_dump($_REQUEST); shows what's the problem with the update message, we have to check simpy for $_REQUEST['settings-updated'].

You can remove the is_admin() checking, as admin_init and admin_menu only run on admin.

The validation function should be something like this, validating each input field and returning a clean array with sanitized values:

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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top