Log In · Register

 
 
Closed TopicStart new topic
Switching Themes with PHP
Fabio.
post Dec 5 2005, 03:17 PM
Post #1


^ Mrs. Jonas
*****

Group: Member
Posts: 592
Joined: Apr 2005
Member No: 263,313



The closest I got when searching was this, and it didn't help.

I'm working on another website, and want to be able to switch themes, and like Heather had said, switch it on all pages. It's NOT wordpress, by the way.

I have header.inc and footer.inc, and then index.php which includes both. I know that I can make header2.inc and footer2.inc, but I don't know how to make some sort of dropdown where you can choose whether you want theme 1 or theme 2, and how to make it include the appropriate header and footer.

I tried googling it, but only came up with Wordpress and B2 hacks, which isn't helping me at all.
 
*mipadi*
post Dec 5 2005, 03:48 PM
Post #2





Guest






Are you doing it for a general website? It's fairly simple, assuming I am following you correctly. It involves three things: making a separate stylesheet for each "theme", making a form to select themes, and write code to handle that.

First, the form: Just make a normal HTML form. The code will look something like this:
CODE
<form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>
   <select name="style">
       <option value="stylea">A</option>
       <option value="styleb">B</option>
   </select>
   <input type="submit" value="Submit"/>
</form>[/code
Put this wherever you want the form to appear. Naturally, you fill in an <option> tag for each option. Feel free to customize the look of the form, as well as the label for the Submit button (in the "value" attribute").

Now, I'm assuming your header.inc file includes the stylesheet, with a line such as:
[code]<link rel="stylesheet" type="text/css" href="style.css"/>

(<?php echo $_SERVER['SCRIPT_NAME']; ?> inserts the filename of the page.)

What you need to do is grab the value from the form, and insert it into HTML code. Assume for a second your stylesheets are named according to the values you used in your form (i.e., you have a style called "stylea.css" and a style called "styleb.css"). When you select a new style, your page will change from, say, page.php to page.php?style=stylea. "style", of course, contains the value of the stylesheet you selected.

Your coding in your header file would look something like this:
CODE
<html>
<head>
   <title>blah blah</title>
   <?php echo '<link rel="stylesheet" type="text/css" href="'.$_GET['style'].'"/>'; ?>
   <!-- rest of code here -->

<?php echo '<link rel="stylesheet" type="text/css" href="'.$_GET['style'].'"/>'; ?> inserts the appropriate <link> tag, plus the value of the "style" property in the query string (the thing after the ? in the URL, in this case, ?style=styla).

Now, whenever the user selects a new theme, the appropriate stylesheet will be included.




(Also, as a minor side note, it is more secure to name include files as "include.php" rather than "include.inc". If a user navigates directly to the file (such as by typing in host.com/include.inc, they will see all the source code for the file--which is had if the source code contains passwords to databases or other confidential data. However, if a user navigates to an include file at host.com/include.php, the include file will be executed and the user will only see output that is explicity shown using PHP.)
 
Fabio.
post Dec 5 2005, 07:25 PM
Post #3


^ Mrs. Jonas
*****

Group: Member
Posts: 592
Joined: Apr 2005
Member No: 263,313



I think that's what I'm looking for. Let me try it out later this evening and I'll tell you if that works (so don't close this yet, ever-helpful mods). But I do think that should work.

Also, thanks for telling me about the .inc files. The tutorial I used for converting to PHP just said to use that, so thanks for telling me about the security risks!
 
Fabio.
post Dec 5 2005, 07:25 PM
Post #4


^ Mrs. Jonas
*****

Group: Member
Posts: 592
Joined: Apr 2005
Member No: 263,313



-sorry, double post-
 
Mulder
post Dec 5 2005, 09:08 PM
Post #5


i lost weight with Mulder!
*******

Group: Official Designer
Posts: 4,070
Joined: Jan 2005
Member No: 79,019



are you looking for this: http://codegrrl.com/tutorials/php/skinning_your_site.php ?

im going to try this soon.

after finals.
 
Heathasm
post Dec 6 2005, 04:12 AM
Post #6


creepy heather
*******

Group: Official Member
Posts: 4,208
Joined: Aug 2004
Member No: 41,580



oh, mel, i bookmarked the site i got my code from!

http://textpattern.org/tips/328/style-switcher-non-plugin
 
Mulder
post Dec 6 2005, 09:15 AM
Post #7


i lost weight with Mulder!
*******

Group: Official Designer
Posts: 4,070
Joined: Jan 2005
Member No: 79,019



QUOTE(Heathasm @ Dec 6 2005, 4:12 AM)
oh, mel, i bookmarked the site i got my code from!

http://textpattern.org/tips/328/style-switcher-non-plugin
*

i love you heather!

throb.gif

i am so doing this after finals!
 
freeflow
post Dec 6 2005, 10:16 AM
Post #8


t-t-t-toyaaa
********

Group: Official Member
Posts: 19,821
Joined: Apr 2004
Member No: 11,270



Just be aware that (well according to what people told me) it takes up alot of space sometimes. Im not exactly sure though. I wanted too but I think it might like take up all the space..
does it?
 
Heathasm
post Dec 6 2005, 01:39 PM
Post #9


creepy heather
*******

Group: Official Member
Posts: 4,208
Joined: Aug 2004
Member No: 41,580



no, not at all
its just a few lines of php and extra stylesheets
 
Mulder
post Dec 6 2005, 05:36 PM
Post #10


i lost weight with Mulder!
*******

Group: Official Designer
Posts: 4,070
Joined: Jan 2005
Member No: 79,019



i dont want to steal lisa's bandwidth..




i might try it...

but im worried about taking all of lisa's space..
 
freeflow
post Dec 6 2005, 07:09 PM
Post #11


t-t-t-toyaaa
********

Group: Official Member
Posts: 19,821
Joined: Apr 2004
Member No: 11,270



^ you should ask who knows her bandwith might not even be affected. I haven't talked to her in MONTHS! we used to talk everyday =/ but I don't think it will mess with her bandwith
 
Fabio.
post Dec 6 2005, 07:19 PM
Post #12


^ Mrs. Jonas
*****

Group: Member
Posts: 592
Joined: Apr 2005
Member No: 263,313



QUOTE(insomniac @ Dec 5 2005, 9:08 PM)
are you looking for this: http://codegrrl.com/tutorials/php/skinning_your_site.php ?

im going to try this soon.

after finals.
*


Ooh, I like that a little better... well, I'll try this one and then Michael's and then maybe Heather's and whichever one works...

DOES it affect space and bandwidth and such? I wouldn't think so... _unsure.gif
 
*mipadi*
post Dec 6 2005, 07:26 PM
Post #13





Guest






QUOTE(Fabio. @ Dec 6 2005, 7:19 PM)
DOES it affect space and bandwidth and such? I wouldn't think so... _unsure.gif
*

Not bandwidth. The stylesheets and code will take up more space, but only a few kilobytes--practically nothing.
 
Mulder
post Dec 6 2005, 08:37 PM
Post #14


i lost weight with Mulder!
*******

Group: Official Designer
Posts: 4,070
Joined: Jan 2005
Member No: 79,019



how would you do this with wordpress?

....


im not really sure how, since all my css is part of my wp theme.

its probably really simple knowing me.
 
Fabio.
post Dec 6 2005, 09:04 PM
Post #15


^ Mrs. Jonas
*****

Group: Member
Posts: 592
Joined: Apr 2005
Member No: 263,313



http://codegrrl.com/tutorials/php/skinning_your_site.php

SUCCESS!!!

Anyhow, I think if all of your CSS is in your wordpress, you could just download a theme switcher plugin, right? And just have different wordpress themes that you have edited. And if your other pages include the same header.php and footer.php files, it should all change. I think.

SUCESS YAYYYYY.....
 
Mulder
post Dec 6 2005, 09:55 PM
Post #16


i lost weight with Mulder!
*******

Group: Official Designer
Posts: 4,070
Joined: Jan 2005
Member No: 79,019



^you use wordpress right?

how are you doing it?
 
Fabio.
post Dec 6 2005, 10:48 PM
Post #17


^ Mrs. Jonas
*****

Group: Member
Posts: 592
Joined: Apr 2005
Member No: 263,313



Well, I haven't adapted it to work with wordpress yet. The site I'm working on is just a site... but if and when I get it figured out, you'll be the first to know. *writes post-it note* But my guess is that you just create multiple wordpress themes and then just download the plugin (I think there's a link to it in the first post of this thread) to switch between themes. I'm not sure... it might get hairy when you start using other pages.

Anyhow. PROBLEM.

So I followed that tutorial completely, and... well, it works, for the most part. It switches the theme, but when you go back to browsing the original site, it goes back to the original theme.

Here's the site: http://writersblock.melilyn.com/index.php [it's still under a lot of construction, and the themes are just example themes, but if you click on 'theme switcher' you'll see what I mean.]

HELP? cry.gif
 
*mipadi*
post Dec 6 2005, 10:54 PM
Post #18





Guest






QUOTE(Fabio. @ Dec 6 2005, 10:48 PM)
Here's the site: http://writersblock.melilyn.com/index.php [it's still under a lot of construction, and the themes are just example themes, but if you click on 'theme switcher' you'll see what I mean.]

HELP?  cry.gif
*

The theme information is passed along in the query string--the part of the URL that says ?newskin=2 or whatever. However, the main page link links back to index.php, and loses the value in the query string. You have to keep passing the value along; you can do that by making sure all your links end in something like this:

CODE
<a href="page.php?newskin=<?php echo $_GET['newskin']; ?>">link text</a>
 
Mulder
post Dec 6 2005, 10:56 PM
Post #19


i lost weight with Mulder!
*******

Group: Official Designer
Posts: 4,070
Joined: Jan 2005
Member No: 79,019



michael, do you know how to make it work with wordpress when you're not using wordpress for all of the pages?
 
*mipadi*
post Dec 6 2005, 11:00 PM
Post #20





Guest






QUOTE(insomniac @ Dec 6 2005, 10:56 PM)
michael, do you know how to make it work with wordpress when you're not using wordpress for all of the pages?
*

Hm...not offhand, but if I get a chance, I'll take a look at the Wordpress source code, see if I can figure something out.
 
Fabio.
post Dec 6 2005, 11:06 PM
Post #21


^ Mrs. Jonas
*****

Group: Member
Posts: 592
Joined: Apr 2005
Member No: 263,313



QUOTE(mipadi @ Dec 6 2005, 10:54 PM)
You have to keep passing the value along; you can do that by making sure all your links end in something like this:

CODE
<a href="page.php?newskin=<?php echo $_GET['newskin']; ?>">link text</a>

*


Would I do this for each and every link on the entire site? Or just the link back to the index?

Thank you for helping, by the way...

Edit: Ahh, all of them. I tested it out and it seems to work. So my problem is resolved, it's just Michelle still! <3

Thanks, Michael.

This post has been edited by Fabio.: Dec 6 2005, 11:28 PM
 
Mulder
post Dec 7 2005, 12:00 AM
Post #22


i lost weight with Mulder!
*******

Group: Official Designer
Posts: 4,070
Joined: Jan 2005
Member No: 79,019



QUOTE(Fabio. @ Dec 6 2005, 11:06 PM)
Edit: Ahh, all of them. I tested it out and it seems to work. So my problem is resolved, it's just Michelle still! <3

*

_dry.gif


its just that my wordpress theme is composed of many files...

i guess i could make it just a css file...

but i couldnt. id still have to have the blog file, the comments file..

should i just use the theme switcher?
 

Closed TopicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: