Add Your Own Setting to the Customizer
Adds a "Site extras" section to Appearance > Customize with a footer note and a contact phone field that preview live, and makes them available with a [site_extra] shortcode, as a pattern for your own settings.
Opens WPIDE on
Code
add_action( 'customize_register', function ( $wp_customize ) {
$wp_customize->add_section( 'wpide_site_extras', array(
'title' => 'Site extras',
'priority' => 160,
) );
$wp_customize->add_setting( 'wpide_footer_note', array(
'default' => '',
'sanitize_callback' => 'wp_kses_post',
'transport' => 'refresh',
) );
$wp_customize->add_control( 'wpide_footer_note', array(
'label' => 'Footer note',
'section' => 'wpide_site_extras',
'type' => 'textarea',
) );
$wp_customize->add_setting( 'wpide_phone', array(
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
) );
$wp_customize->add_control( 'wpide_phone', array(
'label' => 'Contact phone',
'section' => 'wpide_site_extras',
'type' => 'tel',
) );
} );
add_action( 'init', function () {
add_shortcode( 'site_extra', function ( $atts ) {
$atts = shortcode_atts( array( 'key' => 'footer_note' ), $atts, 'site_extra' );
$key = 'phone' === $atts['key'] ? 'wpide_phone' : 'wpide_footer_note';
return wp_kses_post( (string) get_theme_mod( $key, '' ) );
} );
} );
Notes
Use [site_extra key="phone"] or get_theme_mod( 'wpide_phone' ) in templates; block themes need the Customize menu brought back to edit them.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.