get_theme_mod always returns default

I’m working on a website of a client and I’m trying to add social media links to the customizer, which then can be called through get_theme_mod, via a shortcode. However, when trying to call them, I always get the default.

When I checked get_theme_mods, they didn’t seem to be defined at all… However, in the theme customizer, the fields are there.

I tried using the following questions, but that didn’t help me:
get_theme_mod(); returns nothing
https://wordpress.stackexchange.com/questions/144544/get-theme-mod-return-a-blank-value-instead-of-saved-value

The added customizer code is

// Add social links setting
$wp_customize->add_section( 'ta_pluton_social_links', array(
    'title'       => __( 'Social Media Links', 'ta_pluton' ),
    'description' => __( 'Links for the Social Media buttons on the home page.', 'ta_pluton' ),
    'priority'    => 10
) );

$social_links_default_settings = array(
        'default' => '#',
);

$wp_customize->add_setting('ta_pluton_social_link[facebook]', $social_links_default_settings);
$wp_customize->add_setting('ta_pluton_social_link[twitter]', $social_links_default_settings);
$wp_customize->add_setting('ta_pluton_social_link[googleplus]', $social_links_default_settings);
$wp_customize->add_setting('ta_pluton_social_link[tumblr]', $social_links_default_settings);
$wp_customize->add_setting('ta_pluton_social_link[youtube]', $social_links_default_settings);

$wp_customize->add_control('ta_pluton_social_link[facebook]', array(
    'label'      => __('Facebook URL', 'ta_pluton'),
    'section'    => 'ta_pluton_social_links',
    'settings'   => 'ta_pluton_social_link[facebook]',
));

$wp_customize->add_control('ta_pluton_social_link[twitter]', array(
    'label'      => __('Twitter URL', 'ta_pluton'),
    'section'    => 'ta_pluton_social_links',
    'settings'   => 'ta_pluton_social_link[twitter]',
));

$wp_customize->add_control('ta_pluton_social_link[googleplus]', array(
    'label'      => __('Google+ URL', 'ta_pluton'),
    'section'    => 'ta_pluton_social_links',
    'settings'   => 'ta_pluton_social_link[googleplus]',
));

$wp_customize->add_control('ta_pluton_social_link[tumblr]', array(
    'label'      => __('Tumblr URL', 'ta_pluton'),
    'section'    => 'ta_pluton_social_links',
    'settings'   => 'ta_pluton_social_link[tumblr]',
));

$wp_customize->add_control('ta_pluton_social_link[youtube]', array(
    'label'      => __('YouTube URL', 'ta_pluton'),
    'section'    => 'ta_pluton_social_links',
    'settings'   => 'ta_pluton_social_link[youtube]',
));

And this is the shortcode function:

/**
 * Add a social media link shortcode
 */
if( !function_exists( 'ta_pluton_social_media_shortcode' ) ) {
    function ta_pluton_social_media_shortcode( $atts ) {
        $attributes = shortcode_atts( array(
            'default' => '#'
        ), $atts );

        if ( !array_key_exists( 'media' ) ) {
            trigger_error('The social_media_link shortcode requires a medium to be selector, eg: [social_media_link media="facebook"]' );
        }

        return get_theme_mod( 'ta_pluton_social_link[' . strtolower( $attributes['media'] ) . ']', $attributes['default'] ); 
    }
    add_shortcode( 'social_media_link', 'ta_pluton_social_media_shortcode' );
}

1 Answer
1

In the Customizer code you need to do following change to work. You dont need ta_pluton_social_link[facebook] like array in Control. Check following example and modify accordingly to other fields also.

  $wp_customize->add_control('facebook', array(
      'label'      => __('Facebook URL', 'ta_pluton'),
      'section'    => 'ta_pluton_social_links',
      'settings'   => 'ta_pluton_social_link[facebook]',
  ));

To fetch URL you can use get_theme_mod. Check example below. Following snippet demonstrates fetching Facebook URL.

$ta_pluton_social_link = get_theme_mod( 'ta_pluton_social_link' );
$facebook_url = $ta_pluton_social_link['facebook'];

Leave a Comment