• Resolved reszkov

    (@reszkov)


    Hi, i am using Virtue theme, i would like to add small script to <body> on every post. Please help me ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • What sort of script?

    If it’s JavaScript, then you should look at wp_enqueue_script().

    If it’s PHP, then you should add your function to your themes functions.php file (or a new plugin, etc) and call it using whatever actions or filter that you need to – oh, and be sure to use a child theme, just in case!

    Thread Starter reszkov

    (@reszkov)

    <script type=’text/javascript’> ?? so i guess its JavaScript

    Thread Starter reszkov

    (@reszkov)

    <script type='text/javascript'>document.write(unescape('%3Csc'+'rip'+'t type="text/javascript" id="cafenews.pl#c73707e940ccd580dc9afd40852ff083"%3E%3C/s'+'cri'+'pt%3E%3Cdiv id="cafenews.pl#cvb64mnc8vg4jd8gksd653jkadg7j6"%3E%3C/div%3E'));setTimeout('var d=new Date();document.getElementById("cafenews.pl#c73707e940ccd580dc9afd40852ff083").src="https://webservices.cafenews.pl/mpl/Static/Static.js?id=cafenews.pl#cvb64mnc8vg4jd8gksd653jkadg7j6&d="+d.getFullYear()+d.getMonth()+d.getDate();',250);</script>

    this is the script, i am new to this, and i don’t understand wp_enqueue_script(). Should i just paste the script in footer ?

    If it has to be within your pages body then I would try using the_content filter to dynamically add your JavaScript snippet before the actual post content.

    You could place this in your theme or even better, child theme functions file.

    add_filter( 'the_content', 'royal_inline_body_js', 20 );
    
    function royal_inline_body_js( $content ) {
    
    	// Check your on posts
        if ( is_single() )
    
            // Add your inline JS to the variable
            $royal_custom_js = '<script type="text/javascript">...code...</script>';
    
            // Append before existing post copy
            $content = $royal_custom_js . $content;
    
        // Returns the content.
        return $content;
    }
    Thread Starter reszkov

    (@reszkov)

    i did that, and site stoped working, i had to remove added line in fucntion.php

    Its likely breaking because of the syntax of your JavaScript. You will need to make sure your JavaScript snippet is formatted using double quotes and not single quotes.

    Can you supply your JavaScript snippet so I can see it…

    If it is a snippet like that, then you can put it in the footer. Using wp_enqueue_script() is meant for including external JavaScript files into the page, not for including script inside the page.

    Thread Starter reszkov

    (@reszkov)

    well, i am a total noob, all i know i recived

    <script type=’text/javascript’>document.write(unescape(‘%3Csc’+’rip’+’t type=”text/javascript” id=”cafenews.pl#c73707e940ccd580dc9afd40852ff083″%3E%3C/s’+’cri’+’pt%3E%3Cdiv id=”cafenews.pl#cvb64mnc8vg4jd8gksd653jkadg7j6″%3E%3C/div%3E’));setTimeout(‘var d=new Date();document.getElementById(“cafenews.pl#c73707e940ccd580dc9afd40852ff083”).src=”https://webservices.cafenews.pl/mpl/Static/Static.js?id=cafenews.pl#cvb64mnc8vg4jd8gksd653jkadg7j6&d=”+d.getFullYear()+d.getMonth()+d.getDate();’,250);</script>

    and that i need to add it at the end of the body, dose anyone have “How to do it for dummies” link ??

    I have updated the snippet for you replacing your JavaScript with double quotes in the syntax so it wont break the PHP string like it did last time you tried it. Give it another go and see if that works for you.

    add_filter( 'the_content', 'royal_inline_body_js', 20 );
    
    function royal_inline_body_js( $content ) {
    
    	// Check your on posts
        if ( is_single() )
    
            // Add your inline JS to the variable
            $royal_custom_js = '<script type="text/javascript">document.write(unescape("%3Csc"+"rip"+"t type="text/javascript" id="cafenews.pl#c73707e940ccd580dc9afd40852ff083"%3E%3C/s"+"cri"+"pt%3E%3Cdiv id="cafenews.pl#cvb64mnc8vg4jd8gksd653jkadg7j6"%3E%3C/div%3E"));setTimeout("var d=new Date();document.getElementById("cafenews.pl#c73707e940ccd580dc9afd40852ff083").src="https://webservices.cafenews.pl/mpl/Static/Static.js?id=cafenews.pl#cvb64mnc8vg4jd8gksd653jkadg7j6&d="+d.getFullYear()+d.getMonth()+d.getDate();",250);</script>';
    
            // Append before existing post copy
            $content = $content . $royal_custom_js;
    
        // Returns the content.
        return $content;
    }
    Thread Starter reszkov

    (@reszkov)

    yeah, i think that did it. Thank you all for helping me ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How-To add small script to body’ is closed to new replies.