Last active
December 12, 2017 20:21
-
-
Save Thalvik/1a4bca0937dfd9ef9617b318ff780cea to your computer and use it in GitHub Desktop.
WordPress theme options class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sample theme class for adding sections and controls for WordPress theme | |
* | |
* This is a simple class for adding sections and controls for WordPress theme. | |
* You can use it freely, modify it for your needs. Just include it in your functions.php file | |
* | |
* @since 1.0.0 | |
* @author Aleksandar Andrijevic <[email protected]> | |
* @link http://alekandrijevic.com | |
*/ | |
class My_Theme_Customizer { | |
//Change this prefix to identify your options | |
private static $my_theme_prefix = 'my_theme'; | |
//We are registring this customizer | |
public function __construct() { | |
add_action( 'customize_register', array(&$this, 'my_theme_customize_manager' )); | |
} | |
/** | |
* Helper function to return option. | |
* | |
* You can use this function by calling it from global object in which you create instance | |
* of it in, on ending of this class, for example: | |
* | |
* global $my_theme_customizer; | |
* $my_theme_customizer::my_theme_get_option('_sample_color_setting'); | |
* | |
* | |
* You can also use it without accessing global object: | |
* My_Theme_Customizer::my_theme_get_option('_sample_color_setting'); | |
* | |
* | |
* Or you can use directly get_theme_mode, see docs: | |
* get_theme_mod( 'my_theme_sample_color_setting'); | |
*/ | |
public static function my_theme_get_option($option_name, $default = false) { | |
$option = get_theme_mod( self::$my_theme_prefix . $option_name, $default); | |
return $option; | |
} | |
//Seperate sections by samples, for simplicity. Add as many needed | |
public function my_theme_customize_manager( $wp_manager ) { | |
$this->my_theme_customizer_sample_first($wp_manager); | |
$this->my_theme_customizer_sample_second($wp_manager); | |
$this->my_theme_customizer_sample_third($wp_manager); | |
} | |
//First sample, with some image controls | |
private function my_theme_customizer_sample_first($wp_manager) { | |
//Add first section | |
$wp_manager->add_section( self::$my_theme_prefix . '_sample_first_section', array( | |
'title' => __('Sample settings first', 'my_theme_domain'), | |
'priority' => 200, | |
) ); | |
//Color | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_color_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( new WP_Customize_Color_Control( $wp_manager, self::$my_theme_prefix . '_sample_color_setting', array( | |
'label' => __('Sample color', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_first_section', | |
'settings' => self::$my_theme_prefix . '_sample_color_setting', | |
'priority' => 1 | |
) ) ); | |
//Media | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_media_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( new WP_Customize_Media_Control( $wp_manager, self::$my_theme_prefix . '_sample_media_setting', array( | |
'label' => __('Sample media', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_first_section', | |
'settings' => self::$my_theme_prefix . '_sample_media_setting', | |
'priority' => 2 | |
) ) ); | |
//Upload | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_upload_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( new WP_Customize_Upload_Control( $wp_manager, self::$my_theme_prefix . '_sample_upload_setting', array( | |
'label' => __('Sample upload', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_first_section', | |
'settings' => self::$my_theme_prefix . '_sample_upload_setting', | |
'priority' => 3 | |
) ) ); | |
//Image | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_image_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( new WP_Customize_Image_Control( $wp_manager, self::$my_theme_prefix . '_sample_image_setting', array( | |
'label' => __('Sample image', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_first_section', | |
'settings' => self::$my_theme_prefix . '_sample_image_setting', | |
'priority' => 4 | |
) ) ); | |
//Cropped image | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_cropped_image_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( new WP_Customize_Cropped_Image_Control( $wp_manager, self::$my_theme_prefix . '_sample_cropped_image_setting', array( | |
'label' => __('Sample cropped image', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_first_section', | |
'settings' => self::$my_theme_prefix . '_sample_cropped_image_setting', | |
'priority' => 5 | |
) ) ); | |
//Site icon | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_site_icon_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( new WP_Customize_Site_Icon_Control( $wp_manager, self::$my_theme_prefix . '_sample_site_icon_setting', array( | |
'label' => __('Sample site icon', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_first_section', | |
'settings' => self::$my_theme_prefix . '_sample_site_icon_setting', | |
'priority' => 6 | |
) ) ); | |
} | |
//Second sample, with some basic input types | |
private function my_theme_customizer_sample_second($wp_manager) { | |
//Add second section | |
$wp_manager->add_section( self::$my_theme_prefix . '_sample_second_section', array( | |
'title' => __('Sample settings second', 'my_theme_domain'), | |
'priority' => 201, | |
) ); | |
//Sample text | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_text_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_text_setting', array( | |
'label' => __('Sample text', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_second_section', | |
'type' => 'text', | |
'priority' => 1 | |
) ); | |
//Sample checkbox | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_checkbox_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_checkbox_setting', array( | |
'label' => __('Sample checkbox', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_second_section', | |
'type' => 'checkbox', | |
'priority' => 2 | |
) ); | |
//Text area | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_textarea_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_textarea_setting', array( | |
'label' => __('Sample textarea', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_second_section', | |
'type' => 'textarea', | |
'priority' => 3 | |
) ); | |
//Radio | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_radio_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_radio_setting', array( | |
'label' => __('Sample radio', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_second_section', | |
'type' => 'radio', | |
'choices' => array('first' => 'First', 'second' => 'Second', 'third' => 'Third'), | |
'priority' => 4 | |
) ); | |
//Select | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_select_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_select_setting', array( | |
'label' => __('Sample select', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_second_section', | |
'type' => 'select', | |
'choices' => array('first' => 'First', 'second' => 'Second', 'third' => 'Third'), | |
'priority' => 5 | |
) ); | |
//Dropdown pages | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_dropdown_pages_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_dropdown_pages_setting', array( | |
'label' => __('Sample dropdown pages', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_second_section', | |
'type' => 'dropdown-pages', | |
'priority' => 6, | |
'allow_addition' => true | |
) ); | |
} | |
//Third example, with some additional input types | |
private function my_theme_customizer_sample_third($wp_manager) { | |
//Add thrid section | |
$wp_manager->add_section( self::$my_theme_prefix . '_sample_third_section', array( | |
'title' => __('Sample settings third', 'my_theme_domain'), | |
'priority' => 202, | |
) ); | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_email_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_email_setting', array( | |
'label' => __('Sample email', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_third_section', | |
'type' => 'email', | |
'priority' => 1, | |
'allow_addition' => true | |
) ); | |
//URL | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_url_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_url_setting', array( | |
'label' => __('Sample URL', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_third_section', | |
'type' => 'url', | |
'priority' => 2, | |
'allow_addition' => true | |
) ); | |
//Number | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_number_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_number_setting', array( | |
'label' => __('Sample number', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_third_section', | |
'type' => 'number', | |
'priority' => 3, | |
'allow_addition' => true | |
) ); | |
//Hidden | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_hidden_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_hidden_setting', array( | |
'label' => __('Sample hidden', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_third_section', | |
'type' => 'hidden', | |
'priority' => 4, | |
'allow_addition' => true | |
) ); | |
//Date | |
$wp_manager->add_setting( self::$my_theme_prefix . '_sample_date_setting', array( | |
'default' => '', | |
) ); | |
$wp_manager->add_control( self::$my_theme_prefix . '_sample_date_setting', array( | |
'label' => __('Sample date', 'my_theme_domain'), | |
'section' => self::$my_theme_prefix . '_sample_third_section', | |
'type' => 'date', | |
'priority' => 5, | |
'allow_addition' => true | |
) ); | |
} | |
} | |
//Initalize class | |
$my_theme_customizer = new My_Theme_Customizer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment