主题选项的保存按钮不起作用

时间:2013-06-29 作者:Connor

我目前正在为本地主机上的主题(theme options.php)制作一个选项面板。在我尝试测试保存按钮之前,一切都很顺利。单击后,我得到消息,选项已保存,但实际未保存任何内容。不确定这是否重要,但主题选项。php位于主题主目录中名为“options”的文件夹中。以下是我用于保存的当前函数:

//add options page
function fround_add_admin(){
    global $themename, $shortname, $options;

    if ( $_GET[\'page\'] == basename(__FILE__) ){

        if ( \'save\' == $_REQUEST[\'action\'] ) {
            //protect against request forgery
            check_admin_referer(\'theme-save\');

            //save the options
            foreach ($options as $value) {
                if( isset( $_REQUEST[ $value[\'id\'] ] ) ) {
                    update_option( $value[\'id\'], $_REQUEST[ $value[\'id\'] ] );
                } else {
                    delete_option( $value[\'id\'] );
                }
            }

            header("Location: themes.php?page=theme-options.php&saved=true");
            die;

        } else if ( \'reset\' == $_REQUEST[\'action\'] ) {
            //protect against request forgery
            check_admin_referer(\'theme-reset\');
            //delete the options
            foreach ($option as $value) {
                delete_option( $value[\'id\'] );
            }

            header("Location: themes.php?page=theme-options.php&reset=true");
            die;
        }
    }
    add_theme_page($themename." Options", "$themename Options", \'edit_themes\', basename(__FILE__), \'fround_admin\');
}
add_action(\'admin_menu\' , \'fround_add_admin\');
我正在按照教程制作这个面板(对我的需求进行了一些修改),所有内容看起来都与教程中的一样。用我掌握的php知识来看,我也没有发现任何错误。以下是整个主题选项。php:

<?php

//theme vars
$themename = "f Round";
$shortname = "fround";
$options = array();

function theme_options(){
    global $themename, $shortname, $options;

    $options = array (

        array("name" => "General Settings",
            "type" => "section"),
        array("type" => "open"),

        array("name" => "Type of Logo",
            "desc" => "Select your logo type ( Image or Text )",
            "id" => $shortname."_type_of_logo",
            "type" => "select",
            "options" => array("text", "logo"),
            "std" => "text"),

        array("name" => "Logo Upload",
            "desc" => "Upload images using the native media uploader, or define the URL directly",
            "id" => $shortname."_logo_upload",
            "type" => "imageupload",
            "std" => ""),

        array("name" => "Logo Text",
            "desc" => "Enter text for logo",
            "id" => $shortname."_logo_text",
            "type" => "text",
            "std" => ""),

        array("name" => "Logo Slogan",
            "desc" => "Enter text for logo slogan",
            "id" => $shortname."_logo_slogan",
            "type" => "text",
            "std" => ""),

        array("name" => "Custom Favicon",
            "desc" => "Upload a 16px x 16px Png/Gif image that will represent your website\'s favicon",
            "id" => $shortname."_favicon_upload",
            "type" => "imageupload",
            "std" => ""),

        array("type" => "close"),
        array("name" => "Styling Options",
            "type" => "section"),
        array("type" => "open"),

        array("name" => "Background Color",
            "desc" => "Pick a color for the background",
            "id" => $shortname."_background_color",
            "type" => "color",
            "std" => "#ffffff"),

        array("name" => "Body Color",
            "desc" => "Pick a color for the body",
            "id" => $shortname."_body_color",
            "type" => "color",
            "std" => "#ffffff"),

        array("type" => "close"),
        array("name" => "Footer Options",
            "type" => "section"),
        array("type" => "open"),

        array("name" => "Copyright Text",
            "desc" => "Enter text for copyright in footer (if empty it will be removed)",
            "id" => $shortname."_footer_text",
            "type" => "text",
            "std" => "Copyright TestSite.com"),

        array("type" => "close")

    );
}
add_action(\'init\', \'theme_options\');

//add options page
function fround_add_admin(){
    global $themename, $shortname, $options;

    if ( $_GET[\'page\'] == basename(__FILE__) ){

        if ( \'save\' == $_REQUEST[\'action\'] ) {
            //protect against request forgery
            check_admin_referer(\'theme-save\');

            //save the options
            foreach ($options as $value) {
                if( isset( $_REQUEST[ $value[\'id\'] ] ) ) {
                    update_option( $value[\'id\'], $_REQUEST[ $value[\'id\'] ] );
                } else {
                    delete_option( $value[\'id\'] );
                }
            }

            header("Location: themes.php?page=theme-options.php&saved=true");
            die;

        } else if ( \'reset\' == $_REQUEST[\'action\'] ) {
            //protect against request forgery
            check_admin_referer(\'theme-reset\');
            //delete the options
            foreach ($option as $value) {
                delete_option( $value[\'id\'] );
            }

            header("Location: themes.php?page=theme-options.php&reset=true");
            die;
        }
    }
    add_theme_page($themename." Options", "$themename Options", \'edit_themes\', basename(__FILE__), \'fround_admin\');
}
add_action(\'admin_menu\' , \'fround_add_admin\');

//main function
function fround_admin() {
    global $themename, $shortname, $options;

    //saved or reset messages
    if ( $_REQUEST[\'saved\'] ) echo \'<div id="message" class="updated fade"><p><strong>\'.$themename.\' settings saved.</strong></p></div>\';
    if ( $_REQUEST[\'reset\'] ) echo \'<div id="message" class="updated fade"><p><strong>\'.$themename.\' settings reset.</strong></p></div>\';

    //form
    ?>
    <header>
        <h1><?php echo $themename; ?></h1>
    </header>

    <aside>
        <nav>
            <ul>
            <?php
            foreach ($options as $value){
                switch ( $value[\'type\'] ){
                    case \'section\':
                        echo \'<li><a>\'.$value[\'name\'].\'</li></a>\';
                        break;
                }
            }
            ?>
            </ul>
        </nav>
    </aside>

    <form method="post">
        <?php wp_nonce_field(\'theme-save\'); ?>

        <main>
        <?php
            foreach ($options as $value){
                switch ( $value[\'type\'] ){
                    case \'section\':
                        ?>
                        <section name="<?php echo $value[\'name\'];?>">
                            <h3><?php echo $value[\'name\'];?></h3>
                        <?php
                        break;

                    case \'open\':
                        break;

                    case \'close\':
                        ?>
                        </section>
                        <?php
                        break;

                    case \'select\':
                        ?>
                        <div id="<?php echo $value[\'id\']; ?>" class="option <?php echo $value[\'type\']; ?>">
                            <select>
                                <?php
                                foreach ($value[\'options\'] as $option){
                                    echo \'<option value="\'.$option.\'">\'.$option.\'</option>\';
                                }
                                ?>
                            </select>

                            <p><?php echo $value[\'desc\']; ?></p>
                        </div>
                        <?php
                        break;

                    case \'text\':
                        ?>
                        <div id="<?php echo $value[\'id\']; ?>" class="option <?php echo $value[\'type\']; ?>">
                            <input type="text" value="<?php echo $value[\'std\']; ?>">

                            <p><?php echo $value[\'desc\']; ?></p>
                        </div>
                        <?php
                        break;

                    case \'imageupload\':
                        ?>
                        <div id="<?php echo $value[\'id\']; ?>" class="option <?php echo $value[\'type\']; ?>">

                            <p><?php echo $value[\'desc\']; ?></p>
                        </div>
                        <?php
                        break;

                    case \'color\':
                        ?>
                        <div id="<?php echo $value[\'id\']; ?>" class="option <?php echo $value[\'type\']; ?>">

                            <p><?php echo $value[\'desc\']; ?></p>
                        </div>
                        <?php
                        break;
                }
            }
        ?>
        </main>

        <p class="submit">
            <input name="save" type="submit" value="Save changes" class="button-primary" />
            <input type="hidden" name="action" value="save" />
        </p>
    </form>

    <form method="post">
        <?php wp_nonce_field(\'theme-reset\'); ?>
        <p class="submit">
            <input name="reset" type="submit" value="Reset changes" />
            <input type="hidden" name="action" value="reset" />
        </p>
    </form>
    <?php
}
?>
保存按钮不起作用,我到底做错了什么?非常感谢您的帮助。

编辑:我将“选择”和“文本”案例更改为以下内容(这对我仍然不起作用):

case \'select\':
?>
<div id="<?php echo $value[\'id\']; ?>" class="option <?php echo $value[\'type\']; ?>">
    <select>
        <?php
        foreach ($value[\'options\'] as $key=>$option){
            if ($key == get_option($value[\'id\'], $value[\'std\']) ) {
                $selected = \'selected="selected"\';
            } else{
                $selected = "";
            }
        ?>
        <option value="<?php echo $key; ?>" <?php echo $selected ?>><?php echo $option; ?></option>
        <?php } ?>
    </select>

    <p><?php echo $value[\'desc\']; ?></p>
</div>
<?php
break;

case \'text\':
?>
<div id="<?php echo $value[\'id\']; ?>" class="option <?php echo $value[\'type\']; ?>">
    <input type="text" value="<?php echo stripslashes( get_option($value[\'id\'], $value[\'std\']) ); ?>">

    <p><?php echo $value[\'desc\']; ?></p>
</div>
<?php
break;
我将继续这方面的工作,但仍将非常感谢您的帮助。

1 个回复
最合适的回答,由SO网友:Connor 整理而成

我已经开始使用设置API重写所有内容,现在一切都很好。

结束

相关推荐

Wordpress options text format

Wordpress使用智能引号转换我的主题选项文本。我知道要从帖子等中消除问题:remove_filter(\'the_content\', \'wptexturize\'); remove_filter(\'the_excerpt\', \'wptexturize\'); remove_filter(\'comment_text\', \'wptexturize\'); 但如何在保存到Wordpress options api时消除这种影响?