I'm trying to get some Javascript to run on one of my pages. But nothing I do seems to work. I ran the code in a sandbox and it is functional.

I tried: - Inserting and HTML builder element and putting the code in tags - uploading the .js file in the theme and linking to it in my html code - Multiple script plugins

How does one simply execute Javascript on a Wordpress page?

  • Please show what you tried, optimally with code. It makes giving answers to your specific problem easier. Jul 10, 2019 at 18:05

2 Answers 2

function enqueue_javascript_for_certain_page() {

    $page_id = 100; // change this to fit your needs

    if ( is_page( $page_id ) ) { // check the page you are on

            'my_script_name', // script handle
            get_template_directory_uri() . '/js/my-js-file.js', // script URI
            array( // your script is dependent on following:
            NULL, // script version (NULL means no version to display in query string)
            true // load script right before </body> tag



add_action( 'wp_enqueue_scripts', 'enqueue_javascript_for_certain_page' );

add js file using function.php

function add_theme_scripts() {
    wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );


//if you want to add script in plugin use this code in plugin:
function yourScript() {
    wp_register_script( 'script', plugins_url('js/jqueryfile.js',__FILE__),array('jquery'),'1.1', true );
add_action( 'admin_enqueue_scripts', yourScript);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.