當(dāng)前位置:首頁(yè)>WordPress建站>WordPress開發(fā)>WordPress 外觀-自定義(Customizer)選項(xiàng)字段數(shù)據(jù)清理示例

WordPress 外觀-自定義(Customizer)選項(xiàng)字段數(shù)據(jù)清理示例

您可能已經(jīng)知道,主題開發(fā)人員可以使用WordPress Customizer API?為其主題創(chuàng)建設(shè)置,從而允許網(wǎng)站所有者微調(diào)配色方案、背景圖像和其他自定義選項(xiàng)等內(nèi)容,并實(shí)時(shí)查看這些更改的預(yù)覽。

由于我們永遠(yuǎn)不應(yīng)該信任用戶輸入,因此定制器 API 需要為每個(gè)設(shè)置定義一個(gè)回調(diào)函數(shù)來(lái)驗(yàn)證和清理輸入。下面的代碼示例將演示如何為各種數(shù)據(jù)類型定義清理回調(diào)函數(shù)。為了方便起見,代碼還包括在主題定制器中添加部分和設(shè)置的方法。

清理單選框

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
          
      
    //radio box sanitization function
        function theme_slug_sanitize_radio( $input, $setting ){
          
            //input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only
            $input = sanitize_key($input);
  
            //get the list of possible radio box options 
            $choices = $setting->manager->get_control( $setting->id )->choices;
                              
            //return input if valid or return default option
            return ( array_key_exists( $input, $choices ) ? $input : $setting->default );                
              
        }
      
      
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_radio', 
            array(
                'sanitize_callback' => 'theme_slug_sanitize_radio'
            )
        );
          
        $wp_customize->add_control( 
            'theme_slug_customizer_radio', 
            array(
                'label' => esc_html__( 'Your Setting with Radio Box', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'radio',
                'choices' => array(
                    'one' => esc_html__('Choice One','theme_slug'),
                    'two' => esc_html__('Choice Two','theme_slug'),
                    'three' => esc_html__('Choice Three','theme_slug')               
                )
            )
        );      
      
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理多選框

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
          
      
    //checkbox sanitization function
        function theme_slug_sanitize_checkbox( $input ){
              
            //returns true if checkbox is checked
            return ( isset( $input ) ? true : false );
        }
      
      
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_checkbox', 
            array(
                'default' => '',
                'sanitize_callback' => 'theme_slug_sanitize_checkbox'
            )
        );
          
        $wp_customize->add_control( 
            'theme_slug_customizer_checkbox', 
            array(
                'label' => esc_html__( 'Your Setting with Checkbox', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'checkbox'
            )
        );      
      
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理 select 選項(xiàng)

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
          
      
    //select sanitization function
        function theme_slug_sanitize_select( $input, $setting ){
          
            //input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only
            $input = sanitize_key($input);
  
            //get the list of possible select options 
            $choices = $setting->manager->get_control( $setting->id )->choices;
                              
            //return input if valid or return default option
            return ( array_key_exists( $input, $choices ) ? $input : $setting->default );                
              
        }
      
      
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_select', 
            array(
                'sanitize_callback' => 'theme_slug_sanitize_select'
            )
        );
          
        $wp_customize->add_control( 
            'theme_slug_customizer_select', 
            array(
                'label' => esc_html__( 'Your Setting with select', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'select',
                'choices' => array(
                    '' => esc_html__('Please select','theme_slug'),
                    'one' => esc_html__('Choice One','theme_slug'),
                    'two' => esc_html__('Choice Two','theme_slug'),
                    'three' => esc_html__('Choice Three','theme_slug')               
                )
            )
        );      
      
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理單行文本和多行文本域

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
          
                  
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_text', 
            array(
                'sanitize_callback' => 'wp_filter_nohtml_kses' //removes all HTML from content
            )
        );
          
        $wp_customize->add_control( 
            'theme_slug_customizer_text', 
            array(
                'label' => esc_html__( 'Your Setting with text input', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'text'
            )
        );      
      
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理郵箱地址

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
          
                  
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_email', 
            array(
                'sanitize_callback' => 'sanitize_email' //removes all invalid characters
            )
        );
          
        $wp_customize->add_control( 
            'theme_slug_customizer_email', 
            array(
                'label' => esc_html__( 'Your Setting with email input', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'email'
            )
        );      
      
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理網(wǎng)址

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
          
                  
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_url', 
            array(
                'sanitize_callback' => 'esc_url_raw' //cleans URL from all invalid characters
            )
        );
          
        $wp_customize->add_control( 
            'theme_slug_customizer_url', 
            array(
                'label' => esc_html__( 'Your Setting with URL input', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'url'
            )
        );      
      
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理數(shù)字

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
          
                  
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_number', 
            array(
                'sanitize_callback' => 'absint' //converts value to a non-negative integer
            )
        );
          
        $wp_customize->add_control( 
            'theme_slug_customizer_number', 
            array(
                'label' => esc_html__( 'Your Setting with number input', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'number'
            )
        );      
      
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理下拉頁(yè)面

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
          
                  
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_dropdown_pages', 
            array(
                'sanitize_callback' => 'absint' //input value is a page ID so it must be a positive integer
            )
        );
          
        $wp_customize->add_control( 
            'theme_slug_customizer_dropdown_pages', 
            array(
                'label' => esc_html__( 'Your Setting with dropdown_pages input', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'dropdown-pages'
            )
        );      
      
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理文件上傳

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
          
          
          
    //file input sanitization function
        function theme_slug_sanitize_file( $file, $setting ) {
          
            //allowed file types
            $mimes = array(
                'jpg|jpeg|jpe' => 'image/jpeg',
                'gif'          => 'image/gif',
                'png'          => 'image/png'
            );
              
            //check file type from file name
            $file_ext = wp_check_filetype( $file, $mimes );
              
            //if file has a valid mime type return it, otherwise return default
            return ( $file_ext['ext'] ? $file : $setting->default );
        }
  
          
                  
    //add select setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_file', 
            array(
                'sanitize_callback' => 'theme_slug_sanitize_file'
            )
        );
          
          
        $wp_customize->add_control( 
            new WP_Customize_Upload_Control( 
                $wp_customize, 
                'theme_slug_customizer_file', 
                array(
                    'label'      => __( 'Your Setting with file input', 'theme_slug' ),
                    'section'    => 'theme_slug_customizer_your_section'                   
                )
            ) 
        );              
      
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理 CSS

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
           
                   
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_css', 
            array(
                'sanitize_callback' => 'wp_strip_all_tags' //strip all HTML tags including script and style
            )
        );
           
        $wp_customize->add_control( 
            'theme_slug_customizer_css', 
            array(
                'label' => esc_html__( 'Your Setting with CSS input', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'textarea'
            )
        );      
       
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理 HTML 顏色代碼

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
           
                   
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_color', 
            array(
                'default' => '#000000',
                'sanitize_callback' => 'sanitize_hex_color' //validates 3 or 6 digit HTML hex color code
            )
        );
          
        $wp_customize->add_control( 
            new WP_Customize_Color_Control( 
            $wp_customize, 
            'theme_slug_customizer_color', 
                array(              
                    'label'      => __( 'Your Setting with color input', 'theme_slug' ),
                    'section'    => 'theme_slug_customizer_your_section'       
                )
            ) 
        );         
       
}
add_action( 'customize_register', 'theme_slug_customizer' );

清理 HTML 代碼

使用wp_kses_post()僅保留帖子內(nèi)容中允許的 HTML 標(biāo)簽的功能。

function theme_slug_customizer( $wp_customize ) {           
  
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
           
                   
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_html_code', 
            array(
                'sanitize_callback' => 'wp_kses_post' //keeps only HTML tags that are allowed in post content
            )
        );
          
        $wp_customize->add_control( 
            'theme_slug_customizer_html_code', 
            array(
                'label' => esc_html__( 'Your Setting with HTML code', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'textarea'
            )
        );         
       
}
add_action( 'customize_register', 'theme_slug_customizer' );

或者,您可以使用wp_kses()函數(shù)來(lái)定義允許的 HTML 標(biāo)記和屬性,如下所示:

$allowed_html = array(
    'a' => array(
        'href' => array(),
        'title' => array()
    ),
    'br' => array(),
    'em' => array(),
    'strong' => array(),
);
  
wp_kses($input, $allowed_html);

清理 JAVASCRIPT 代碼

我們正在使用base64_encode()函數(shù)將腳本正確保存在數(shù)據(jù)庫(kù)中,并使用base64_decode()函數(shù)來(lái)轉(zhuǎn)義自定義程序中 textarea 的腳本。還可以在前端使用base64_decode()函數(shù)來(lái)回顯腳本。

function theme_slug_customizer( $wp_customize ) {           
   
    //your section
        $wp_customize->add_section( 
            'theme_slug_customizer_your_section', 
            array(
                'title' => esc_html__( 'Your Section', 'theme_slug' ),
                'priority' => 150
            )
        );      
           
              
    //script input sanitization function
        function theme_slug_sanitize_js_code($input){
            return base64_encode($input);
        }
          
          
    //output escape function    
        function theme_slug_escape_js_output($input){
            return esc_textarea( base64_decode($input) );
        }
          
                   
    //add setting to your section
        $wp_customize->add_setting( 
            'theme_slug_customizer_js_code', 
            array(          
                'sanitize_callback' => 'theme_slug_sanitize_js_code', //encode for DB insert
                'sanitize_js_callback' => 'theme_slug_escape_js_output' //ecape script for the textarea
            )
        );
          
        $wp_customize->add_control( 
            'theme_slug_customizer_js_code', 
            array(
                'label' => esc_html__( 'Your Setting with JS code', 'theme_slug' ),
                'section' => 'theme_slug_customizer_your_section',
                'type' => 'textarea'
            )
        );         
       
}
add_action( 'customize_register', 'theme_slug_customizer' );

WordPress 清理函數(shù)列表

以下是WordPress清理函數(shù)列表。也許其中之一更適合您的項(xiàng)目。

還有一些 PHP 函數(shù)來(lái)填補(bǔ)一些空白。

注:本文出自 divpusher.com ,由 WordPress大學(xué) 翻譯整理。

聲明:本站所有文章,如無(wú)特殊說(shuō)明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺(tái)。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號(hào) WPDAXUE
WordPress開發(fā)商城相關(guān)

如何獲取 WooCommerce 訂單詳細(xì)信息?

2022-3-4 16:08:34

WordPress開發(fā)

Gutenberg 區(qū)塊樣式 API 簡(jiǎn)介

2022-4-10 10:06:28

0 條回復(fù) A文章作者 M管理員
    暫無(wú)討論,說(shuō)說(shuō)你的看法吧
?
個(gè)人中心
購(gòu)物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

潍坊市| 温泉县| 丹凤县| 雅江县| 崇文区| 苏尼特左旗| 安远县| 额尔古纳市| 虞城县| 固安县| 岢岚县| 兴文县| 甘孜县| 台州市| 沁水县| 盐亭县| 印江| 潞城市| 南充市| 茂名市| 玉环县| 临颍县| 宝兴县| 金溪县| 平南县| 海原县| 英超| 康保县| 龙胜| 偏关县| 蛟河市| 南乐县| 双城市| 喀喇沁旗| 乾安县| 砀山县| 汤原县| 正安县| 乐至县| 沂源县| 北票市|