• let’s say I have <script type=”text/javascript”>bla bla bla</script> and need to insert it directly on the article or wordpress page, I tried doing that about now, but it was converted with CDATA, I believe it doesn’t work like that.

    Is it correct?

Viewing 9 replies - 1 through 9 (of 9 total)
  • That is correct. To properly include javascript onto a page or post, you’ll ideally want to use

    wp_enqueue_script();

    You’ll want to create a .js file on your server, and use the function mentioned above to load the script onto the appropriate page using a conditional or something similar.

    As a good starting point, this will load the js file across all pages on the front end:
    (you’ll want to add this to functions.php of your active theme)

    function enqueue_our_script_right_meow() {
      wp_register_script( 'our-custom-js-handle' , 'path/to/js/file/file.js' , array( 'jquery' ) , 'all' );
      wp_enqueue_script( 'our-custom-js-handle' );
    }
    add_actiom( 'wp_enqueue_scripts' , 'enqueue_our_script_right_meow' );

    you’ll notice we’ve used two seperate functions to load the script. Wp_register_script tells WordPress to get the file ready for use, wp_enqueue_Script actually loads the file onto the page.

    Hope that helps clear things up a bit!

    Evan

    Moderator t-p

    (@t-p)

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Thread Starter spikespiegel

    (@spikespiegel)

    @evan Herman
    Thx for the info, I’ll take a look into that.
    It won’t use functions.php file, right? I don’t want to hack the code, as my site suffers constant updates in the files.

    @tara
    Thx, gotta a lot of info in there, I’ll make sure to ready it carefully.

    @andrew
    I’m with a lot of plugins already, almost exceeding php memory, but I’ll make that my last option if nothing else helps, anyway, thx for taking your time for finding that plugin, you’re great 😀

    Yes, you want to add that into your functions.php

    Ideally, you should be using a child theme so that any updates do not over write your custom code.

    Thread Starter spikespiegel

    (@spikespiegel)

    Unfortunately I can’t. I’ll have find another way then, 🙁

    Why , are you not using a child theme currently?

    Thread Starter spikespiegel

    (@spikespiegel)

    I not even know what’s that, lol.
    I’m using the template MAGAZINE, it updates sometimes.

    Yea if you’re not sure what it is , chances are you are not using one.

    My recommendation would be to split off from using the parent theme itself, and create a child theme that will allow you to alter the theme in any way you choose without losing any of the modifications in future updates:

    https://wordpress.org/plugins/one-click-child-theme/ <- havent used it but worth a shot

    http://codex.wordpress.org/Child_Themes

    Evan

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How can I insert javascript in a page or article?’ is closed to new replies.