3

PHP newbie here, and I have this on my functions.php

function mcs_social($wp_customize){

    $wp_customize->add_section('mcs_social_handle', array(
        'title'    => __('Social Network Handles', 'mcs'),
        'description' => 'i.e., Acme Company\'s Facebook is https://facebook.com/acmecompany then enter "acmecompany"',
        'priority' => 70,
    ));

    //  =============================
    //  = Facebook                  =
    //  =============================
    $wp_customize->add_setting('mcs_fb_op', array(
        'default'        => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'option',

    ));

    $wp_customize->add_control('mcs_fb', array(
        'label'      => __('Facebook Handle', 'mcs'),
        'section'    => 'mcs_social_handle',
        'settings'   => 'mcs_fb_op',
    ));

        //  =============================
    //  = Twitter                  =
    //  =============================
    $wp_customize->add_setting('mcs_tw_op', array(
        'default'        => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'option',

    ));

    $wp_customize->add_control('mcs_tw', array(
        'label'      => __('Twitter Handle', 'mcs'),
        'section'    => 'mcs_social_handle',
        'settings'   => 'mcs_tw_op',
    ));

    //  =============================
    //  = RSS                       =
    //  =============================
     $wp_customize->add_setting('mcs_rs_op', array(
        'default'        => 'rss2_url',
        'capability'     => 'edit_theme_options',
        'type'           => 'option',

    ));
    $wp_customize->add_control( 'mcs_rs', array(
        'settings' => 'mcs_rs_op',
        'label'   => 'RSS Feed',
        'section' => 'mcs_social_handle',
        'type'    => 'select',
                'choices'       => array(
                    'rdf_url'       => 'RDF/RSS 1.0 feed',
                    'rss2_url'  => 'RSS 2.0 feed',
                    'atom_url'  => 'Atom feed',
        ),
    ));
}

//add
add_action( 'customize_register', 'mcs_social' );

As you can see, it's a blatant copy of the sample in the WordPress Codex.

Now, that works perfectly. When I go to appearance -> customise, I see the social handle section and can add and save to textbox and select from the select.

My problem now is, I can't seem to grab the values to use in the templates. I have other customizer sections with 1 setting in the customizer (i.e. the Logo image) and they work just okay, but when I use the same get_theme_mod() for the "social" settings nothing comes up.

<div class="blue-social-box">
          <h5>Follow us on:</h5>
          <ul>
          <?php if ( get_theme_mod( 'mcs_fb_op' ) ) : ?>
            <li>
              <figure><a href="https://www.facebook.com/<?php echo esc_url( get_theme_mod( 'mcs_fb_op' ) ); ?>" target="_blank"><img src="<?php echo $fbImg; ?>" /></a></figure>
              <label><a href="https://www.facebook.com/<?php echo esc_url( get_theme_mod( 'mcs_fb_op' ) ); ?>" target="_blank">Facebook</a></label>
            </li>
          <?php elseif ( get_theme_mod( 'mcs_fb_op' ) ) : ?>
                        <li>
              <figure><a href="https://www.twitter.com/<?php echo esc_url( get_theme_mod( 'mcs_tw_op' ) ); ?>" target="_blank"><img src="<?php echo $twImg; ?>" /></a></figure>
              <label><a href="https://www.twitter.com/<?php echo esc_url( get_theme_mod( 'mcs_tw_op' ) ); ?>" target="_blank">Twitter</a></label>
            </li>
          <?php 
                    elseif ( get_theme_mod( 'mcs_rs_op' ) ) : 
                    $rssType = esc_url(get_theme_mod('mcs_rs_op'));
                        ?>
                        <li>
              <figure><a href="<?php bloginfo($rssType); ?>" target="_blank"><img src="<?php echo $rsImg; ?>" /></a></figure>
              <label><a href="<?php bloginfo($rssType); ?>" target="_blank">RSS Feed</a></label>
            </li>
                    <?php else : //Nothing ?>
          <?php endif; ?>
          </ul>
        </div>

I didn't find anything in the documentation helpful, so what is the correct way for grabbing these values?

4

Your code is good except in add_setting. You have assigned option for type parameter. It will save separate option for each field. If you use theme_mod, it will solve your problem.

'type' => 'theme_mod',

  • well, waddya' know, it worked. There were other problems in my code, but that was the easy part to solve. Thanks a bunch again! – vm7488 May 1 '15 at 4:25
  • Glad that it worked. If it solved the issue then you can accept my answer. Thanks. – Nilambar May 1 '15 at 5:26

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.