Using WordPress ‘force_filtered_html_on_import’ PHP filter

The force_filtered_html_on_import WordPress PHP filter allows you to control whether or not to filter imported data through kses on import in a multisite environment.

Usage

add_filter('force_filtered_html_on_import', 'my_force_filtered_html_on_import', 10, 1);

function my_force_filtered_html_on_import($force) {
    // your custom code here
    return $force;
}

Parameters

  • $force (bool): Determines whether to force data to be filtered through kses. Default is false.

More information

See WordPress Developer Resources: force_filtered_html_on_import

Examples

Force filtering of imported data

In this example, the filter forces all imported data to be filtered through kses.

add_filter('force_filtered_html_on_import', 'force_filter_on_import');

function force_filter_on_import($force) {
    return true;
}

Do not force filtering of imported data

In this example, the filter does not force imported data to be filtered through kses.

add_filter('force_filtered_html_on_import', 'no_force_filter_on_import');

function no_force_filter_on_import($force) {
    return false;
}

Force filtering only for specific user roles

In this example, the filter forces imported data to be filtered through kses only for authors and editors.

add_filter('force_filtered_html_on_import', 'force_filter_for_specific_roles');

function force_filter_for_specific_roles($force) {
    $user = wp_get_current_user();
    if (in_array('author', $user->roles) || in_array('editor', $user->roles)) {
        return true;
    }
    return $force;
}

Force filtering based on user capability

In this example, the filter forces imported data to be filtered through kses only for users who do not have the ‘manage_options’ capability.

add_filter('force_filtered_html_on_import', 'force_filter_based_on_capability');

function force_filter_based_on_capability($force) {
    if (!current_user_can('manage_options')) {
        return true;
    }
    return $force;
}

Force filtering only for specific file types

In this example, the filter forces imported data to be filtered through kses only for specific file types (e.g., CSV files).

add_filter('force_filtered_html_on_import', 'force_filter_for_csv_files', 10, 1);

function force_filter_for_csv_files($force) {
    // Assume $imported_file is the imported file path
    $file_extension = pathinfo($imported_file, PATHINFO_EXTENSION);
    if ('csv' === $file_extension) {
        return true;
    }
    return $force;
}