Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Wordpress: problem to insert a script in a post content

I intend to insert the simple script below in a Wordpress post content through Classic Editor; but whenever I switch to visual mode or publish the post, Wordpress automatically removes the whole code (both HTML tags and script):

<p id="Test"></p>
<script>
var x = 10;
document.getElementById("Test").innerHTML= x;
</script>

To solve the issue, I followed these steps respectively:

1) The following snippet code was added to functions.php- so, now, Wordpress doesn't remove formatted HTML anymore:

function uncoverwp_tiny_mce_fix( $init )
{
    $init['extended_valid_elements'] = 'div[*]';

    return $init;
}
add_filter( 'tiny_mce_before_init', 'uncoverwp_tiny_mce_fix' );

2) By adding the following code, too, the above script is supposed to run on my post (here, for example, post id=123)

function wpb_hook_javascript() {
  if (is_page ('123')) { 
    ?>
        <script type="text/javascript">
          // My javscript code
        </script>
    <?php
  }
}
add_action('wp_head', 'wpb_hook_javascript');

Everything looks fine and logical, but this method doesn't work. I also deferred the script, but the problem remained. What am I missing here?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To solve the issue, I installed and activated Advanced Editor Tools plugin; then, from settings, checked the box "Keep paragraph tags in the Classic block and the Classic Editor". so now Wordpress doesn't strip out HTML tags and lets me insert scripts directly in the text editor.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!