1

I've set up a new section in the customizer called Social Links, under which I've set up an option and text field control called Facebook. Here's the code in my functions.php:

// SOCIAL LINKS
$wp_customize->add_section( 'social_links_section' , array(
    'title'      => __( 'Social Links', 'my_theme' ),
    'priority'   => 100,
) );
// Facebook
$wp_customize->add_setting( 'social_link_facebook' , array(
    'default' => ''
) );
$wp_customize->add_control(
    'social_link_facebook', 
    array(
        'label'    => __( 'Facebook', 'my_theme' ),
        'section'  => 'social_links_section',
        'settings' => 'social_link_facebook',
        'type'     => 'url',
    )
);

But when I try to get the value of social_link_facebook in my theme template files, it isn't returning anything. Here's the code from my header.php:

<?php $facebook = get_option( 'social_link_facebook' );
if( !empty($facebook) ) { ?>
    <a href="<?php echo $facebook ?>" target="_blank" ><span class="dashicons dashicons-facebook-alt"></span></a>
<?php } ?>

Any idea why this might be?

  • You should use get_theme_mod. – Luis Sanz May 23 '16 at 14:49
  • @LuisSanz Thanks! I wasn't expecting it to be that simple, now I feel stupid :P – AT92 May 23 '16 at 15:08
0

There are two ways to solve this:

1 Keep the mods as they are and use get_theme_mod in stead of get_option

2 Store the mods as options by changing the setting:

$wp_customize->add_setting( 'social_link_facebook' , array(
    'default' => '',
    'type' => 'option'
) );
  • Thanks, I used get_theme_mod and that did the trick :) – AT92 May 23 '16 at 15:23

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.