Create An Awesome WordPress Theme Options Page (part 2 – implementation)

Posted on 01. Jun, 2009 by Alex Denning in Theme Options Page

Continuing on the ‘Create An Awesome WordPress Theme Options Page’ series, and following directly on from part one (ie you need to read it first), we’ll be implementing the options we’ve created into our hypothetical WordPress theme, Hexadecimal.

Update 21.08.09: this tutorial series has been updated with a new series – check out “Create An Advanced Theme Options Page in WordPress“.

Changing colour

changingcolour

The first thing we did in part one was let the user change the site’s colour scheme, as the picture above shows. Now we’re going to implement it. First up, open up your header.php file, and add the following code:

  1. <?php
  2. global $options;
  3. foreach ($options as $value) {
  4. if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
  5. }
  6. ?>

I’ll say this now to avoid hours of wasted time later – you must put the code above at the start of all the files which you add options to.

Right. Anyway. To change the colours we’re going to need an additional stylesheet. Lets call it extrastyle.php. Once you’ve created the file, upload it to your theme folder. Next thing to do is add the following line to your header.php file, below the stylesheet:

  1. <?php include (TEMPLATEPATH . '/extrastyle.php'); ?>

Once you’ve done that, switch back to your new php file, and the following lines:

  1. <?php
  2. global $options;
  3. foreach ($options as $value) {
  4. if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
  5. }
  6. ?>
  7.  
  8. <style type="text/css">
  9.  
  10.  
  11. body{
  12. background: #<?php echo $hd_main_bg;?>;
  13. }
  14.  
  15. </style>

Remember in part one at the start we made a shortened name of our theme? We’re going to use that here – where it says “background:#<?php echo$hd_….” you need to change the hd (the shortend version of hexadecimal) to your theme’s shortname. And that’s that.

Showing and disabling ‘stuff’

showandgetrid

The next thing we did in part one was give the user the option to show or disable the search box, which we can do with the following code (it’s more or lest the same as the code above, so I won’t go into a deep and meaningful explanation).

  1. &amp;lt;?php
  2. global $options;
  3. foreach ($options as $value) {
  4. if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
  5. }
  6. ?&amp;gt;
  7.  
  8. &amp;lt;!– don't forget to change hd to your theme's shortname –&amp;gt;
  9.  
  10. &amp;lt;?php if ($hd_search_box == &amp;amp;quot;true&amp;amp;quot;) {?&amp;gt;
  11.  
  12. &amp;lt;!– sidebar code here –&amp;gt;
  13.  
  14. &amp;lt;?php }?&amp;gt;

Putting in your name

name

The final thing we did was put in the user’s name. Again, no explanation this time, hopefully you get the jist :D

  1. &amp;lt;!– don't forget the code to put at the start!–&amp;gt;
  2.  
  3. &amp;lt;?php echo $hd_my_name;?&amp;gt;

And finally

And there we have it. Wasn’t that fun!

Part one - Creating the options page

Part two – Implementation

Part three – Inspiration

Related posts:

  1. Free Theme: Happy Go Try

Tags:

Follow on Twitter! Subscribe!
Alex's Gravatar

Alex Denning is the founder of WPShout. A WordPress developer from London, Alex co-founded WPShift at the start of 2010 where he sells awesome WordPress themes.

You can find Alex on Twitter and at AlexDenning.com.

25 Responses to “Create An Awesome WordPress Theme Options Page (part 2 – implementation)”

  1. Konrad

    04. Jun, 2009

    I thought about making the theme i currently work on a bit more flexible without open aptana. Now i have the chance to do it. Thank you!

    Reply to this comment
  2. terrance

    12. Aug, 2009

    very cool, i am newbie and find your info very easy to follow; some of it……lol

    Reply to this comment
  3. Nick

    19. Aug, 2009

    I am trying to implement the theme options page according to your tutorial but after creating everything. Now wordpress is all messed up with that theme activated. If i click on anything, it goes to blank page and then I have to click on Back. Only after I delete the theme for which I created the options page, my wordpress acts normal again.

    Reply to this comment
    • Alex Denning

      20. Aug, 2009

      Sounds as though you’re missing something rather vital – go back through the options page really carefully, checking for errors.

      Reply to this comment
  4. Tommy

    21. Oct, 2009

    Great site! (I will subscribe) and Great Tutorial, unfortunately i di’dnt got it to work on my site.

    Should i keep the [php] & [/php] tag when i post the code in to my header.php file? or is it just to the example

    Reply to this comment
    • Alex Denning

      22. Oct, 2009

      Looks as though I need to update the tutorial so the code displays properly. In the meantime, check out the new series on creating an advanced theme options page.

      Reply to this comment
  5. Darice de Cuba

    25. Oct, 2009

    The text fields work for me but the checkbox stay stuck on true. If I untick the box and hit save it will stay checked…..an echo of the check box variable shows that it’s always on true.

    Any ideas what could be the problem?
    .-= Darice de Cuba´s last blog ..A PHP DOM parser case =-.

    Reply to this comment
  6. John MacMenamin

    19. Nov, 2009

    This code breaks when you upgrade to WP 2.8.6 looks like?

    I could be the only one but I doubt it.

    Is their a fix?
    .-= John MacMenamin´s last blog ..Stop DW _Notes Folder! =-.

    Reply to this comment
  7. virg

    10. Feb, 2010

    Hello, thanks for the tutorial! I managed to get the background theme options but when I put the php code from above in the header I get:

    Parse error: syntax error, unexpected ‘&’ in /home/requestg/public_html/themes/portfolio/wp-content/themes/mytheme/header.php on line 62

    Can you please help me?

    Reply to this comment
  8. virg

    10. Feb, 2010

    Sorry nvm, I got it to work but now when I try to save the theme options I get

    You do not have sufficient permissions to access this page.

    Reply to this comment
  9. wpdesigner

    23. Feb, 2010

    for all those having problems with this tutorial you need to replace all the [php] and other bits until you get something like

    <?php include (TEMPLATEPATH . ‘/extrastyle.php’); ?>

    Reply to this comment
  10. wpdesigner

    23. Feb, 2010

    sorry i mean this

    <?php include (TEMPLATEPATH . ‘/extrastyle.php’); ?>

    and it works like it should thanks to the author ;-)

    Reply to this comment
  11. wpdesigner

    23. Feb, 2010

    another example

    <!– don’t forget to change hd to your theme’s shortname –->

    <?php if ($hd_search_box == “true”) {?>

    <!–- search box code here -–>

    <?php }?>

    Reply to this comment
  12. alan

    19. Apr, 2010

    hard to follow. weired code printed on pages.
    You know just don’t ,wasting peoples time.I hate lazy developers!

    Fix the code printing [php] wtf !

    Reply to this comment
  13. phil

    22. May, 2010

    excellent tutorial,
    i have learn a lot but i can’t figure out how to add the upload form to an array,

    does anyone find a solution for this?

    thanks a lot for sharing your skills with us Alex!

    Reply to this comment
  14. WPExplorer

    06. Jun, 2010

    I wish I had seen this post sooner as I did not save myself hours by knowing to add the global options to my page..thanks for this post.

    Reply to this comment

Trackbacks/Pingbacks

  1. [...] Create An Awesome WordPress Theme Options Page (part 2 – implementation) Continuing on the ‘Create An Awesome WordPress Theme Options Page’ series, and following directly on from part one – By Nometech [...]

  2. [...] Create An Awesome WordPress Theme Options Page (part 2 – implementation) [...]

  3. [...] a little description of what the option does. ‘id’ is what’ll we’ll use in part two to implement the colour changing into the theme in part two. [...]

  4. [...] 3b. Create An Awesome WordPress Theme Options Page Part 2 [...]

  5. [...] is something that we’ve just covered on WPShout! In part one we created the backend, and in part two we implemented it.5. Tabbed sidebarTabbed content on your sidebar is very cool. Fact. Thankfully, [...]

  6. [...] 3b. Create An Awesome WordPress Theme Options Page Part 2 [...]

Leave a Reply

Please use your real name when commenting. Wrap code in <code> tags and make sure HTML is encoded. You can use regular HTML like <a href="... etc.

Get yours questions answered quicker

If you're using this post for paid work and have a question of any complexity then please ask at WPQuestions where you'll get a prompt response.