Amongst other features, WordPress 2.7 includes numerous improvements around the security and flexibility of user options for third-party themes and plug-ins. I’ve been trying to add an options page to my theme but I couldn’t find any clear, up-to-date advice on how to go about it for this particular version. Here’s how I did it.
I’m going to show you how to create an options page for your theme users that they can access from the Appearance menu in their WordPress 2.7 dashboard. For theme options, this is the traditional, most visible, and hence user-friendly method in my opinion. In this version of WordPress, it is also possible to add custom options to any of the native settings pages, but this is not covered here.
What’s more, we’re going to heed the WordPress team’s warnings about existing option methods being deprecated in future and use the new settings API to make ours forward-compatible and consequently more secure. Hopefully, it will also mean one less thing to fix when the next major version drops.
Finally, we’ll learn how to retrieve these options, but because this has not changed since version 2.6, these are already well-documented elsewhere. There are also so many ways to implement them that we would need at least another post to do them justice.
To write this post, I pieced together the code from the following pages on WordPress.org:
As with earlier versions of WordPress, there are two main steps:
- Amend your theme’s functions.php file to automatically generate an options page for your theme users’ dashboard containing the options you specify.
- Add code to your other theme files that can make use of the options saved in this page.
Step 1 – creating the options page
Add the options page to the dashboard
Open up the functions.php file in your theme’s folder. If there isn’t one, create it and insert:
<?php // paste all the code between these tags ?>
To begin with, we need to tell WordPress to add the options page to the dashboard whenever it is displayed. We can use a native WordPress command to do this. Add this to your functions.php file:
add_action('admin_menu', 'mytheme_menu');
The only thing you can / might want to change is 'mytheme_menu' which will be the name of a function we’ll create now. In 2.6, this would be the page building function, but in 2.7 it will call an intermediate function that adds the new menu placement and security features first.
Register the options you intend to use
Add the following code above the previous line.
function mytheme_menu () {
add_theme_page (
'My theme options', 'My theme options',
8, __FILE__, 'mytheme_options_page');
register_setting (
'mytheme_options', 'mytheme_option1', '');
register_setting (
'mytheme_options', 'mytheme_option2', '');
}
Here, because we are using 'add_theme_page', our options page is positioned in the Appearance menu – alternatives are available. Then, the options are registered for security. You can add more of these if you wish. If you changed 'mytheme_menu' in the first line of code, make sure you change the name of this function accordingly. For more explanation about the variables, search around, but for now here are the ones you need to know about:
The two ‘mytheme_option’ values are the title of your new options menu item and options page in the dashboard. The 'mytheme_options_page' value is the name of the function we are about to create that will build your options page. The 'mytheme_options' value is the group name for your options, which in this example are named 'mytheme_option1' and 'mytheme_option2'. They don’t have to be numbered sequentially or anything, just named differently. The variables after these option names can contain default values, but we’ll leave them blank.
Create the options page
Now we’ll add the function that builds the page. It’s based on the code supplied for 2.6 (at time of writing) on this page, but it’s a bit simpler. Again, paste it in above the code you’ve added so far.
function mytheme_options_page () { ?>
<div class="wrap">
<h2>My theme options</h2>
<form method="post" action="options.php">
<table class="form-table">
<tr valign="top">
<th scope="row">My theme's first option</th>
<td><input type="text" name="mytheme_option1"
value="<?php
echo get_option('mytheme_option1'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">My theme's second option</th>
<td><input type="text" name="mytheme_option2"
value="<?php
echo get_option('mytheme_option2'); ?>" /></td>
</tr>
</table>
<?php settings_fields('mytheme_options'); ?>
<p class="submit">
<input type="submit" class="button-primary"
value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php }
If you know your HTML, most of this will make sense, and you’ll be able to remove the line breaks I added to fit my blog. If not, don’t worry, there’s actually very little you have to change here. Aside from the function name at the top, there’s the name="mytheme_option1" attribute in each input tag to change along with the accompanying php echo get_option('mytheme_option1'), which retrieves the value if it already exists.
If you’re using more options, select all the code from <tr valign="top"> to the </tr> and copy it as many times as you need. Don’t forget to change the name of each option and register them in the other function. Please note that you aren’t limited to text fields – you should be able to track down instructions for adding checkboxes and dropdown menus, etc.
Then, save and upload your functions.php file and log in to the dashboard to check out your new options page! Enter some values, save them and let’s get on with the second step: retrieving them.
Step 2 – retrieving your options
This is the easy part, because you’ve already encountered the command that retrieves your option values in the preceding code for the options page.
get_option('mytheme_option1');
There seems to be plenty of advice on the web about how to implement your options once you have retrieved them, so I’ll leave you with the most basic example I can think of. Let’s assume mytheme_option1 contains a colour value (eg #336699) that you wish to apply to an HTML div container in one of your theme files.
<div id="header" style="background-color:
<?php echo get_option('mytheme_option1'); ?>;">
Conclusion
There. We’ve got our theme options page set up in the dashboard, have registered our options for security and forwards-compatibility, and can retrieve those values elsewhere in our theme. I’ll leave you to work out how to add different form elements to your options page and how best to implement its values in your theme.
7 comments
Zack
Jul 1st 2009, 10:26 am
Thanks!
Easily the best tutorial I’ve found on the subject, thanks for sharing.
Shivanand Sharma
Aug 18th 2009, 5:02 pm
The code to save the settings is missing. Should be something like
global $themename, $shortname, $options;if ( $_GET['page'] == basename(__FILE__) ) {
if ( 'save' == $_REQUEST['action'] ) {
foreach ($options as $value) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
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=functions.php&saved=true");
die;
} else if( 'reset' == $_REQUEST['action'] ) {
foreach ($options as $value) {
delete_option( $value['id'] ); }
header("Location: themes.php?page=functions.php&reset=true");
die;
}
}
admin
Aug 18th 2009, 7:46 pm
Thanks for your comment Shivanand, but I’m not sure your additions are necessary. The code I outlined above works for me under both WordPress 2.7 and 2.8. I think the register_setting and settings_fields functions take care of this process now.
Shivanand Sharma
Aug 19th 2009, 12:54 am
Sorry about the earlier report. I did get it to work. I started from scratch and used your exact code. Then I customized it to get what I wanted.
Maybe it broke earlier because I was changing the names of the functions and the form while copying your code step by step – could have missed something.
Many thanks for this tutorial. It is simple and not complicated like some of the others out there.
Also could you enlighten on how to save these options as an array. I see many themes creating a single database entry by putting all options into a single array. That will keep things clean. Many thanks again.
admin
Aug 19th 2009, 9:34 am
I’m pleased it’s working and thanks for your kind words. I don’t know much about the array method but it sounds like a good idea. It sounds complicated enough to need another post though. Have you seen this recent post on the subject? Have a look and let me know if you still need help!
Shivanand Sharma
Aug 21st 2009, 5:57 am
Perfect. That’s what I was looking for. Looks like I was using the wrong keywords on Google search.
I also think you should take up this article yourself because of the simple and easy approach you take and also because it will compliment this article perfectly. Thanks again.
Tim
Jun 23rd 2010, 5:41 pm
I encountered a problem with the Theme Options page not loading under a localhost WAMP server running WordPress 3.0. Changing the __FILE__ value in the example mytheme_menu function I supplied above to an empty ” or descriptive string such as ‘my_theme_options’ got it working again.
1 pingback