views:

32

answers:

1

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

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.

Pekka
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