Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I try to save an option from the frontend but i can't find yet how to do it.
So, the idea is to give the blog owner a simple form with a dropdown that can select a different stylesheet.css. I have build it for the wp-admin area and it works fine, but i want to do the same in the frontend,

i have tried to include the wp-admin/options.php in the header but nothing, just errors,
i post the form to "wp-admin/options.php" but it redirect me in the wp-admin/options page.

here is the frontend form:

<form id="save-theme" name="save-theme" action="/wp-admin/options.php" method="post">
<select>
    <option>Select Theme</option>
    <option>Red</option>
    <option>Dark</option>
    <option>White</option>
</select>
<button name="update-options" type="submit">Save</button>
<?php wp_nonce_field('update-options'); ?>
</form>

thanks a lot!

share|improve this question
    
Ehm, how do you identify users? In the backend there is a login, but in the frontend, there is none. Should users set the option for the whole site? –  hakre Apr 3 '11 at 10:13
    
@hakre, i have frontend login/register pages for the users and i give access to the form below only to admins, yes i need to change the whole site. –  Philip Apr 3 '11 at 11:16
add comment

2 Answers

up vote 2 down vote accepted

You Do Not want to post /wp-admin/options.php from the front end , thats a bad idea and can cause problems.

To updated Options from the frontend simply use update_option() and make sure you verify correctly. here is an example using your code with minor fixes:

<?php 
    if (isset($_POST['stylesheet']) && isset($_POST['action']) && $_POST['action'] == "update_theme"){
        if (wp_verify_nonce($_POST['theme_front_end'],'update-options')){ 
            update_option('my_theme-style',$_POST['stylesheet']);
        }else{
        ?><div class="error"><?php echo 'update failed'; ?></div><?php}
    }
?>


<form id="save-theme" name="save-theme" action="" method="post">
<select name="stylesheet">
<?php $selected = get_option('my_theme-style');
    <option>Select Theme</option>
    <option value="1" <?php if ($selected == 1) echo 'selected="selected"'; ?>>Red</option>
    <option value="2" <?php if ($selected == 2) echo 'selected="selected"'; ?>>Dark</option>
    <option value="3" <?php if ($selected == 3) echo 'selected="selected"'; ?>>White</option>
</select>
<?php wp_nonce_field('update-options','theme_front_end'); ?>
<input type="hidden" name="action" value="update_theme">
<input type="submit" name="update-options" value="Save">
</form>

Now this assumes that the option key or name is my_theme-style.

share|improve this answer
    
thanks a lot @Bainternet!!! i found a solution to my question, but your way is better! –  Philip Apr 3 '11 at 14:18
    
Glad i could help –  Bainternet Apr 3 '11 at 14:45
    
Isn't that code always going to produce a update failed message whenever the stylesheet and action keys are set? –  t31os Apr 5 '11 at 15:30
    
@t31os: yep, my bad, i added an else clause. Thanks –  Bainternet Apr 5 '11 at 16:29
    
This may be obvious to most people, but it should be <?php if ($selected == 1) echo 'selected="selected" I think –  Dalton Feb 4 '12 at 2:07
show 1 more comment

Maybe Theme Switcher Reloaded (Wordpress Plugin) contains enough information on how that could be done?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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