Programmatically create a text format
<php>
$format_exists = (bool) db_query_range('SELECT 1 FROM {filter_format} WHERE name = :name', 0, 1, array(':name' => 'My Filter'))->fetchField();
if (!$format_exists) {
// Define allowed tags.
$allowed_tags = [
'grouping' => '<p> <hr> <pre> <blockquote> <ol> <ul> <li> <dl> <dt> <dd> <figure> <figcaption> <div> <main>',
'others' => '<source>',
];
$format = [
'format' => 'my_filter',
'name' => 'My Filter',
// Use a higher weight here to ensure that this format will not
// be the default format for anyone.
'weight' => 15,
'filters' => [
// Allowed HTML tags.
'filter_html' => [
'status' => 1,
'weight' => 4,
'settings' => [
'allowed_html' => implode(' ', $allowed_tags),
'filter_html_help' => 0,
'filter_html_nofollow' => 0,
],
],
],
];
// Prepare and save the text format.
$format = (object) $format;
filter_format_save($format);
drupal_set_message(t('A <a href="@filter">My Filter</a> text format has been created.', array('@filter' => url('admin/config/content/formats/' . $format->format))));
}