Brendan Murty

Home > Posts > Add some character to your website with themes

It's easy to add themes to your website and allow your visitors to customise their experience. This post will guide you through implementing theme support on your website using PHP, CSS and cookies.

There are a few requirements of a theming system:

  1. Setting up your CSS files
  2. Showing a list of themes
  3. Allow the user to select a theme
  4. Include the selected theme styles

Setting up your CSS files

This method is designed around the idea of placing common styles in a file named *common.css *and placing theme specific styling in other CSS files. The filename of these files will become the theme name, like so:

For this example, the CSS files are placed in a top level folder named styles. Feel free to update this by changing the value of the $folder variables in the PHP functions.


// theme_list - Return a list of the available themes
function theme_list(){
	$folder='/styles/';// Path to the themes folder

	// Extract a list of files in the folder and sort them

	// Create the theme selector list
	foreach($files as $file){
		if($file!='.' && $file!='..' && $ext=='css' && $file!='common.css'){
			$theme_title=str_replace("_"," ",str_replace("-"," ",ucwords(strtolower($theme_name))));
			$return.='<li><a href="/theme-set.php?theme='.$theme_name.'">'.$theme_title.'</a></li>';
	if($return!=''){ return '<ul class="themes">'.$return.'</ul>'; }


// theme_get - Return the currently selected theme name
function theme_get(){
  $theme_selected='red';// Set the default theme
	$folder='/styles/';// Path to the themes folder
	if(isset($_COOKIE['theme']) && $_COOKIE['theme']!=''){
	return $theme_selected;

// Output the CSS includes to the page
print '<link rel="stylesheet" href="/styles/common.css">';
print '<link rel="stylesheet" href="/styles/'.$theme_selected.'.css">';


// theme_set - Update the theme in use
function theme_set($theme){

// Check the URL for a theme
if(isset($_GET['theme']) && $_GET['theme']!=''){
	// Clean this data! One method:

	// Set the theme then redirect to the home page
	header('Location: /');


Themes aren't suitable for every website, so use them wisely to allow for customisations that suit your visitors' needs. Also, don't forget to ensure your default theme remains usable, including suitably sized text and clear link styling.