0

My wordpress theme accepts skin files. These skin files all install into my main theme folder via a zip uploader that's part of my theme.

Each skin has a set of custom color codes (4 in all) that are stored in the wordpress options table like so...

Assume the skin name is "halloween"...These are the values in my options.php for one of my skin values...

halloween_color1 = 000000
halloween_color2 = ff0000
halloween_color3 = 777777
halloween_color4 = 333333

So I just need a means to store these values inside of each new skin's folder (the one that I send to people who use my theme) so that when they install the skin (via a simple zip extractor upload) I can place code into my zip extractor to write the skin's custom color values to the database.

I'm assuming a simple, colors.txt or colors.xml file will suffice.

How should I store the data in the text file in order to easily parse it and write it to the database? Name/value pairs or XML?

<skin>
<color name="halloween_color1" value="000000" />
<color name="halloween_color2" value="000000" />
<color name="halloween_color3" value="000000" />
<color name="halloween_color1" value="000000" />
</skin>
Scott B
  • 38,833
  • 65
  • 160
  • 266
  • Why would you store the values in a file in the first place when they get stored in the database anyway? – Pekka Sep 19 '10 at 22:44
  • The skin is added to the main theme after its been deployed into the wild. There has to be some means for the skin file to report to the theme what its colors are in order for them to be written to the database. – Scott B Sep 20 '10 at 01:53
  • Long story, but my skins all have background gif images that can be easily changed via a color picker once the skin is installed, giving the skin an infinite color palette. But the initial color palette is defined by the skin itself. So rather than mess with the css values with hardcoded background colors, I use tiled gif images for the backgrounds and allow the user to change the color of these background images via colorpicker. – Scott B Sep 20 '10 at 01:55
  • @Pekka, hopefully I've explained it well enough, but this is no ordinary WP theme. Its basically a theme that takes an unlimited number of skin files (a folder with css file and images) that are created after its been deployed. I can install one theme that can be changed, on the fly, to have a completely different look (think css zen garden) – Scott B Sep 20 '10 at 02:02

1 Answers1

0

There are several options. Two that PHP can read natively are ini and CSV.

INI example:

[skin]
halloween_color1 = 000000
halloween_color2 = ff0000
halloween_color3 = 777777
halloween_color4 = 333333

CSV example:

halloween_color1;000000
halloween_color2;ff0000
halloween_color3;777777
halloween_color4;333333

For me personally, YAML has become the favourite format for human-readable configuration files.

YAML example:

skin:
 halloween_colors:
   - 000000
   - ff0000
   - 777777
   - 333333

its advantages in my view are:

  • Parsing is very strict; it will exit immediately and throw an error if it doesn't like the file's structure

  • It supports nested data structures, the building of associative arrays, typing, and lists (Useful e.g. if you want to add a fifth halloween colour)

But, it needs a third party library. Whether that is justified, you have to decide. See a list of PHP parser libraries here.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I won't see enough use of this to require a library added to my main package install. I'm going to check out the ini file method and just add some code to my uploader to parse out the values and write them to the database. Thanks for the input as always Pekka :) – Scott B Sep 20 '10 at 14:40