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();


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;
  • that works, but does still output the JavaScript as plain text, is there anyway to completely remove it? – Jim-miraidev Nov 10 '17 at 14:30
  • I have updated my answer – Drupalizeme Nov 10 '17 at 14:44
  • 1
    Perfect! just what i was looking for, thank you :) – Jim-miraidev Nov 10 '17 at 14:59

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.