Using WordPress ‘kses_remove_filters()’ PHP function

The kses_remove_filters() WordPress PHP function removes all KSES input form content filters.

Usage

kses_remove_filters();

Parameters

  • None

More information

See WordPress Developer Resources: kses_remove_filters()

Important information:

  • KSES is a recursive acronym which stands for “KSES Strips Evil Scripts”.
  • KSES is an HTML/XHTML filter written in PHP.
  • It removes all unwanted HTML elements and attributes, and it also does several checks on attribute values.
  • KSES can be used to avoid Cross-Site Scripting (XSS).
  • This function does not remove the kses_init() function from ‘init’ hook (priority is default) and does not remove kses_init() function from ‘set_current_user’ hook (priority is also default).

Examples

Remove KSES filters before displaying content

function my_custom_content_filter( $content ) {
    // Remove KSES filters
    kses_remove_filters();

    // Process content with your custom filter
    $content = my_custom_filter_function( $content );

    // Return the filtered content
    return $content;
}
add_filter( 'the_content', 'my_custom_content_filter' );

Disable KSES filters for a specific user role

function disable_kses_for_contributors() {
    if ( current_user_can( 'contributor' ) ) {
        kses_remove_filters();
    }
}
add_action( 'init', 'disable_kses_for_contributors' );

Remove KSES filters when updating a post

function my_update_post_function( $post_id, $post_data ) {
    // Remove KSES filters
    kses_remove_filters();

    // Update post with new data
    wp_update_post( $post_data );
}

Temporarily remove KSES filters when saving post content

function my_save_post_function( $post_id, $post_data ) {
    // Temporarily remove KSES filters
    kses_remove_filters();

    // Save post with new data
    wp_update_post( $post_data );

    // Re-add KSES filters after saving
    kses_init_filters();
}

Remove KSES filters for a specific custom post type

function disable_kses_for_custom_post_type( $post ) {
    if ( 'my_custom_post_type' === $post->post_type ) {
        kses_remove_filters();
    }
}
add_action( 'the_post', 'disable_kses_for_custom_post_type' );