Theme Options

29th March 2017

My latest composer release is a small library to help with theme options in WordPress. It provides a simple interface for defining theme options and retrieving them later.

I made this library quite a while ago and manually copied it from theme to theme as I built them. Newer themes have newer versions and I decided it was time to move it onto composer.

I originally built it to replace the function themeslug_theme_option_or_default('option_name', 'default_value'); which required me to remember the default value everywhere I used the option. Theme options solves this by having you declare your options in the init action. Now when you call $themeOptions->themeOption('option_name'); it handles returning the default value if needed.

Getting it.

You can get theme options from composer using

composer require arcath/theme-options

or download it from GitHub.

Once you have it require it (or use composers autoload) at the top of your functions.php.

Using It.

Somewhere near the top of your functions.php you need to create a new instance of Arcath\ThemeOptions and pass your themes slug. This instance then needs to be globally referenced whenever you want to use it.

2$themeOptions = new Arcath\ThemeOptions('theme_slug');
4add_action('init', function(){
5 global $themeOptions;
7 // Add an option using a custom control class
8 $themeOptions->addThemeOptionCustomControl('logo_light',
9 array(
10 'type' => 'option',
11 'default' => 0
12 ),
13 array(
14 'section' => 'home',
15 'label' => __('Light Logo', 'theme_slug'),
16 'description' => __('Light Logo used on darker backgrounds', 'theme_slug'),
17 ),
18 'WP_Customize_Image_Control'
19 );
21 // Add an option with the standard control class
22 $themeOptions->addThemeOption('phone_number',
23 array(
24 'capability' => 'edit_theme_options',
25 'type' => 'option',
26 'default' => '0123456789',
27 ),
28 array(
29 'label' => 'Phone Number',
30 'section' => 'home',
31 'description' => __('Your Phone Number', 'theme_slug')
32 )
33 );
35add_action('customize_register', function($wp_customize){
36 // Create the required sections
37 $wp_customize->add_section(
38 'home',
39 array(
40 'title' => 'Home',
41 'description' => __('Home Page Template options.', 'theme_slug'),
42 'capability' => 'edit_theme_options',
43 )
44 );
Language php

To define an option you use either addThemeOption or addThemeOptionCustomControl. Both take the same 3 arguments with addThemeOptionCustomControl taking a 4th.

  • Option Name, The name of your option, used to recall it later.
  • Setting Options, Passed to $wp_customize->addSetting. You can omit the setting name. The default supplied here will be used as the default when you ask for the option later.
  • Control Options, Passed to $wp_customize->addControl. You can omit the setting name.
  • Control Class Name, A string containing the name of the class you want to use for your control. Only used by addThemeOptionCustomControl.

To use your theme options in your theme simply call themeOption and pass the name of your setting.

2global $themeOptions;
Language php

Theme options is a pretty simple library, I will update it more in the future as I need to. Feel free to open issues on GitHub if you have any suggestions or issues.