beliy333 beliy333 - 6 months ago 61
PHP Question

Is it okay to save my javascript in a .php format for a wordpress site?

Instead of putting my javascript into a .js file, I would like to use a .php file so that I can use some php code (for example getting my wordpress blog url, or "if this page" statements).

Are there any down sides in terms of performance if I do this?


You can have a .php page and have your js functions within it with the proper <script></script> tags, as you said, and there shouldn't be anything wrong with it.

Though, if you have lots of js functions that require its own page, then you can pass the values that come from the server-side to the function in the client side. For example.

in .php page

var blogUrl = "<?php echo get_bloginfo('url'); ?>";

myFunction (blogUrl);


and in .js page

function myFunction (blogUrl)
   //do something 

This way, you can have a proper pure js file, while not making your php files too messy with JavaScript either.