Advertisement
MrPauloeN

WordPress Customizer Basic Controls Example

Mar 9th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.28 KB | None | 0 0
  1. /**
  2.  * Customizer: Add Controls: Basic
  3.  *
  4.  * This file demonstrates how to add basic core controls to the Customizer.
  5.  *
  6.  * The Customizer API includes basic controls for the following control types:
  7.  * - basic: checkbox
  8.  * - basic: dropdown pages
  9.  * - basic: radio
  10.  * - basic: select
  11.  * - basic: text
  12.  * - basic: textarea
  13.  *
  14.  * WordPress 4.0 also introduced controls for the following specialized text input control types:
  15.  * - text: email
  16.  * - text: number
  17.  * - text: password (not included here)
  18.  * - text: search (not included here)
  19.  * - text: tel
  20.  * - text: url
  21.  *
  22.  * @package   code-examples
  23.  * @copyright Copyright (c) 2015, WordPress Theme Review Team
  24.  * @license   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, v2 (or newer)
  25.  */
  26.  
  27.  
  28. /**
  29.  * Theme Options Customizer Implementation.
  30.  *
  31.  * Implement the Theme Customizer for Theme Settings.
  32.  *
  33.  * @link http://ottopress.com/2012/how-to-leverage-the-theme-customizer-in-your-own-themes/
  34.  *
  35.  * @param WP_Customize_Manager $wp_customize Object that holds the customizer data.
  36.  */
  37. function theme_slug_register_customizer_controls_basic( $wp_customize ){
  38.  
  39.     /**
  40.      * Failsafe is safe
  41.      */
  42.     if ( ! isset( $wp_customize ) ) {
  43.         return;
  44.     }
  45.    
  46.    
  47.     /**
  48.      * Basic Checkbox control.
  49.      *
  50.      * - Control: Basic: Checkbox
  51.      * - Setting: Display Footer Credit Link
  52.      * - Sanitization: checkbox
  53.      *
  54.      * Register the core "checkbox" control to be used to configure the Display Footer Credit Link setting.
  55.      *
  56.      * @uses $wp_customize->add_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/
  57.      * @link $wp_customize->add_control() https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
  58.      */
  59.     $wp_customize->add_control(
  60.         // $id
  61.         'display_footer_credit_link',
  62.         // $args
  63.         array(
  64.             'settings'      => 'display_footer_credit',
  65.             'section'       => 'theme_slug_section_footer',
  66.             'type'          => 'checkbox',
  67.             'label'         => __( 'Display Footer Credit Link', 'theme-slug' ),
  68.             'description'   => __( 'Should the Theme developer credit link be displayed in your site footer?', 'theme-slug' ),
  69.         )
  70.     );
  71.    
  72.    
  73.     /**
  74.      * Basic Drop-down Pages control.
  75.      *
  76.      * - Control: Basic: Dropdown Pages
  77.      * - Setting: Call-To-Action Link
  78.      * - Sanitization: dropdown_pages
  79.      *
  80.      * Register the core "dropdown-pages" control to be used to configure Call-To-Action Link setting.
  81.      *
  82.      * @uses $wp_customize->add_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/
  83.      * @link $wp_customize->add_control() https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
  84.      */
  85.     $wp_customize->add_control(
  86.         // $id
  87.         'cta_link',
  88.         // $args
  89.         array(
  90.             'settings'      => 'cta_link',
  91.             'section'       => 'theme_slug_section_frontpage',
  92.             'type'          => 'dropdown-pages',
  93.             'label'         => __( 'Call-To-Action Link', 'theme-slug' ),
  94.             'description'   => __( 'Select the page to link to the Call-To-Action button on the front page.', 'theme-slug' ),
  95.         )
  96.     );
  97.    
  98.    
  99.     /**
  100.      * Basic Radio Button control.
  101.      *
  102.      * - Control: Basic: Radio
  103.      * - Setting: Menu Position
  104.      * - Sanitization: select
  105.      *
  106.      * Register the core "radio" control to be used to configure the Menu Position setting.
  107.      *
  108.      * @uses $wp_customize->add_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/
  109.      * @link $wp_customize->add_control() https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
  110.      */
  111.     $wp_customize->add_control(
  112.         // $id
  113.         'menu_position',
  114.         // $args
  115.         array(
  116.             'settings'      => 'menu_position',
  117.             'section'       => 'theme_slug_section_header',
  118.             'type'          => 'radio',
  119.             'label'         => __( 'Menu Position', 'theme-slug' ),
  120.             'description'   => __( 'Display the main navigation menu above or below the header image?', 'theme-slug' ),
  121.             'choices'       => array(
  122.                 'above' => __( 'Above', 'theme-slug' ),
  123.                 'below' => __( 'Below', 'theme-slug' )
  124.             )
  125.         )
  126.     );
  127.    
  128.    
  129.     /**
  130.      * Basic Select control.
  131.      *
  132.      * - Control: Basic: Select
  133.      * - Setting: Color Scheme
  134.      * - Sanitization: select
  135.      *
  136.      * Register the core "select" control to be used to configure the Color Scheme setting.
  137.      *
  138.      * @uses $wp_customize->add_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/
  139.      * @link $wp_customize->add_control() https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
  140.      */
  141.     $wp_customize->add_control(
  142.         // $id
  143.         'color_scheme',
  144.         // $args
  145.         array(
  146.             'settings'      => 'color_scheme',
  147.             'section'       => 'theme_slug_section_colors',
  148.             'type'          => 'select',
  149.             'label'         => __( 'Color Scheme', 'theme-slug' ),
  150.             'description'   => __( 'Select the color scheme to be used for your site.', 'theme-slug' ),
  151.             'choices'       => array(
  152.                 'blue' => __( 'Blue', 'theme-slug' ),
  153.                 'red' => __( 'Red', 'theme-slug' ),
  154.                 'green' => __( 'Green', 'theme-slug' )
  155.             )
  156.         )
  157.     );
  158.    
  159.    
  160.     /**
  161.      * Basic Text control.
  162.      *
  163.      * - Control: Basic: Text
  164.      * - Setting: Footer Copyright Text
  165.      * - Sanitization: html
  166.      *
  167.      * Register the core "text" control to be used to configure the Footer Copyright Text setting.
  168.      *
  169.      * @uses $wp_customize->add_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/
  170.      * @link $wp_customize->add_control() https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
  171.      */
  172.     $wp_customize->add_control(
  173.         // $id
  174.         'footer_copyright_text',
  175.         // $args
  176.         array(
  177.             'settings'      => 'footer_copyright_text',
  178.             'section'       => 'theme_slug_section_footer',
  179.             'type'          => 'text',
  180.             'label'         => __( 'Footer Copyright Text', 'theme-slug' ),
  181.             'description'   => __( 'Copyright or other text to be displayed in the site footer. HTML allowed.', 'theme-slug' )
  182.         )
  183.     );
  184.    
  185.    
  186.     /**
  187.      * Basic Textarea control.
  188.      *
  189.      * - Control: Basic: Textarea
  190.      * - Setting: Custom CSS
  191.      * - Sanitization: css
  192.      *
  193.      * Register the core "textarea" control to be used to configure the Custom CSS setting.
  194.      *
  195.      * @uses $wp_customize->add_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/
  196.      * @link $wp_customize->add_control() https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
  197.      */
  198.     $wp_customize->add_control(
  199.         // $id
  200.         'custom_css',
  201.         // $args
  202.         array(
  203.             'settings'      => 'custom_css',
  204.             'section'       => 'theme_slug_section_css',
  205.             'type'          => 'textarea',
  206.             'label'         => __( 'Custom CSS', 'theme-slug' ),
  207.             'description'   => __( 'Define custom CSS be used for your site. Do not enclose in script tags.', 'theme-slug' ),
  208.         )
  209.     );
  210.    
  211.    
  212.     /**
  213.      * Basic Email control.
  214.      *
  215.      * - Control: Text: Email
  216.      * - Setting: Contact Email
  217.      * - Sanitization: email
  218.      *
  219.      * Register the core "email" text control to be used to configure the Contact Email setting.
  220.      *
  221.      * @uses $wp_customize->add_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/
  222.      * @link $wp_customize->add_control() https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
  223.      */
  224.     $wp_customize->add_control(
  225.         // $id
  226.         'contact_email',
  227.         // $args
  228.         array(
  229.             'settings'      => 'contact_email',
  230.             'section'       => 'theme_slug_section_footer',
  231.             'type'          => 'email',
  232.             'label'         => __( 'Contact Email', 'theme-slug' ),
  233.             'description'   => __( 'Contact email address to be displayed in the site footer.', 'theme-slug' )
  234.         )
  235.     );
  236.    
  237.    
  238.     /**
  239.      * Basic Number control.
  240.      *
  241.      * - Control: Text: Number
  242.      * - Setting: Slide Count
  243.      * - Sanitization: number_absint
  244.      *
  245.      * Register the core "number" text control to be used to configure the Slide Count setting.
  246.      *
  247.      * @uses $wp_customize->add_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/
  248.      * @link $wp_customize->add_control() https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
  249.      */
  250.     $wp_customize->add_control(
  251.         // $id
  252.         'slide_count',
  253.         // $args
  254.         array(
  255.             'settings'      => 'slide_count',
  256.             'section'       => 'theme_slug_section_frontpage',
  257.             'type'          => 'number',
  258.             'label'         => __( 'Slide Count', 'theme-slug' ),
  259.             'description'   => __( 'Set the number of sticky posts to display in the slider.', 'theme-slug' )
  260.         )
  261.     );
  262.    
  263.    
  264.     /**
  265.      * Basic Telephone control.
  266.      *
  267.      * - Control: Text: Tel
  268.      * - Setting: Contact Telephone
  269.      * - Sanitization: number_range
  270.      *
  271.      * Register the core "tel" text control to be used to configure the Contact Telephone setting.
  272.      *
  273.      * A valid telephone number is either 10 or 12 digits, so the control is passed attributes
  274.      * for min 10 and max 12, with step = 2.
  275.      *
  276.      * @uses $wp_customize->add_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/
  277.      * @link $wp_customize->add_control() https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
  278.      */
  279.     $wp_customize->add_control(
  280.         // $id
  281.         'contact_telephone',
  282.         // $args
  283.         array(
  284.             'settings'      => 'contact_telephone',
  285.             'section'       => 'theme_slug_section_footer',
  286.             'type'          => 'number',
  287.             'label'         => __( 'Contact Telephone', 'theme-slug' ),
  288.             'description'   => __( 'Contact phone number to be displayed in the site footer. Numbers only.', 'theme-slug' ),
  289.             'input_attrs'   => array(
  290.                 'min'   => 10,
  291.                 'max'   => 12,
  292.                 'step'  => 2
  293.             )
  294.         )
  295.     );
  296.    
  297.    
  298.     /**
  299.      * Basic URL control.
  300.      *
  301.      * - Control: Text: URL
  302.      * - Setting: Contact Link
  303.      * - Sanitization: url
  304.      *
  305.      * Register the core "url" text control to be used to configure the Contact Link setting.
  306.      *
  307.      * @uses $wp_customize->add_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/
  308.      * @link $wp_customize->add_control() https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_control
  309.      */
  310.     $wp_customize->add_control(
  311.         // $id
  312.         'contact_link',
  313.         // $args
  314.         array(
  315.             'settings'      => 'contact_link',
  316.             'section'       => 'theme_slug_section_footer',
  317.             'type'          => 'url',
  318.             'label'         => __( 'Contact Link', 'theme-slug' ),
  319.             'description'   => __( 'Contact link URL to be displayed in the site footer.', 'theme-slug' )
  320.         )
  321.     );
  322.  
  323. }
  324.  
  325. // Settings API options initilization and validation.
  326. add_action( 'customize_register', 'theme_slug_register_customizer_controls_basic' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement