1

I used to write JS inline in my footer but since it's getting too much I want to write it in an external file and include the file in the footer then. I'm currently using

<?php include "https://example.com/myjs.php"; ?>

in the footer.

In that Php file which is mainly js with a few php expressions, I'm defining all the functions first and afterwards I'm calling some of them functions "onload" with

jQuery(document).ready(function() {
 // functions....  
 });

I've put everything inside

<script></script>

The thing is that I get an error showing the functions I'm calling arent defined. However, when I just paste the code in the footer template inside

<script> </script> 

tags it works fine.

I'm assuming that the error is with the tags and inlcude... What am I doing wrong?

EDIT: I am using WP functions which are based on logged in user id and page id. Do these might cause the errors? I'm using wordpress with jQuery.

14
  • check the page source in the browser (ctrl-u ?) does it look right? Commented Jan 25, 2018 at 4:17
  • Well it doesn’t include the js from the php file Commented Jan 25, 2018 at 4:18
  • I've never used php include with an URL before, does that work? (read the documentation, indeed it does, but it seems an odd thing to do) Commented Jan 25, 2018 at 4:19
  • 1
    @erik7 Because it requires allow_url_fopen to be on. And here is an interesting post you might wanna take a look at. Commented Jan 25, 2018 at 4:33
  • 1
    Also, let me brighten your day by adding something else that might help you now or in the future, but is in no way, shape or form an answer to your question. I don't know if you've ever worked with APIs, but let's suppose you want to serve your WordPress page with a PHP file that echoes out some JS. Cool, let your page request the information using cURL. What's super awesome about cURL is that you can even send POST, GET, PUT (and more) data! You get a response and use it as you want. You can even use JSON or XML. I'm writing this like a radio commercial script, so bear with me (laugh). Commented Jan 25, 2018 at 4:54

3 Answers 3

1

Since it's a remote URL, all php scripts will be executed before inclusion into your file. So how about echo file_get_contents("https://example.com/myjs.php");

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

1 Comment

This actually works and doesnt give me an error, still I am not getting the result I wanted. Anyway Thanks.
0

I could solve the problem: I defined the php vars in JS before the script within the footer.php. Credits to: Access PHP var from external javascript file

Then I just replaced the php vars wih the js vars and included it with html as script. Works like a charm now, thanks anyway for all the help!

3 Comments

LOL so funny that's very close to what I said might change your entire code. I'm glad you got it working, but remember that if one day you get an error stating that a selected DOM element is undefined, that's because if you're writing code directly into the script tags, those will execute as soon as they're loaded. So if that JS appears before (above) the element appears on the DOM, then that will trigger an error and nothing else will work. So I recommend you to load those JS files at the bottom of the page before closing </body>. I hope I'm being clear!
Yea I am using async and defer as well. To avoid getting those undefined errors I am using jQuery(document).ready(function() { // functions.... });
Alright, it's just that I hate jQuery and every given library. I'm more of a write-it-all-on-your-own kind of guy. But that works, I'm glad at least you understand the general ideas behind things. Good luck!
0

Debug your Javascript:

This error will also happen when the JavaScript in the include has an error in it.

For others not finding the selected solution helpful - I just resolved this "Function not defined" problem by debugging the JavaScript that I was including along with the PHP include.

The JavaScript had a small error in it. This was causing the the "Function not defined" error. Once I fixed the JavaScript error - the "Function not defined" error was resolved.

I guess a function can not be "defined" if it is erroneous.

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.