0

Is there any way to disable or remove JavaScript in the page/post content area?

So if there was any <script> tags it would remove them or ignore them when outputting the_content();

1 Answer 1

2

You could use the_content filter:

add_filter( 'the_content', 'remove_script_tag' );

function remove_script_tag( $content )
{
    return strip_tags( $content, '<script>' );
}

When you also want the content of the tag to be removed you could use this one:

add_filter( 'the_content', 'remove_script_tag_and_content' );

function remove_script_tag_and_content( $content )
{
    $striped_text =  preg_replace('@<script>.*?<\/script>@si', '', $content);
    return $striped_text;
}
3
  • that works, but does still output the JavaScript as plain text, is there anyway to completely remove it? Commented Nov 10, 2017 at 14:30
  • I have updated my answer Commented Nov 10, 2017 at 14:44
  • 1
    Perfect! just what i was looking for, thank you :) Commented Nov 10, 2017 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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