Day 4 of Creating an Advanced Theme Options Page in WordPress is here!
Today comes the exciting implementation of our options page that we created yesterday. The first thing to do is to create a new file called get-theme-options.php and save it with your theme files. In this file paste the following:
<?php
//allows the theme to get info from the theme options page
global $options;
foreach ($options as $value) {
if (get_option( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; }
else { $$value['id'] = get_option( $value['id'] ); }
}
?>
What it does is it allows us to reference the options we’ve selected in the backend. We need to include it in every file that you want to use theme options, which we can do with the following:
<?php include (TEMPLATEPATH.'/get-theme-options.php'); ?>
Implementing Feedburner
The very first option on our options page was the Feedburner address, where users could input their Feedburner URL and it’ll become the RSS feed. With get-theme-options at the top of our header.php file, to get the contents of the Feedburner field from the options page, it’s just a simple echo. At this point you’ll need the shortname you entered in part two and combine this with the ID for the Feedburner, in our example with a shortname of blt and the feedburner ID of _feedburner, that makes blt_feedburner. Combine that with an echo and you get this:
<?php echo $blt_feedburner;?>
Add this to the RSS link in the header and you’ll get something like this:
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?>" href="<?php echo $blt_feedburner; ?>" />
The same applies to the other text boxes -
<?php echo $[shortname][ID]; ?>
. For example, for the Analytics code, you’d implement it like so
<?php echo $blt_analytics; ?>
, replacing ‘blt’ with the shortname you selected.
Changing the layout
The next thing we wanted to let users do was enable/disable certain features, with the first option being change the layout. To be able to do this you’ll need to have two (or more) stylesheets, with the different layouts in them. Our example had three layouts to choose from – standard, widened content area and three columns. I’ve created three separate stylesheets, two-column.css, two-column-wide.css and three-column.css. These have been uploaded to /mytheme/css/. With that done, next step is to open up your header.php file and find where the stylesheet is referenced. Below that, with get-theme-options included, copy and paste the following, changing the shortname and ID if necessary:
<?php if ($blt_three_column == "true") {?>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/three-column.css" type="text/css" media="screen,projection" />
<?php }?>
<?php if ($blt_two_column == "true") {?>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/two-column.css" media="screen,projection" />
<?php }?>
<?php if ($blt_two_column_wide == "true") {?>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/two-column-wide.css" type="text/css" media="screen,projection" />
<?php }?>
Essentially all we’re doing here is saying if checkbox x is true, do this:.
implementing checkboxes: show and hide
The same principal applies to showing and hiding ‘stuff’: to hide ‘Homepage Area 1’ I can do so with the following:
<?php //make sure you include get-theme-options
if ($blt_homepage_area_1 == "true") {?>
<!—homepage area one here -->
<?php }?>
And that is all there is to it! You can obviously repeat the principal, so for ‘Homepage Area 2’ you’d need:
<?php //make sure you include get-theme-options
if ($blt_homepage_area_2 == "true") {?>
<!—homepage area two here -->
<?php }?>
The other option you’ve got is if a checkbox is unticked; just change true to false:
<?php //make sure you include get-theme-options
if ($blt_homepage_area_2 == "false") {?>
<!—homepage area two here -->
<?php }?>
Concluding
And with that, we’ve finished implementing all the different options we created – now spend the next five minutes enabling and disabling! We’ve also finished the week’s series, but with still a day to go, not to worry; tomorrow we’ll be wrapping up. See you then!
You should follow me on Twitter
Related Posts









59 Comments - Add Yours!
Hi!
Great tutorial, thank you. But your get-theme-options.php code is missing from the top page. What is the code that goes into get-theme-options.php file to make this all work?
Thanks again.
Ditto comment above/below. Next time you visit, please respect the request not to use your site’s name as the name of your comment.
Hi,
I have been following this and all ok until today.
A bit confused about what actually goes in the get-theme-options.php file?
At the top of this page, is there some code not showing, i.e.
——————————–
Today comes the exciting implementation of our options page that we created yesterday. The first thing to do is to create a new file called get-theme-options.php and save it with your theme files. In this file paste the following:
CODE HERE
——————————–
Thanks for sharing this process – much appreciated.
James
Sorry about that. That was meant to remind me to paste the code! You should see it now.
Hi Alex,
Thanks for your reply. I still cannot see the code after clearing my cache but will check later.
Thanks again for sharing.
Should be there once the cache has refreshed. It if isn’t I’ll post it in a comment.
Hi Alex,
If you could paste it in the comment that would be great. Not sure why, but still unable to see it. Refreshed cache, tried different browsers too…
Thanks again.
Sorry about my first comment, my forms are saved in firefox so it got there automatically. Code is still not visible in the post. I cleared my cache and restarted computer but still no show. Please post it in the comment.
Thanks.
.-= Damir´s last blog ..Copper Black – Wordpress Theme =-.
Not to worry. The code you want is below. I’ve cleared the site’s cache – is it there now?
<?php//allows the theme to get info from the theme options page
global $options;
foreach ($options as $value) {
if (get_option( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; }
else { $$value['id'] = get_option( $value['id'] ); }
}
?>
Hi Alex,
Thanks – yes it can be seen within the post now as well.
Thanks, this is a great tutorial!
Glad you enjoyed it!
Thank you for this great tutorial, it is just what I was looking for. I get the theme options page under theme links as it should but I do get an error when trying to save changes “You do not have sufficient permissions to access this page.” I’m using wp 2.8.4 What could be causing the error?
Thanks again.
.-= Damir´s last blog ..Copper Black – Wordpress Theme =-.
Erm. Hard to diagnose without seeing what you have and haven’t done, although I do recall a couple of similar errors when I was writing the tutorial. What I’d suggest doing is grab yourself a copy of Biblioteca and compare the options page code in that to what you’ve got. The options page in Biblioteca is located in /library/functions/
Thanks for the quick reply, I sort of figured it out. First I was calling the get-theme-options.php inside my existing functions.php (with rest of your code merged inside) using “include_once (TEMPLATEPATH . ‘/….) so I don’t have to use ” include get-theme-options.php…” in every template file that I wanted to use theme options but existing functions file already has “global $options; foreach ($options as $value)…)” code for footer html option page and it was giving me different errors. But using it as you have suggested it works so thank you again for this great tutorial.
.-= Damir´s last blog ..Copper Black – Wordpress Theme =-.
No problem. Do let me see the results!
Wow. I must have really great timing. I was just looking to incorporate and options page and stumbled on your series. Thanks for the great tutorial.
I have one question about the implementation phase here. For including the get_theme_options.php — where do you recommend. I tried loading it in header.php and got fairly large error. Any tips?
Thanks again!
.-= Joel G Goodman´s last blog ..NaNoWriMo 2009 =-.
You need to put it in every single template file that calls the theme options, at the top of the file.
So it’s not enough to include in the header.php file that gets called on every page. I’ll play around and see what happens.
thanks again!
.-= Joel G Goodman´s last blog ..NaNoWriMo 2009 =-.
Great stuff exactly at the right time
Just one question: When I put the Analytics code in the optionspage field and then look in the sourcecode of the page everything seems to be fine.
So I copied this options area and wanted to use it for an AdSense code but this doesn´t work because double quotes are not correctly interpreted. This is part of the output for the Analytics code:
and this for the AdSense code:
Any idea why this can be and how to solve it?
Sorry the code was strippped out…here again:
and this for the AdSense code:
Any idea why this can be and how to solve it?
.-= Michael Oeser´s last blog ..WordPress vor Angriffen, Viren und Spam schützen – rechtlich unbedenkliche Methoden und Werkzeuge =-.
Hey Michael,
Your code has gone again! You’ll need to wrap it with
and encode it with this tool. Then it should work!Hi!
Great post. I get a “You do not have sufficient permissions to access this page” message when I try to save the options. Any ideas as how to fix the problem?
thanks.
Hey Christian,
I might be jumping to conclusions here, but it looks like you’ve Googled theme options pages, stumbled upon this site and found it doesn’t work. I apologise if this isn’t the case, but it seems you implemented it for a client’s project, found it doesn’t work and so thought you’d leave a comment. I of course am then obliged to spend my time replying to your comment to make it all work and you’ll then send it to your client and get paid lots. That’s the impression I’ve got anyway.
I’ll give you the same advice as above – compare your code to Biblioteca’s and see what differences you can find.
Hi Cristian,
If you work out what is causing this issue can you paste your findings here? I have tried and cannot find the problem.
Hope you are more successful…
Thanks
Hi Cristian,
I worked out that you need to change these to suit your file path. I changed mine to the following:
header(“Location: themes.php?page=functions.php&reset=true”);
Note the change to functions.php…
Thanks for sharing that James. In retrospective I was a tad harsh on Christian, but it’s always nice to see a community helping each other out. It’s comments like this I like
Ouch! You hit the nail on the head. This is client work. But aren’t we all looking for advice and hints for the advancement of our own work? And I’m not demanding anything, just made an attempt to see if your knowledge base had a reference of this scenario. But I guess not. Can’t blame me for trying. Keep up the good work.
It was more the way you did it than anything else. I’ve happily helped countless readers before, but I just can’t stand the
kind of comment. Perhaps I was a bit harsh. Sorry about that. Luckily a lovely reader has replied to your comment with a solution.
Thanks for the solution to that problem, and thanks to the Author for this helpful tutorial…
OK, I´ll try it again:
This is the AdSense code that doesn´t work:
And that´s the Analytics code which does work:
I added some blanks and hope it shows up now.
.-= Michael Oeser´s last blog ..Vorschau auf BranfordMagazine 4.0 =-.
Still not displaying; I’m just replying to your email.
Great tutorial, I’ve learned a lot.
I am struggling, however, to understand how you allow a structural change in layout ONLY through css. For instance, if I want to offer two columns layout and three columns layout I would have to alter the html and not merely the css.
Right now I’m using Tim Thumb to resize images (for a gallery theme) according to what layout the user selects in the admin section. And the only way I can figure to do this is put the Tim Thumb code in an if/else statement, but I can’t figure out how to do this.
Is there a simpler way to allow a structural change in the theme such that a user can simply select it from the backend? I see that you show an example of a three column layout, but I don’t know how you could do that only through css.
Thanks for your help.
.-= Justin´s last blog ..project 365 =-.
In short, you can’t. You need to do and if statement and include a second sidebar the right way. Download a copy of Biblioteca for an example of how to do this.
With a single if statement, you can include a stylesheet and also a second sidebar, which I think is what you’re trying to do. Again, Biblioteca does this.
Justin – A couple things come to mind. You’d need to make sure your markup is clean enough to handle this, but you separate the classes you need to be variable widths/heights in different places into new stylesheets. Then based on the theme options selection you would load that stylesheet.
So, left column, large body is checked, then load style1.css (code would look just like Alex’s last example in this post). I’m not sure you’re having php/javascript issues as much as understanding the power of CSS and clean, semantically rich markup.
Sorry I cant figure it out when i’m saving or reset it gives me an error You do not have sufficient permissions to access this page. I put that get-option.php codes in to my functions.php and was tried with everything but still i can’t fix the problem can you pls tell me why this is happening.
I’m having a problem with
I get this error
Warning: Cannot modify header information – headers already sent by (output started at /var/www/wordpress/wp-content/themes/theme/functions.php:7) in /var/www/wordpress/wp-content/themes/theme/library/theme-options.php on line 83
Line 7 in functions.php is the previous php code,
and line 83 is;
header(“Location: themes.php?page=theme-options.php&saved=true”);
line 84; die;
Hope you can help. regards, Benjamin
I forgot to tell you this happens when I try to save the options
Thanks, Benjamin
See the above comments for a fix. You’ll need to change the page depending where you’ve put the file:
header(“Location: themes.php?page=theme-options.php&saved=true”);
That wasn’t it I had to save the file in UTF-8 and that fixed it
Regards, Benjamin
One of the best WP tutorial I have read. Keep up the good work. Very well written and easy to follow.
Thanks
Quick question…
When doing the show/hide implementation, do you only have to do a ‘true’ state? For example, if you only do a ‘true’ state and what happens if true, does it just automatically hide it if the box is unchecked?
Thanks!
Alex
Hi Alex! Great tutorial! I’m wondering if you know how to list the authors on this options page? I’d like to feature an author on the home page and let the editor choose it.. any clue?
thanks!
Joaquín
Not of the top of my head, I’m afraid. Take a look for some other themes that do the same and see if you can learn something from the way they do it.
Hi there – first off this is a great tutorial and I am VERY grateful for the time you’ve spent on this. I am a novice – you’ve saved me a ton of time.
One quick question – when I click “save changes” the updates go live, but I’m left with a blank page. I’m sure this is my mistake, somewhere, but could you point me to where you think the issue might be please?
thanks again.
Steve
UPDATE: All functions that have a “save” or “submit”, or even logout now go to a blank page. What did I miss?
Thanks
Steve
UPDATE 2: Scratch all of that. I found that if you leave a space after ?> at the beginning or end of the code it blows it out of the water. Sorry for the multiple posts – I now have a fully functional options page again!
Steve
Good to hear
Really nice tutorial. I’m trying to understand it from past few days.
It is working well. I’ve one confusion. How can I change the css style.
I’ve created a style.php file where I have pasted a necessary code and defined text/css type. The only problem is I’m unable to change the style.
Pls help me out with this.
I solved it. I forgot to include style.php file in header file.
One more doubt how can I make a tabbed menu for admin panel?
Like in “General” tab there is an option for general settings. “Header” tab there is a setting for header area and so on…..
Great stuff works as it should and well explained i’m gonna use this for my upcoming designs thanks
Great tutorial. I do have one question. I am trying to change the background color of the blog using custom options page. I am trying to pass the value of the background color variable to the style.php file. I added a code snippet at the top of the file to include the options. However the CSS is not picking up the value. As you can see the file is being converted to .CSS via code.
body { background: ; background-image:url(images/bg.jpg); color: #000; font-family: Arial, Helvetica, sans-serif; height:100%}
Any advise appreciated.
James
This is what I have in the header in the above post (got cut of)
header(“Content-type: text/css”);
and
include (TEMPLATEPATH.’/get-theme-options.php’);
Does get-theme-options.php exist?
Yes it exists.
I seem to be able to read the values in all other php files except for when I insert the code in the style.php and have the header(“Content-type: text/css”); code. Then it just does not recognize? Any suggestions please?
And if you don’t mind, can you please tell me how I can set up a field for uploading a picture file for the blog logo from the Options page? I’ve looked for it everywhere but I can’t seem to find an answer.
One Trackback