in Journal

Coding a style changer for WordPress 2.0

I'm sure there are plugin's that take care of this function, however I decided to hardcode this right into my WordPress theme. Why? Because I don't plan to use any other themes. All style changes I make will be done through changing the CSS style sheet. I coded this so that I could work on a new style sheet offline, and when I'm finished, upload the file to my styles directory. It will then automatically show up in the drop down box on the Style Changer page.

So here is what I did. First I created a new WordPress page and called it Style Changer. On this page I put the following PHP code:

With WordPress 2.0, there's an "Execute page content between ?php ? as PHP." option. I checked this box so that my PHP code would be executed. If you're having problems doing this, I highly reccommend you turn off the Visual Rich Editor option in your WordPress User options. The visual rich editor does too many funky things in the background.

Next I created a file called set_style.php and added this code:

That is the file that gets executed when a user changes the style on the Style Changer page.

The last step was to add code to my theme's header.php file. This code needs to check if the cookie is already set, and if it's not, it needs to set the cookie. If the cookie is already set, it checks to make sure the style defined in that cookie exists in my style's directory. If it doesn't exist, then it sets the cookie to the default style (default.css). If the style does exist, then the style is used. See the next block of code for how setting the style is done in the HTML.

Now that we have the name of the style stored in the $style variable, we can combine the $styleroot variable with the $style variable to get the full path to the style sheet.

That's it! Now I just need to upload a new style sheet to my styles directory, and it will show up in the drop down box on the Style Changer page. Once the style is changed, it's name is stored in a cookie on the user's computer and is used whenever my page is visited.

If you have any comments or suggestions, please post them!

Write a Comment

Comment

  1. WoW. LoL

    Suddenly, me coding for my games seem infinitely minute than they previously were. I’m sure there are much tougher codings, but anything above:

    {
    speed += 1
    instance_create(10,0,boom)
    str = ‘Omg, Lamer’
    }

    puts me in my place. =P

  2. Haha.

    Programming is one of those things I can be doing for hours and it will feel as if only a few minutes have passed. I’ve spent 30+ straight hours coding, without sleep. I’m not sure what it is I enjoy so much, but I do wish I had more free time to do it!

  3. Wolfenstien is one of those things I can be doing for hours and it will feel as if only a few minutes have passed.

    On another note – I finally posted to the blog. Thanks for adding me one 🙂 – i think.

  4. Yea DJT, I miss Wolfenstien. 🙁

    Now I always feel that my time could be spent doing better things than playing Wolf. I should really give myself a little break every now and then and play again…

  5. I can even get a wolf fix just playing one round every now and then. Just the other night after packing I took a 15 minute break and played. It was enough to settle the nerves of packing. I even did a 1 on 1 with HillBilly and won – tho he’ll never admit to it.