Skip to content

Instantly share code, notes, and snippets.

@immanuelsun
Created June 15, 2016 00:31
Show Gist options
  • Save immanuelsun/6492b62ab369406b1aa4d255fa88ef69 to your computer and use it in GitHub Desktop.
Save immanuelsun/6492b62ab369406b1aa4d255fa88ef69 to your computer and use it in GitHub Desktop.
WordPress Customizer: add contact details section
// Add theme customizer controls, settings, etc.
function kingdom_customize_register($wp_customize)
{
/********************************************
! define genric controls
*******************************************/
/**
* create class to define textarea in Customizer
*/
class Kingdom_Customize_Textarea_Control extends WP_Customize_Control
{
public $type = 'textarea';
public function render_content()
{
echo '<label>';
echo '<span class="customize-control-title">';
echo esc_html( $this->label );
echo '</span>';
echo '<textarea name="" id="" cols="" rows="2" class="widefat" ';
$this->link();
echo '>';
esc_textarea( $this->value() );
echo '</textarea>';
echo '</label>';
}
}
/********************************************
! Contact Details
*********************************************/
/* add section */
$wp_customize->add_section( 'kingdom_contact', array(
'title' => __( 'Contact Details', 'kingdom' )
));
/**
* Address
*/
/* add setting */
$wp_customize->add_setting('kingdom_address_setting', array(
'default' => __( 'Your address', 'kingdom' )
));
/* add controls */
$wp_customize->add_control( new Kingdom_Customize_Textarea_Control(
$wp_customize,
'kingdom_address_setting',
array(
'label' => 'Address',
'section' => 'kingdom_contact',
'setting' => 'kingdom_address_setting'
)
));
/**
* Phone
*/
/* add setting */
$wp_customize->add_setting('kingdom_phone_setting', array(
'default' => __( 'Your phone number', 'kingdom' )
));
/* add controls */
$wp_customize->add_control( new Kingdom_Customize_Textarea_Control(
$wp_customize,
'kingdom_phone_setting',
array(
'label' => 'Phone Number',
'section' => 'kingdom_contact',
'setting' => 'kingdom_phone_setting'
)
));
/**
* Email
*/
/* add setting */
$wp_customize->add_setting('kingdom_email_setting', array(
'default' => __( 'Your email', 'kingdom' )
));
/* add controls */
$wp_customize->add_control( new Kingdom_Customize_Textarea_Control(
$wp_customize,
'kingdom_email_setting',
array(
'label' => 'Email',
'section' => 'kingdom_contact',
'setting' => 'kingdom_email_setting'
)
));
}
add_action('customize_register', 'kingdom_customize_register');
/********************************************
! add controls / content to theme
*********************************************/
function kingdom_display_contact_details_in_header()
{
$html = '';
$html .= '<address>';
// Address
$html .= '<p class="address">';
$html .= get_theme_mod( 'kingdom_address_setting', 'Your Address' );
$html .= '</p><!-- /.address -->';
// Phone
$html .= '<p class="phone">';
$html .= get_theme_mod( 'kingdom_phone_setting', 'Your phone number' );
$html .= '</p><!-- /.phone -->';
// Email
$html .= '<p class="email">';
$email = get_theme_mod( 'kingdom_email_setting', 'Your email address' );
$html .= '<a href="' . $email . ' ">';
$html .= $email;
$html .= '</a>';
$html .= '</p><!-- /.email -->';
$html .= '</address>';
echo $html;
}
add_action('kingdom_in_header', 'kingdom_display_contact_details_in_header');
load_template( get_template_directory() . '/includes/customizer.php' );
/* action hook for any content placed inside the right hand header, including the widget area. */
do_action ( 'kingdom_in_header' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment