Ok, so I’m working on a custom theme, it has multiple theme colour options. So I have it calling up the theme colour options through my wordpress admin page. Everything works perfectly, didn’t run into a SINGLE problem. I had stepped away from web design for a long while, and am recently getting back into it. I am knowledgeable in the PHP Language (at least the old way), but the code will show how I used to & currently doing ‘arrays’ and ‘if/while/foreach’ statements. I just want to know if there is a new up to date way that this is done, so that way my code doesn’t become deprecated anytime soon, and can be modified by anyone who wants to edit this theme.
This is my function, and calls up flawlessly:
function peridot_colour_styles() {
$colour_list = array('peridot', 'sapphire');
foreach ($colour_list as $output_list) {
if (get_option('peridot_theme_colour') == $output_list) { $selected = 'selected="selected"'; } else { $selected = ''; }
echo '<option value="'. $output_list .'" '. $selected .'>'. ucfirst($output_list) .'</option>';
}
}
I’m not really wanting to create 10 options in the WordPress options base, just for theme colours, so I will be just naming the css file ‘peridot.css’ and ‘sapphire.css’ so whey they select ‘Peridot’ I can just call up the style general name.
–Note–
As I said, I’m just wanting to know if there is better ways to do this; or a newer method used for doing these. I don’t want this code re-written for me, but am not opposed to someone ‘modifying’ anything. I would also be fine with ‘you could do it like this blah blah blah, search google for this’ 🙂
I’m knowledgeable in PHP and can wrap my head around tutorials/guides very easily.
Overall, your code is fine. Your approach is also optimal in that you’re loading a separate stylesheet based on the selected option. The only thing I would do is leverage WordPress’ Selected helper function to reduce things a little bit: