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.

digitalsuite.net
  • 339
  • 6
  • 20
  • check the page source in the browser (ctrl-u ?) does it look right? – Jaromanda X Jan 25 '18 at 04:17
  • Well it doesn’t include the js from the php file – digitalsuite.net Jan 25 '18 at 04: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) – Jaromanda X Jan 25 '18 at 04:19
  • @JaromandaX just to answer that question, it depends. If your `php.ini` file has `allow_url_include` enabled, then yes! – Zeke Jan 25 '18 at 04:22
  • I'm using an absolute path because I wanna keep the file in my wp root folders white the footer is called deeply inside my theme structure.. – digitalsuite.net Jan 25 '18 at 04:22
  • `I'm using an absolute path` - you're actually using HTTP(S), not an absolute path at all - see Zeke's comment regarding php.ini – Jaromanda X Jan 25 '18 at 04:23
  • @Zeke Never!!! Do not suggest `allow_url_include` – Aniket Sahrawat Jan 25 '18 at 04:23
  • @AniketSahrawat - but that's the only way to include via HTTP :p – Jaromanda X Jan 25 '18 at 04:25
  • @AniketSahrawat I did not suggest to enable it, I'm just saying that's the way... when did I suggest it? – Zeke Jan 25 '18 at 04:27
  • @Zeke My Host actually has this setting enabled. Why wouldn't you recommend it? – digitalsuite.net Jan 25 '18 at 04:29
  • 1
    @erik7 Because it requires allow_url_fopen to be on. And here is an [interesting post](https://stackoverflow.com/a/24049705/6099347) you might wanna take a look at. – Aniket Sahrawat Jan 25 '18 at 04:33
  • @erik7 sorry for not addressing your question. Quite honestly, even though you say you're using some PHP inside the JS (which explains why you'd be doing this) I'm sure there's a better way to do whatever you're doing without mixing PHP and JS. That's why my recommendations might change way too much your current code and are therefore invalid. So I'll let my fellow developers help you. To answer your comment, it's due to security concerns. Even if you aren't doing anything harmful, a hosting company cannot afford to enable this since users might do stuff they shouldn't do with it. – Zeke Jan 25 '18 at 04:38
  • 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). – Zeke Jan 25 '18 at 04:54
  • I will have a look at cURL. Sounds good because I am mostly using ajax requests in that js script. – digitalsuite.net Jan 25 '18 at 04:57

3 Answers3

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");

Phil
  • 1,444
  • 2
  • 10
  • 21
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!

digitalsuite.net
  • 339
  • 6
  • 20
  • 1
    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 `

    `. I hope I'm being clear!

    – Zeke Jan 25 '18 at 05:05
  • Yea I am using async and defer as well. To avoid getting those undefined errors I am using jQuery(document).ready(function() { // functions.... }); – digitalsuite.net Jan 25 '18 at 05:11
  • 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! – Zeke Jan 25 '18 at 05:18
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.

TV-C-1-5
  • 680
  • 2
  • 13
  • 19