Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

152 PART II • Designing and Developing WordPress Themes


// Add setting for kihon_footer_copyright
$wp_customize->add_setting( 'kihon_footer_copyright', array(
'default' => '© '. get_bloginfo( 'name' ),
) );

// Add control for kihon_footer_copyright
$wp_customize->add_control( 'kihon_footer_copyright', array(
'label' => 'Text in the footer',
'type' => 'text',
'section' => 'kihon_footer', // Add to section kihon_footer
) );

}
add_action( 'customize_register', 'kihon_theme_settings', 11 );

Are you done now? Well yes, the Footer Details box will indeed show up in the Theme
Customizer, and the data will be saved, but what is the point of such a live preview interface if
the feature isn’t implemented in the theme? With the help of get_theme_mod(), you’ll
output the data. This little code snippet in footer.php will output whatever text is saved in the
settings, and if no text is added, the default value (from where you added the setting) will be
there instead:

<?php echo get_theme_mod( 'kihon_footer_copyright' ); ?>

With get_theme_mod(), you can easily pick up whatever setting you have created by
passing the setting ID.

Now, you’re halfway there. There’s the box, and there’s the live preview of whatever copy you
put into the text field.

Let’s add another setting for the credit line below the copy. For this, you don’t need to create
the section again; you can just use the one you’ve got. Yes, this means that you can add
settings to any section, including the ones that are already available.

// Add setting for kihon_footer_credits
$wp_customize->add_setting( 'kihon_footer_credits', array(
'default' => 0,
) );

This setting is called kihon_footer_credits, and the default value is nil. That’s because
you’re going to make this one a check box — either you want to credit line with links to the
theme home page and WordPress.org or you don’t. Here’s the control that’ll make it all come
true:

// Add control for kihon_footer_credits
$wp_customize->add_control( 'kihon_footer_credits', array(
'label' => 'Hide credits from the footer',
'type' => 'checkbox',
'section' => 'kihon_footer', // Add to section kihon_footer
) );
Free download pdf