DESINFECTAR LA ENTRADA DE ARCHIVOS
function softpang_sanitize_file( $file, $setting ) {
$mimes = array(
'jpg|jpeg|jpe' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png'
);
$file_ext = wp_check_filetype( $file, $mimes );
return ( $file_ext['ext'] ? $file : $setting->default );
}
$wp_customize->add_setting(
'sp_urlimg_team0',
array(
'sanitize_callback' => 'softpang_sanitize_file'
)
);
$wp_customize->add_control(
new WP_Customize_Upload_Control(
$wp_customize,
'sp_urlimg_team0,
array(
'label' => __( 'Seleccione Imagen', 'softpang' ),
'section' => 'sp_team0_section'
)
));
DESINFECTAR LA CASILLA DE VERIFICACIÓN
function sp_sanitize_checkbox( $checked ) {
return ( ( isset( $checked ) && true == $checked ) ? true : false);
}
$wp_customize->add_setting('sp_show_clientspanel',array(
'default' => false,
'sanitize_callback' => 'sp_sanitize_checkbox',
'capability' => 'edit_theme_options',
));
$wp_customize->add_control( 'sp_show_clientspanel', array(
'settings' => 'sp_show_clientspanel',
'section' => 'sp_clients_section',
'label' => __('Mostrar Seccion','softpang'),
'type' => 'checkbox'
));
DESINFECTAR EL CÓDIGO DE COLOR 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' );
DESINFECTAR EL CÓDIGO 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_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' );
LISTA DE FUNCIONES DE DESINFECCIÓN DE WORDPRESS
- absint () : convierte el valor en un entero positivo, útil para números, ID, etc.
- esc_url_raw () – para insertar URL en la base de datos de forma segura
- sanitize_email () : elimina todos los caracteres que no están permitidos en una dirección de correo electrónico
- sanitize_file_name () : elimina los caracteres especiales que son ilegales en los nombres de archivo en ciertos sistemas operativos
- sanitize_hex_color () – devuelve color hexadecimal de 3 o 6 dígitos con #, o nada
- sanitize_hex_color_no_hash () – lo mismo que el anterior pero sin un #
- sanitize_html_class () : desinfecta un nombre de clase HTML para garantizar que solo contenga caracteres válidos
- sanitize_key () : se permiten caracteres alfanuméricos en minúscula, guiones y guiones bajos
- sanitize_mime_type () – útil para guardar el tipo de mime en la base de datos, por ejemplo, el tipo de archivo cargado
- sanitize_option () : desinfecta valores como update_option () y add_option () para varios tipos de opciones. Aquí está la lista de opciones disponibles: https://codex.wordpress.org/Function_Reference/sanitize_option#Notes
- sanitize_sql_orderby () : garantiza que una cadena sea un pedido SQL válido por cláusula
- sanitize_text_field () : elimina todo el marcado HTML, así como los espacios en blanco adicionales, no deja nada más que texto sin formato
- sanitize_title () : valor devuelto con la intención de que sea adecuado para su uso en una URL
- sanitize_title_for_query () – utilizado para consultar la base de datos para un valor de URL
- sanitize_title_with_dashes () – igual que el anterior pero no reemplaza los caracteres especiales acentuados
- sanitize_user () – desinfecta el nombre de usuario eliminando los caracteres inseguros
- wp_filter_post_kses (), wp_kses_post () : solo mantiene etiquetas HTML que también están permitidas en el contenido de la publicación
- wp_kses () : solo permite etiquetas HTML y atributos que especifique
- wp_kses_data () : desinfectar el contenido con las reglas de HTML Kses permitidas
- wp_rel_nofollow () : agrega una cadena rel nofollow a todos los elementos HTML A en el contenido
También te doy algunas funciones PHP para llenar algunos vacíos.
- filter_var ($ variable, $ filter) : filtra una variable con un filtro específico
- strlen () : obtiene la longitud de la cadena, útil para códigos postales, números de teléfono
https://divpusher.com/blog/wordpress-customizer-sanitization-examples/