skip to Main Content

I need to run this javascript code on a specific wordpress page.
The code inserted in the "Head", now works on all pages.
What code should I insert?

<script src="https://example.com/code.js" type="text/javascript"></script>

I am not a developer, but a newbie.

2

Answers


  1. Please use the code below. Add this code to your functions.php file:

        function cstm_script_spec_pg() {
            if ( is_page(123) || is_page('home') ) {
                wp_enqueue_script('custom-js', 'https://example.com/code.js', array(), null, true);
            }
        }
    add_action('wp_enqueue_scripts', 'cstm_script_spec_pg');

    Note:

    is_page(123): Replace 123 with the ID of the page where you want to run this script.

    is_page(‘home’): Replace home with the slug of the page where you want to run this script.

    Login or Signup to reply.
  2. Because you’re new to save a meltdown. Rather than editing functions.php (1 typo and your whole instance goes down). I suggest you either install one of the many code plugins that allow you to target a specific page. Or just add a code module to the content of the page and put your script in there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search