Create An Advanced Theme Options Page in WordPress: Day 4

Create An Advanced Theme Options Page in WordPress: Day 4

Day 4 of Creating an Advanced Theme Options Page in WordPress is here!

  • Day 1: Introduction
  • Day 2: Creating the different options
  • Day 3: Styling the options page
  • Day 4: Implementing the options into a theme
  • 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

    WordPress as a Tumblelog and CMS

    WordPress as a Tumblelog, CMS: Advanced Uses of WordPress


    Advanced Uses of WordPress

    WPShout


    9 Ways to Create a Crash Proof Blog

    9 Ways to Create a Crash Proof WordPress Site


    WPShift

    59 Comments - Add Yours!

    • Comment Name Violation wrote on October 18, 2009 at 6:43 am | Permalink

      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.

      • Alex Denning wrote on October 19, 2009 at 7:39 am | Permalink

        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.

    • James wrote on October 19, 2009 at 7:11 am | Permalink

      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

      • Alex Denning wrote on October 19, 2009 at 7:38 am | Permalink

        Sorry about that. That was meant to remind me to paste the code! You should see it now.

        • James wrote on October 19, 2009 at 8:47 am | Permalink

          Hi Alex,

          Thanks for your reply. I still cannot see the code after clearing my cache but will check later.

          Thanks again for sharing.

          • Alex Denning wrote on October 19, 2009 at 7:02 pm | Permalink

            Should be there once the cache has refreshed. It if isn’t I’ll post it in a comment.

            • James wrote on October 19, 2009 at 7:24 pm | Permalink

              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.

        • Damir wrote on October 20, 2009 at 12:37 am | Permalink

          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 =-.

          • Alex Denning wrote on October 20, 2009 at 7:32 am | Permalink

            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'] ); }
            }
            ?>

            • James wrote on October 20, 2009 at 9:33 am | Permalink

              Hi Alex,

              Thanks – yes it can be seen within the post now as well.

    • max wrote on October 20, 2009 at 2:09 pm | Permalink

      Thanks, this is a great tutorial!

      • Alex Denning wrote on October 20, 2009 at 4:36 pm | Permalink

        Glad you enjoyed it!

    • Damir wrote on October 22, 2009 at 5:52 am | Permalink

      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 =-.

      • Alex Denning wrote on October 22, 2009 at 7:13 am | Permalink

        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/

        • Damir wrote on October 23, 2009 at 3:25 am | Permalink

          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 =-.

          • Alex Denning wrote on October 23, 2009 at 9:03 am | Permalink

            No problem. Do let me see the results!

    • Joel G Goodman wrote on October 23, 2009 at 3:22 am | Permalink

      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 =-.

      • Alex Denning wrote on October 23, 2009 at 9:04 am | Permalink

        You need to put it in every single template file that calls the theme options, at the top of the file.

        • Joel G Goodman wrote on October 23, 2009 at 12:44 pm | Permalink

          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 =-.

    • Michael Oeser wrote on November 17, 2009 at 1:45 pm | Permalink

      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?

    • Michael Oeser wrote on November 17, 2009 at 1:46 pm | Permalink

      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 =-.

      • Alex Denning wrote on November 17, 2009 at 5:09 pm | Permalink

        Hey Michael,

        Your code has gone again! You’ll need to wrap it with and encode it with this tool. Then it should work!

    • Cristian wrote on November 17, 2009 at 2:47 pm | Permalink

      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.

      • Alex Denning wrote on November 17, 2009 at 5:06 pm | Permalink

        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.

      • James wrote on November 17, 2009 at 8:15 pm | Permalink

        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

      • James wrote on November 17, 2009 at 9:14 pm | Permalink

        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…

        • Alex Denning wrote on November 17, 2009 at 9:18 pm | Permalink

          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 :)

          • Cristian wrote on November 20, 2009 at 8:45 pm | Permalink

            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.

            • Alex Denning wrote on November 21, 2009 at 10:02 am | Permalink

              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

              AMAZING post!! THansk!! But it doesn’t work when I…

              kind of comment. Perhaps I was a bit harsh. Sorry about that. Luckily a lovely reader has replied to your comment with a solution.

        • Michael Cummings wrote on December 4, 2009 at 6:16 am | Permalink

          Thanks for the solution to that problem, and thanks to the Author for this helpful tutorial…

    • Michael Oeser wrote on November 17, 2009 at 5:21 pm | Permalink

      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 =-.

      • Alex Denning wrote on November 21, 2009 at 10:04 am | Permalink

        Still not displaying; I’m just replying to your email.

    • Justin wrote on November 28, 2009 at 4:14 pm | Permalink

      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 =-.

      • Alex Denning wrote on December 2, 2009 at 5:43 pm | Permalink

        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.

      • Joel G Goodman wrote on December 2, 2009 at 5:46 pm | Permalink

        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.

    • PatzZ wrote on December 20, 2009 at 10:24 am | Permalink

      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.

    • Benjamin Kornal wrote on December 30, 2009 at 10:28 pm | Permalink

      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

      • Benjamin Kornal wrote on December 30, 2009 at 10:37 pm | Permalink

        I forgot to tell you this happens when I try to save the options
        Thanks, Benjamin

      • Alex Denning wrote on December 31, 2009 at 11:19 am | Permalink

        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”);

        • Benjamin Kornal wrote on December 31, 2009 at 4:05 pm | Permalink

          That wasn’t it I had to save the file in UTF-8 and that fixed it
          Regards, Benjamin

    • Keith wrote on January 8, 2010 at 7:24 pm | Permalink

      One of the best WP tutorial I have read. Keep up the good work. Very well written and easy to follow.

      • Alex Denning wrote on January 18, 2010 at 3:45 pm | Permalink

        Thanks :)

    • Alex Ball wrote on January 10, 2010 at 4:44 am | Permalink

      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

    • Joaquin wrote on January 13, 2010 at 1:29 am | Permalink

      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

      • Alex Denning wrote on January 18, 2010 at 3:46 pm | Permalink

        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.

    • Steve Shead wrote on January 16, 2010 at 12:59 am | Permalink

      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

    • Steve Shead wrote on January 16, 2010 at 4:54 am | Permalink

      UPDATE: All functions that have a “save” or “submit”, or even logout now go to a blank page. What did I miss?

      Thanks

      Steve

    • Steve Shead wrote on January 16, 2010 at 3:46 pm | Permalink

      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

      • Alex Denning wrote on January 18, 2010 at 3:46 pm | Permalink

        Good to hear :)

    • Ricky wrote on January 24, 2010 at 11:40 am | Permalink

      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.

      • Ricky wrote on January 24, 2010 at 1:47 pm | Permalink

        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…..

    • Bob wrote on February 6, 2010 at 1:37 am | Permalink

      Great stuff works as it should and well explained i’m gonna use this for my upcoming designs thanks ;-)

    • James Smith wrote on February 14, 2010 at 3:49 pm | Permalink

      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

    • James Smith wrote on February 14, 2010 at 3:50 pm | Permalink

      This is what I have in the header in the above post (got cut of)

    • James Smith wrote on February 14, 2010 at 3:51 pm | Permalink

      header(“Content-type: text/css”);

    • James Smith wrote on February 14, 2010 at 3:51 pm | Permalink

      and

      include (TEMPLATEPATH.’/get-theme-options.php’);

      • Alex Denning wrote on February 15, 2010 at 3:38 pm | Permalink

        Does get-theme-options.php exist?

        • James Smith wrote on February 15, 2010 at 3:43 pm | Permalink

          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?

    • Joyful Thiek wrote on February 17, 2010 at 6:16 am | Permalink

      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.

    Post a Comment

    Please take not of the comment policy.

    Your email is never published nor shared.

    *
    *
    User Gravatar