Skip to content

Instantly share code, notes, and snippets.

@LeMiira
Created November 29, 2024 14:19
Show Gist options
  • Save LeMiira/0c9389b691c5f124522fdc365e012693 to your computer and use it in GitHub Desktop.
Save LeMiira/0c9389b691c5f124522fdc365e012693 to your computer and use it in GitHub Desktop.
Allow All safe tags for output content in WordPress
<?php // Define allowed HTML tags
$allowed_tags = array(
'a' => array(
'href' => array(),
'title' => array(),
'target' => array(),
'rel' => array(),
),
'abbr' => array(
'title' => array(),
),
'acronym' => array(
'title' => array(),
),
'b' => array(),
'blockquote' => array(
'cite' => array(),
),
'cite' => array(),
'code' => array(),
'del' => array(
'datetime' => array(),
),
'div' => array(
'class' => array(),
'id' => array(),
'style' => array(),
),
'em' => array(),
'i' => array(),
'img' => array(
'src' => array(),
'alt' => array(),
'title' => array(),
'width' => array(),
'height' => array(),
),
'li' => array(),
'ol' => array(),
'p' => array(
'class' => array(),
'style' => array(),
),
'q' => array(
'cite' => array(),
),
'span' => array(
'class' => array(),
'id' => array(),
'style' => array(),
),
'strong' => array(),
'sub' => array(),
'sup' => array(),
'table' => array(
'class' => array(),
'id' => array(),
'style' => array(),
),
'tbody' => array(),
'td' => array(
'colspan' => array(),
'rowspan' => array(),
'style' => array(),
),
'th' => array(
'colspan' => array(),
'rowspan' => array(),
'scope' => array(),
'style' => array(),
),
'thead' => array(),
'tr' => array(),
'ul' => array(),
// Add more as necessary depending on the use case and required flexibility
);
$content=get_content();
// Sanitize the content
$safe_content = wp_kses($content, $allowed_tags);
// Output the safe content
echo $safe_content;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment