-1

I do have a php page in my WP site, and I am trying to add a Jquery script directly in the php. basically something like this , the structure...**

<?php
/*
Template Name: Gallery Page
*/
?>
<?php get_header(); ?>


<?php
     //here i have my working php code
?>               
      //here some HTML           




  <script  language="JavaScript" type="text/javascript"    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> 



                           alert("hi");


 </script>


        <?php
                endwhile;

                    else :

                    // no rows found

                    endif;



        ?>


 <div class="spacing"></div>

<?php get_footer(); ?>

Anyone knows why my javascript code dont work if i set like I set in the script,. while it works if I only type

<script>

 alert("Hi");

</script>

Also: if I link jquery in the footer, and call a external script in my site with this code above, it works. The alert starts. Anyone knows why? it seems correct what i did and i am struggling to get over this.

thanks in advance

1
  • Do you get any errors in console btw? Commented Jul 5, 2015 at 14:30

3 Answers 3

1

Remove language from script tag:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

And also make sure that you close the script tag before you call the alert. Actually just remove the alert altogether.

Important note: Since you're using WordPress you don't even have to include jQuery on your page since this comes with WordPress from the start.

To include a main.js script file in WordPress;

  • Create a functions.php file
  • Paste the following snippet into this file:
  • <?php
        function include_my_scripts() {
          wp_enqueue_script('main', 'scripts/main.js', array('jquery'), '', true);
        }
        add_action('wp_enqueue_scripts', 'include_my_scripts');
    ?>
    

    This will name the script to main, and it's located at scripts/main.js, it depends on jQuery, it has no version, and it should be included at the bottom of the page.

    Check the WordPress codex for additional information.

    Also script including should be done from functions.php and not from your templates.

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

    1 Comment

    Hi thanks. I actually tried your code but it does not work. i did not know that..so I have to call the script in functions? Not sure how, i need to search. Anyway. the script works for external files.
    0

    Like @Chrillewoodz said, language attribute is deprecated. Also, you can't write code in a <script> tag that has a src attribute. You must choose between the two. You should move the alert() to a separate <script> tag. You can also self-close the tag that loads the external script: <script src=... />.

    1 Comment

    thanks all clear, i fixed it setting the way was recommended
    0

    Since your question is WordPress related, you must use enqueue_scripts function to load JS.

    In case of general JS, as mentioned in the previous answers, you may use <script type="text/javascript" src="...url..."></script> to load a script from a given URL or <script type="text/javascript">...code...</script> to execute JS.

    Reference: https://developer.wordpress.org/reference/functions/wp_enqueue_script/

    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.