0

I am creating a jquery script to edit some parts of the wordpress page.

This is the code that I have tried so far in functions.php

function theme_name_scripts() {
    if (is_page('21')) {
        wp_enqueue_script('myscript', get_stylesheet_directory_uri().
            '/js/myscript.js', array(), '1.0.0', true);
    }

}
add_action('wp_enqueue_scripts', 'theme_name_scripts');

And this is the simple jQuery script that I want to load:

<script type="text/javascript">
    var j = jQuery.noConflict();
    j(function() {
        alert('tables test alert');
    });
</script>

The script above is not loading. Please help.

Any help is appreciated.

2
  • have you check that your is_page condition is true? and you are using get_stylesheet_directory_uri()?if it is really not loaded please check develooper console for an error. Commented Jan 19, 2016 at 6:47
  • if your code is in .js file why you use script tags there? <script type="text/javascript"> Commented Jan 19, 2016 at 6:50

2 Answers 2

3

Your include function looks right, you must remove script tags <script type="text/javascript"> from your .js file.

Sign up to request clarification or add additional context in comments.

Comments

-1

If you are looking to make changes for a script in a specific page, you might as well try the Custom Script Integration plugin instead of meddling with functions.php. This plugin will let you add your code under head or body for individual pages so it will be easier to manage scripts.

Link to plugin: http://www.wpwox.com/wp-plugin/custom-script-integration/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.