2

Is there any way to remove the <script> tags including their contents?

wp_kses_post seems to only remove the tags, while their content remains visible on the page.

Thank you

2

KSES is designed to prevent execution of undesired and potentially dangerous tags, not preventing display of the innerHTML. Blocking the content would require

--1 Either a custom function that used some kind of string manipulation or xmlDOM manipulation to remove content; or

--2 A function that blocked posts that contain the tag with a message why the post failed and instructions for using HTML codes to render script tag when using it in tutorials rather than as an execution tag.

Number 1 may produce performance issues with its higher overhead. If optimizing performance is an issue or if the site involves heavy posting by multiple users, then #2 is likely a more feasible solution.

1

This is what I use on my site to strip images and paragraph tags. I'm assuming this is the_content?

<?php
ob_start();
the_content();
$old_content = ob_get_clean();
$new_content = strip_tags($old_content, '<insert HTML tags you want kept');
echo $new_content; ?>

The $new_content line will remove all HTML tags except those tags listed after $old_content

0

Give wp_strip_all_tags() a shot. I believe it removes the content of script tags also.

  • I want to only remove the <script> tags. I had a mistake in my post and the editor removed <script> because I didn't properly enclose it. – Alex Dumitru Sep 12 '14 at 19:22
0

I suggest You to use preg_replace() to filter the post content:

add_filter('the_content', 'my_strip_scripts');

function my_strip_scripts($content){

    return preg_replace('#<script(.*?)>(.*?)</script>#is', '', $content);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.