The kses_init() WordPress PHP function sets up most of the KSES filters for input form content. It first removes all of the KSES filters in case the current user does not need to have KSES filter the content. If the user does not have unfiltered_html
capability, then KSES filters are added.
Usage
kses_init();
Parameters
- None
More information
See WordPress Developer Resources: kses_init()
Examples
Basic usage of kses_init()
In this example, we use kses_init() before saving the content to the database.
// Add KSES filters before saving content kses_init(); // Save content to the database $content = '<script>alert("Hello!");</script>'; $filtered_content = wp_kses_post($content);
Disabling KSES filters for admin users
In this example, we disable KSES filters for users with the unfiltered_html
capability.
// Check if the user has unfiltered_html capability if (current_user_can('unfiltered_html')) { // Remove KSES filters kses_remove_filters(); } else { // Add KSES filters kses_init(); }
Using kses_init() with a custom text area
In this example, we use kses_init() to filter content from a custom text area in a form.
// Add KSES filters before processing the content kses_init(); // Get content from a custom text area $content = $_POST['custom_textarea']; // Apply KSES filters to the content $filtered_content = wp_kses_post($content);
Using kses_init() with a custom filter
In this example, we use kses_init() to set up KSES filters and then add a custom filter for content.
// Add KSES filters kses_init(); // Add a custom filter function custom_kses_filter($content) { // Filter content here return $content; } add_filter('content_save_pre', 'custom_kses_filter'); // Save content to the database $content = '<script>alert("Hello!");</script>'; $filtered_content = wp_kses_post($content);
Using kses_init() in a plugin
In this example, we create a simple plugin that uses kses_init() to filter content.
/* Plugin Name: KSES Example Plugin Description: A plugin that uses kses_init() to filter content. */ function kses_example_plugin_init() { // Add KSES filters kses_init(); } add_action('init', 'kses_example_plugin_init');