If your custom control’s render_content()
is not being called, the most likely cause is either a PHP error preventing the class from being loaded, or the file is not being included at all. Double-check your file paths and logs. Otherwise, your approach is solid and follows best practices for custom controls in the WordPress Customizer.
Minimal Working Example
1. Add to your theme’s functions.php
or a customizer file:
add_action( 'customize_register', 'zero_register_typography_control' ); function zero_register_typography_control( $wp_customize ) { require_once get_template_directory() . '/inc/customizer/controls/class-zero-control-typography.php';
$wp_customize->add_section( 'zero_typography_section', [ 'title' => __( 'Typography', 'zero' ), 'priority' => 10, ] ); $presets = [ 'playfair-open-sans' => esc_html__( 'Playfair Display / Open Sans', 'zero' ), ]; $wp_customize->add_setting( 'zero_typography_preset', [ 'default' => 'playfair-open-sans', 'sanitize_callback' => function( $val ) use ( $presets ) { return isset( $presets[ $val ] ) ? $val : 'playfair-open-sans'; }, 'transport' => 'postMessage', ] ); $wp_customize->add_control( new Zero_Control_Typography( $wp_customize, 'zero_typography_preset', [ 'label' => __( 'Font Presets', 'zero' ), 'section' => 'zero_typography_section', 'choices' => $presets, ] ) );
}
2. Place this in
/inc/customizer/controls/class-zero-control-typography.php
:if ( ! class_exists( 'WP_Customize_Control' ) ) return; class Zero_Control_Typography extends WP_Customize_Control { public $type="typography"; public function render_content() { if ( empty( $this->choices ) ) return; echo '<span class="customize-control-title">' . esc_html( $this->label ) . '</span>'; echo '<ul>'; foreach ( $this->choices as $slug => $name ) { $checked = checked( $this->value(), $slug, false ); list( $h, $b ) = explode( '-', $slug, 2 ); printf( '<li><label class="preset-card">' . '<input type="radio" data-customize-setting-link="%1$s" value="%2$s"%3$s />' . '<span class="preset-card__heading" style="font-family:\'%4$s\';">Heading</span>' . '<span class="preset-card__body" style="font-family:\'%5$s\';">Body text</span>' . '</label></li>', esc_attr( $this->id ), esc_attr( $slug ), $checked, esc_attr( ucwords( str_replace('-', ' ', $h)) ), esc_attr( ucwords( str_replace('-', ' ', $b)) ) ); } echo '</ul>'; } }