2

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' );
}
4

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'];
  • That didn't work for me, same thing as before. – MisterBla May 17 '15 at 11:31
  • Above code only solves the customizer options issue. Value will be saved properly. If you are talking about your shortcode function then there are lots of mistakes. In the question you were asking about not saving options. codex.wordpress.org/Shortcode_API – Nilambar May 18 '15 at 3:44
  • I don't think the issue is with my shortcode. I tried using get_theme_mod( 'ta_pluton_social_link[facebook]', '#' ) on its own, where the expected value is #Facebook as I defined in the customization, but the actual value is #, which is the default value. – MisterBla May 18 '15 at 4:41
  • Check the updated answer. It shows how to get social URL. – Nilambar May 18 '15 at 5:39
  • I found that you need to pass the name of the associated setting, see the settings property in the array. – MisterBla May 30 '15 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.