How to Enable Social Icons in the WordPress Theme Customizer

I figure I might as well write about this before I deprecate it in my upcoming Theme. Social Nav Menus are much better.

A week or two ago as I was getting deep into developing a new WordPress Theme, I stumbled upon some pretty good looking code that would allow me to add Social Media Icons to the Theme Customizer like so:

theme_customizer_social_icons

At the time, it was a superb idea. I was just learning about all the possibilities the Theme Customizer affords developers and it was fun to implement the code and watch it work so smoothly.

However, after watching Morten Rand-Hendriksen’s Building Themes from Scratch Using Underscores and reading Justin Tadlock’s post on building Social Nav menus, I think I will scratch this bit of code in favor of Tadlock’s solution.

It just makes sense to not want to have to re-enter your Social Media links in every new Theme you install. Why not just leave all the Social links you want in a nav menu and use your Theme to style and output it specifically as a Social menu?

TwentyFifteen also does just that. (See #3 in this post.)

Nevertheless, I felt that the code itself was worth my time to invest in learning, so I’m reproducing it here for reference. This code is modified slightly from the example I found – I’ve included email as an option. It should be pretty easy to see how it’s done:

add_section(), add_setting(), add_control()

 /**
 * Social site icons for Quick Menu bar
 * 
 * @link: https://www.competethemes.com/social-icons-wordpress-menu-theme-customizer/
 */
 $wp_customize->add_section( 'social_settings', array(
     'title' => __( 'Social Media Icons', 'theme_slug' ),
     'priority' => 100,
 ));
 
 $social_sites = theme_slug_get_social_sites();
 $priority = 5;
 
 foreach( $social_sites as $social_site ) {
 
     $wp_customize->add_setting( "$social_site", array(
         'type' => 'theme_mod',
         'capability' => 'edit_theme_options',
         'sanitize_callback' => 'esc_url_raw',
     ));
 
     $wp_customize->add_control( $social_site, array(
         'label' => ucwords( __( "$social_site URL:", 'social_icon' ) ),
         'section' => 'social_settings',
         'type' => 'text',
         'priority' => $priority,
     ));
 
     $priority += 5;
 }

function theme_slug_get_social_sites()

/**
 * Social Media icon helper functions
 * 
 * @return array
 * 
 * @link: https://www.competethemes.com/social-icons-wordpress-menu-theme-customizer/
 */
function theme_slug_get_social_sites() {
 
     // Store social site names in array
     $social_sites = array(
         'twitter', 
         'facebook', 
         'google-plus',
         'flickr',
         'pinterest', 
         'youtube',
         'vimeo',
         'tumblr',
         'dribbble',
         'rss',
         'linkedin',
         'instagram',
         'email'
     );
 return $social_sites;
}

function theme_slug_show_social_icons()

// Get user input from the Customizer and output the linked social media icons
function theme_slug_show_social_icons() {
 
     $social_sites = theme_slug_get_social_sites();
 
     // Any inputs that aren't empty are stored in $active_sites array
     foreach( $social_sites as $social_site ) {
         if ( strlen( get_theme_mod( $social_site ) ) > 0 ) {
             $active_sites[] = $social_site;
         }
     }
 
     // For each active social site, add it as a list item
     if ( !empty( $active_sites ) ) {
         echo "<ul class='social-media-icons'>";
 
         foreach ( $active_sites as $active_site ) { ?>

             <li>
             <a href="<?php echo get_theme_mod( $active_site ); ?>">
             <?php if( $active_site == 'vimeo' ) { ?>
                 <i class="fa fa-<?php echo $active_site; ?>-square"></i> <?php
             } else if( $active_site == 'email' ) { ?>
                 <i class="fa fa-envelope"></i> <?php
             } else { ?>
                 <i class="fa fa-<?php echo $active_site; ?>"></i> <?php
             } ?>
             </a>
             </li> <?php
         }
         echo "</ul>";
     }
}

Oh, and obviously you’ll need to remember to enqueue FontAwesome in your WordPress Theme in order to make this all function properly. Then, the CSS styling is totally up to you.

Default WordPress Theme Customizer Controls

I’ve recently been messing around with the WP Theme Customizer a bunch and have been looking for the names of all the default settings, sections, and controls. I found them in a file in the wp-includes folder:

/wp-includes/class-wp-customize-manager.php

The various default section names, setting names, and 'priority' are listed below.This is mostly useful if you’re trying to simply rearrange the position ('priority' => 20) of some options, or want to rename some of them like so:

$wp_customize->get_section( 'nav' )->title = __( 'Menus', 'theme_slug' );
$wp_customize->get_control( 'blogname' )->priority = 10;
$wp_customize->get_setting( 'background_color' )->default = '#169A70';
  1. Site Title & Tagline ('title_tagline'), 20
    1. 'blogname'
    2. 'blogdescription'
    3. 'display_header_text'
  2. Colors ('colors'), 40
    1. 'header_textcolor'
    2. 'background_color'
  3. Header Image ('header_image'), 60
    1. 'header_image'
    2. 'header_image_data'
  4. Background Image ('background_image'), 80
    1. 'background_image_thumb'
    2. 'background_repeat'
    3. 'background_position_x'
    4. 'background_attachment'
  5. Navigation ('nav'), 100
    1. "nav_menu_locations[{$location}]"
  6. Widgets ('widgets'), 110
  7. Static Front Page ('static_front_page'), 120
    1. 'show_on_front'
    2. 'page_on_front'
    3. 'page_for_posts'

The code specifically for 'widgets' is located in:

/wp-includes/class-wp-customize-widgets.php

The full code for the default Theme Customizer settings is here:

 /**
 * Register some default controls.
 *
 * @since 3.4.0
 */
 public function register_controls() {

 /* Control Types (custom control classes) */
 $this->register_control_type( 'WP_Customize_Color_Control' );
 $this->register_control_type( 'WP_Customize_Upload_Control' );
 $this->register_control_type( 'WP_Customize_Image_Control' );
 $this->register_control_type( 'WP_Customize_Background_Image_Control' );

 /* Site Title & Tagline */

 $this->add_section( 'title_tagline', array(
 'title' => __( 'Site Title & Tagline' ),
 'priority' => 20,
 ) );

 $this->add_setting( 'blogname', array(
 'default' => get_option( 'blogname' ),
 'type' => 'option',
 'capability' => 'manage_options',
 ) );

 $this->add_control( 'blogname', array(
 'label' => __( 'Site Title' ),
 'section' => 'title_tagline',
 ) );

 $this->add_setting( 'blogdescription', array(
 'default' => get_option( 'blogdescription' ),
 'type' => 'option',
 'capability' => 'manage_options',
 ) );

 $this->add_control( 'blogdescription', array(
 'label' => __( 'Tagline' ),
 'section' => 'title_tagline',
 ) );

 /* Colors */

 $this->add_section( 'colors', array(
 'title' => __( 'Colors' ),
 'priority' => 40,
 ) );

 $this->add_setting( 'header_textcolor', array(
 'theme_supports' => array( 'custom-header', 'header-text' ),
 'default' => get_theme_support( 'custom-header', 'default-text-color' ),

 'sanitize_callback' => array( $this, '_sanitize_header_textcolor' ),
 'sanitize_js_callback' => 'maybe_hash_hex_color',
 ) );

 // Input type: checkbox
 // With custom value
 $this->add_control( 'display_header_text', array(
 'settings' => 'header_textcolor',
 'label' => __( 'Display Header Text' ),
 'section' => 'title_tagline',
 'type' => 'checkbox',
 ) );

 $this->add_control( new WP_Customize_Color_Control( $this, 'header_textcolor', array(
 'label' => __( 'Header Text Color' ),
 'section' => 'colors',
 ) ) );

 // Input type: Color
 // With sanitize_callback
 $this->add_setting( 'background_color', array(
 'default' => get_theme_support( 'custom-background', 'default-color' ),
 'theme_supports' => 'custom-background',

 'sanitize_callback' => 'sanitize_hex_color_no_hash',
 'sanitize_js_callback' => 'maybe_hash_hex_color',
 ) );

 $this->add_control( new WP_Customize_Color_Control( $this, 'background_color', array(
 'label' => __( 'Background Color' ),
 'section' => 'colors',
 ) ) );


 /* Custom Header */

 $this->add_section( 'header_image', array(
 'title' => __( 'Header Image' ),
 'theme_supports' => 'custom-header',
 'priority' => 60,
 ) );

 $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
 'default' => get_theme_support( 'custom-header', 'default-image' ),
 'theme_supports' => 'custom-header',
 ) ) );

 $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
 // 'default' => get_theme_support( 'custom-header', 'default-image' ),
 'theme_supports' => 'custom-header',
 ) ) );

 $this->add_control( new WP_Customize_Header_Image_Control( $this ) );

 /* Custom Background */

 $this->add_section( 'background_image', array(
 'title' => __( 'Background Image' ),
 'theme_supports' => 'custom-background',
 'priority' => 80,
 ) );

 $this->add_setting( 'background_image', array(
 'default' => get_theme_support( 'custom-background', 'default-image' ),
 'theme_supports' => 'custom-background',
 ) );

 $this->add_setting( new WP_Customize_Background_Image_Setting( $this, 'background_image_thumb', array(
 'theme_supports' => 'custom-background',
 ) ) );

 $this->add_control( new WP_Customize_Background_Image_Control( $this ) );

 $this->add_setting( 'background_repeat', array(
 'default' => get_theme_support( 'custom-background', 'default-repeat' ),
 'theme_supports' => 'custom-background',
 ) );

 $this->add_control( 'background_repeat', array(
 'label' => __( 'Background Repeat' ),
 'section' => 'background_image',
 'type' => 'radio',
 'choices' => array(
 'no-repeat' => __('No Repeat'),
 'repeat' => __('Tile'),
 'repeat-x' => __('Tile Horizontally'),
 'repeat-y' => __('Tile Vertically'),
 ),
 ) );

 $this->add_setting( 'background_position_x', array(
 'default' => get_theme_support( 'custom-background', 'default-position-x' ),
 'theme_supports' => 'custom-background',
 ) );

 $this->add_control( 'background_position_x', array(
 'label' => __( 'Background Position' ),
 'section' => 'background_image',
 'type' => 'radio',
 'choices' => array(
 'left' => __('Left'),
 'center' => __('Center'),
 'right' => __('Right'),
 ),
 ) );

 $this->add_setting( 'background_attachment', array(
 'default' => get_theme_support( 'custom-background', 'default-attachment' ),
 'theme_supports' => 'custom-background',
 ) );

 $this->add_control( 'background_attachment', array(
 'label' => __( 'Background Attachment' ),
 'section' => 'background_image',
 'type' => 'radio',
 'choices' => array(
 'scroll' => __('Scroll'),
 'fixed' => __('Fixed'),
 ),
 ) );

 // If the theme is using the default background callback, we can update
 // the background CSS using postMessage.
 if ( get_theme_support( 'custom-background', 'wp-head-callback' ) === '_custom_background_cb' ) {
 foreach ( array( 'color', 'image', 'position_x', 'repeat', 'attachment' ) as $prop ) {
 $this->get_setting( 'background_' . $prop )->transport = 'postMessage';
 }
 }

 /* Nav Menus */

 $locations = get_registered_nav_menus();
 $menus = wp_get_nav_menus();
 $num_locations = count( array_keys( $locations ) );

 $this->add_section( 'nav', array(
 'title' => __( 'Navigation' ),
 'theme_supports' => 'menus',
 'priority' => 100,
 'description' => sprintf( _n('Your theme supports %s menu. Select which menu you would like to use.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations ), number_format_i18n( $num_locations ) ) . "\n\n" . __('You can edit your menu content on the Menus screen in the Appearance section.'),
 ) );

 if ( $menus ) {
 $choices = array( 0 => __( '&mdash; Select &mdash;' ) );
 foreach ( $menus as $menu ) {
 $choices[ $menu->term_id ] = wp_html_excerpt( $menu->name, 40, '&hellip;' );
 }

 foreach ( $locations as $location => $description ) {
 $menu_setting_id = "nav_menu_locations[{$location}]";

 $this->add_setting( $menu_setting_id, array(
 'sanitize_callback' => 'absint',
 'theme_supports' => 'menus',
 ) );

 $this->add_control( $menu_setting_id, array(
 'label' => $description,
 'section' => 'nav',
 'type' => 'select',
 'choices' => $choices,
 ) );
 }
 }

 /* Static Front Page */
 // #WP19627

 $this->add_section( 'static_front_page', array(
 'title' => __( 'Static Front Page' ),
 // 'theme_supports' => 'static-front-page',
 'priority' => 120,
 'description' => __( 'Your theme supports a static front page.' ),
 ) );

 $this->add_setting( 'show_on_front', array(
 'default' => get_option( 'show_on_front' ),
 'capability' => 'manage_options',
 'type' => 'option',
 // 'theme_supports' => 'static-front-page',
 ) );

 $this->add_control( 'show_on_front', array(
 'label' => __( 'Front page displays' ),
 'section' => 'static_front_page',
 'type' => 'radio',
 'choices' => array(
 'posts' => __( 'Your latest posts' ),
 'page' => __( 'A static page' ),
 ),
 ) );

 $this->add_setting( 'page_on_front', array(
 'type' => 'option',
 'capability' => 'manage_options',
 // 'theme_supports' => 'static-front-page',
 ) );

 $this->add_control( 'page_on_front', array(
 'label' => __( 'Front page' ),
 'section' => 'static_front_page',
 'type' => 'dropdown-pages',
 ) );

 $this->add_setting( 'page_for_posts', array(
 'type' => 'option',
 'capability' => 'manage_options',
 // 'theme_supports' => 'static-front-page',
 ) );

 $this->add_control( 'page_for_posts', array(
 'label' => __( 'Posts page' ),
 'section' => 'static_front_page',
 'type' => 'dropdown-pages',
 ) );
 }