0

我第一次尝试创建 Wordpress 主题选项页面,并且一直在学习一些教程,但未能成功实现我想要的。

该页面的显示和格式都很好,但它只是没有保存选项或确认选项已保存。

到目前为止,这是我的代码(主要是从http://www.onedesigns.com/tutorials/how-to-create-a-wordpress-theme-options-page借来的):

<?php 


$sa_options = array(
    'primary_colour' => '#4385B2',
    'secondary_colour' => '#d93600',
    'header_image' => '',
    'header_colour' => '#EFEFEF',
    'logo_image' => '',
    'social_links' => ''
);

if ( is_admin() ) :

add_action( 'admin_enqueue_scripts', 'mw_enqueue_color_picker' );
function mw_enqueue_color_picker( $hook_suffix ) {
    // first check that $hook_suffix is appropriate for your admin page
    wp_enqueue_style( 'wp-color-picker' );
    wp_enqueue_script( 'my-script-handle', content_url('themes/towntown/options/js/admin.js', __FILE__ ), array( 'wp-color-picker' ), false, true );
}

function sa_register_settings() {
    register_setting( 'sa_theme_options', 'sa_options', 'sa_validate_options' );
}

add_action( 'admin_init', 'sa_register_settings' );



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

function sa_theme_options() {
    add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'sa_theme_options_page' );
}
add_action( 'admin_menu', 'sa_theme_options' );

function sa_theme_options_page() {
    global $sa_options;

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

    <div class="wrap">

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

    <?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
    <div><p><strong><?php '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( 'sa_options', $sa_options ); ?>

    <?php settings_fields( 'sa_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><!-- Grab a hot cup of coffee, yes we're using tables! -->

    <tr valign="top"><th scope="row"><label for="primary_colour">Primary Colour:</label></th>
    <td>
    <input id="primary_colour" name="sa_options[primary_colour]" type="text" value="<?php  echo $settings['primary_colour']; ?>" class="colour-field" />
    </td>
    </tr>

    <tr valign="top"><th scope="row"><label for="secondary_colour">Secondary Colour: </label></th>
    <td>
    <input id="secondary_colour" name="sa_options[secondary_colour]" type="text" value="<?php echo $settings['secondary_colour']; ?>" class="colour-field" />
    </td>
    </tr>

    <tr valign="top"><th scope="row"><label for="header_image">Header Image: </label></th>
    <td>
    <input id="header_image" name="sa_options[header_image]" type="text" value="<?php echo $settings['header_image']; ?>" />
    </td>
    </tr>

    <tr valign="top"><th scope="row"><label for="header_colour">Header Background Colour: </label></th>
    <td>
    <input id="header_colour" name="sa_options[header_colour]" type="text" value="<?php echo $settings['header_colour']; ?>" class="colour-field" />
    </td>
    </tr>

    <tr valign="top"><th scope="row"><label for="logo_image">Logo: </label></th>
    <td>
    <input id="logo_image" name="sa_options[logo_image]" type="text" value="<?php echo $settings['logo_image']; ?>" />
    </td>
    </tr>

    <tr valign="top"><th scope="row"><label for="social_links">Social Links: </label></th>
    <td>
    <input id="social_links" name="sa_options[social_links]" type="text" value="<?php echo $settings['social_links']; ?>" />
    </td>
    </tr>
    </table>

    <p><input type="submit" value="Save Options" /></p>

    </form>

    </div>

    <?php

    function sa_validate_options( $input ) {
        global $sa_options;

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

        // We strip all tags from the text field, to avoid vulnerablilties like XSS
        $input['primary_colour'] = wp_filter_nohtml_kses( $input['primary_colour'] );
        $input['secondary_colour'] = wp_filter_post_kses( $input['secondary_colour'] );
        $input['header_image'] = wp_filter_nohtml_kses( $input['header_image'] );
        $input['header_colour'] = wp_filter_post_kses( $input['header_colour'] );
        $input['logo_image'] = wp_filter_nohtml_kses( $input['logo_image'] );
        $input['social_links'] = wp_filter_post_kses( $input['social_links'] );

        return $input;
    }

}

endif;  // EndIf is_admin()

提到我在输入框中不断出现错误可能很有用 -Notice: Uninitialized string offset: 0 in /var/www/vhosts/liveworkinvest.com/town/wp-content/themes/towntown/options/theme-options.php on line 89

如何更正错误并保存更改?

4

0 回答 0