WP Customizer в своей теме

Либа для дополнительных типов полей Cody Framework:

скачать Cody framework

Общая заготовка

*с использованием Cody Framework

Открыть

        
function  theme_customize_preview() {
    ? >
    < script type="text/javascript">
        jQuery( function( $ ) {
            wp.customize( 'test_textbox', function( value ) {
                value.bind( function( to ) {
                    $( '.test' ).text( to ); // в элемент #copyright помещаем текст
                });
            });

        } )
    
    is_preview() && ! is_admin() ) {
    //для js-а
        add_action( 'wp_footer', 'theme_customize_preview', 21);
    }
    
    $wp_customize->add_section(
        'header_section',
        array(
            'title' => 'Header сайта',
            'description' => '',
            'priority' => 250,
        )
    );
  //функции полей
  
}

add_action('customize_register', 'theme_customizer');

        
    

Image field

Открыть

        
/*
 * Поле картинки изображения
 */
function field_image ($name , $label  , $selector,$default = '' , $section) {

    global $wp_customize;
    $transport = 'postMessage';
    $setting_name = $name;
    $wp_customize->add_setting(
        $setting_name,
        array(
            'default' => $default,
            'height'    => 325,
            'transport' => $transport,
        )
    );
    // Add Controls
    $wp_customize->add_control(
        new WP_Customize_Image_Control(
            $wp_customize,
            $setting_name,
            array(
                'label'             => $label,
                'section'     => $section,
                'settings'          =>  $setting_name,
            )));

    $wp_customize->selective_refresh->add_partial($setting_name, array(
        'selector' => $selector, //должен содержать class или id элемента с текстом
        'render_callback' => function() use ($setting_name) {
            return nl2br(get_theme_mod($setting_name));
        }
    ));
}
        
    

Text field

Открыть

        
/*
 * Поле текста
 */
function field_text($name , $label , $selector,$default = '', $section) {
    global $wp_customize;
    $transport = 'postMessage';
    $setting_name = $name;


    $wp_customize->add_setting(
        $setting_name,
        array(
            'default' => $default,
            'sanitize_callback' => 'theme_sanitize_text',
            'transport' => $transport,
        )
    );
    $wp_customize->add_control(
        new CF_Textarea_Control (
            $wp_customize,
            $setting_name,
            array(
                'label'       => $label,
                'description' => '',
                'section'     => $section,
                'type'        => 'text',
                //'relation'    => 'children'    // This parameter specifies a relationship to CF_Switch_Control
            )
        )
    );
    $wp_customize->selective_refresh->add_partial($setting_name, array(
        'selector' => $selector, //должен содержать class или id элемента с текстом
        'render_callback' => function() use ($setting_name) {
            return nl2br(get_theme_mod($setting_name));
        }
    ));

}
        
    

Textarea field

Открыть

        
/*
 * Поле текстовой области
 */
function field_textarea($name , $label , $selector,$default = '', $section) {
    global $wp_customize;
    $transport = 'postMessage';
    $setting_name = $name;


    $wp_customize->add_setting(
        $setting_name,
        array(
            'default' => $default,
            'sanitize_callback' => 'theme_sanitize_text',
            'transport' => $transport,
        )
    );
    $wp_customize->add_control(
        new CF_Textarea_Control (
            $wp_customize,
            $setting_name,
            array(
                'label'       => $label,
                'description' => '',
                'section'     => $section,
                'type'        => 'textarea',
                //'relation'    => 'children'    // This parameter specifies a relationship to CF_Switch_Control
            )
        )
    );
    $wp_customize->selective_refresh->add_partial($setting_name, array(
        'selector' => $selector, //должен содержать class или id элемента с текстом 
        'render_callback' => function() use ($setting_name) {
            return nl2br(get_theme_mod($setting_name));
        }
    ));

}
        
    

HTML-editor field

Открыть

        
/*
 * Поле редактора кодом
 */
function field_codeeditor($name , $label , $selector,$default = '', $section) {
    global $wp_customize;
    $transport = 'postMessage';
    $setting_name = $name;


    $wp_customize->add_setting(
        $setting_name,
        array(
            'default' => $default,
            'sanitize_callback' => 'theme_sanitize_text',
            'transport' => $transport,
        )
    );
    $wp_customize->add_control(
        new WP_Customize_Code_Editor_Control(
            $wp_customize,
            $setting_name,
            array(
                'label'    =>  $label,
                'section' => $section,
            ) ) );

    $wp_customize->selective_refresh->add_partial($setting_name, array(
        'selector' => $selector, //должен содержать class или id элемента с текстом в подвале
        'render_callback' => function() use ($setting_name) {
            return nl2br(get_theme_mod($setting_name));
        }
    ));

}
        
    

WP TextEditor field

Открыть

        

/*
 * Поле текстового редактора
 */
function field_wptexteditor($name , $label , $selector,$default = '', $section) {
    global $wp_customize;
    $transport = 'postMessage';
    $setting_name = $name;


    $wp_customize->add_setting(
        $setting_name,
        array(
            'default' => $default,
           // 'sanitize_callback' => 'theme_sanitize_text',
            'transport' => $transport,
        )
    );
    $wp_customize->add_control(
        new CF_WPEditor_Control(
            $wp_customize, $setting_name,
            array(
                'label'       => $label,
                'description' => '',
                'section'     => $section,
                'type'        => 'wp_editor',
                'params'      => array(
                    'textarea_rows'    => 10,
                    'media_buttons'    => false,
                    'drag_drop_upload' => false,
                    'teeny'            => true,
                    'quicktags'        => false,
                ),
                'relation'    => 'children'    // This parameter specifies a relationship to CF_Switch_Control
            )
        )
    );

    $wp_customize->selective_refresh->add_partial($setting_name, array(
        'selector' => $selector, //должен содержать class или id элемента с текстом в подвале
        'render_callback' => function() use ($setting_name) {
            return nl2br(get_theme_mod($setting_name));
        }
    ));

}
        
    
Тэги:

Комментарии (0)


Оставить комментарий

Success/Error Message Goes Here

TOP