简体   繁体   中英

Wordpress customizer breaks with custom control

I'm hoping someone will help me find an answer to this. I am building a base child theme for the Genesis theme, & am adding to the Theme Customizer to allow quick setup of basic theme settings. Where I'm running into problems is that whenever I try to add a custom control the Customizer breaks. The site still works, just the customizer breaks. My code is this:

add_action( 'customize_register', 'um_register_theme_customizer' );
function um_register_theme_customizer( $wp_customize ) {

// Customizations that work here

I have setup basic color settings that all work. It breaks when I add this setting:

$wp_customize->add_setting(
    'um_h6_font_size',
    array(
        'default'     => '1.2'
    )
);
$wp_customize->add_control(
        new UM_Customize_Number_Control(
        'um_h6_font_size',
        array(
            'label'      => __( 'H6 Font Size (in rem)', 'um' ),
            'section'    => 'um_font_size_options',
            'settings'   => 'um_h6_font_size',
            'type' => 'number'
        )
        )
);

// Classes 
class UM_Customize_Number_Control extends WP_Customize_Control
{
    public $type = 'number';

    public function render_content()
    {
        ?>
        <label>
            <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
            <input type="number" size="2" step="1" min="0" value="<?php echo esc_attr(  $this->value() ); ?>" />
        </label>
        <?php
    }
}
} // Closes the um_register_theme_customizer function

If I remove the custom control then it works, defaulting to a text input (changing the type to text). What I'm trying to do is to make this a number field.

I was trying to use the same method to make a simple section of h6 text in the customizer for sub-headings, but was running into the same issue.

Any help as to why this isn't working would be greatly appreciated. I'm sure I'm missing something simple.

The answer to this was simple. You cannot set up a custom control inside of the main customizer function. It has to be added outside of that function. ie I now have a separate php file for custom controls that is included in my main functions file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM