0

I'm currently working on making an options panel for a theme (theme-options.php) on my localhost. Every thing was going well until I tried testing the Save button. After clicking it, I got the message that the options were saved but nothing actually saved. Not sure if this matters, but theme-options.php is located in a folder named "options" in the theme's home directory. Here is the current function I'm using for saving:

//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');

I'm following a tutorial to make this panel (with a couple modifications to my needs) and everything looks the same as in the tutorial. Looking through it with the php knowledge I have, I see nothing wrong either. Here's the entire theme-options.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
}
?>

What exactly am I doing wrong for the save button not to work? Any help is greatly appreciated.

EDIT: I changed the 'select' and 'text' cases to the following (which still isn't working for me):

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;

I'm going to continue working on this but some help would still be much appreciated.

4
  • May i suggest you edit out the profanity from your sample code.
    – t31os
    Commented Jul 1, 2013 at 11:34
  • Okay all edited out, sorry. I also made a few changes to my code (shown above) but it's still not working.
    – Connor
    Commented Jul 1, 2013 at 19:06
  • this is a bit of an odd way to create a theme options system, perhaps the tutorial you followed predates the Settings API, which simplifies a lot of the details.
    – Milo
    Commented Jul 1, 2013 at 19:35
  • Yea I was just looking through a tutorial that used that now: wp.tutsplus.com/tutorials/creative-coding/… But yea I'm learning from the Build Your Own Wicked Wordpress Themes book from SitePoint. Any reason why this doesn't work though?
    – Connor
    Commented Jul 1, 2013 at 19:37

1 Answer 1

0

I've started rewriting everything using the Settings API and everything is working well now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.