- Sold between 1 000 and 5 000 dollars
- France
- Has been a member for 2-3 years
- Exclusive Author
- Bought between 1 and 9 items
Hello,
I’m a young developper and I’m actually coding my first theme for Themeforest but I’ve some questions to ask.
In my theme, there is an admin panel, which allow to disable or not some jQuery plugins, but i want neither to have a lot of HTTP request nor to have a big Javascript file or CSS file then i had an idea to solve my problem.
I’m requesting a script called javascript.php in my footer with some parameters like Gallery, ScrollTo, etc.. and in this script i get all my parameters to disable some parts of my code , here is an exemple :
footer.php
<script src="<?php echo get_template_directory_uri(); ?>/js/javascript.php?Gallery=true&Scrollto=false"></script>
Javascript.php
<?php
header("Content-type: text/javascript; charset: UTF-8");
$parameters = array ($_GET['Gallery'], $_GET['Scrollto']);
if ($parameters[0] == true ) { ?>
function(window, document, $) {
blablablabla...
}
<?php
}
if ($parameters[1] == true ) {
?>
function(window, document, $) {
blablablabla...
}
<?php } ?>
Is it a good way to merge all my scripts , and don’t have a huge file when it’s loaded in the page ?
Thanks, and sorry for my english.
I get what you want to do here and for passing any kind of variables from WordPress to your scripts you should use a wp_localize_script function. This way you will keep you files clean and avoid incompatibility (some servers will only parse PHP when the file has a PHP extension).
- Sold between 1 000 and 5 000 dollars
- France
- Has been a member for 2-3 years
- Exclusive Author
- Bought between 1 and 9 items
pogoking said
I get what you want to do here and for passing any kind of variables from WordPress to your scripts you should use awp_localize_scriptfunction. This way you will keep you files clean and avoid incompatibility (some servers will only parse PHP when the file has a PHP extension).
Actually i don’t care to get some variables coming from Wordpress, but thanks this links could help me for some others stuff 
You’re overcomplicating it with parameters / if / else etc. It’s an overkill, will be harder to maintain and probably can open a security flaw.
Here’s what I am doing:
- merge all javascripts (plugins, etc) with php with simple includes - load it on all admin pages, cache it via http headers - in main javascript code call plugins and functions only when required:
if($('#my-options-page')) {
$('#element').plugin();
}
I hope I was clear 
- Sold between 1 000 and 5 000 dollars
- France
- Has been a member for 2-3 years
- Exclusive Author
- Bought between 1 and 9 items
Hum.. Are you saying that you do something like that :
<?php
header("Content-type: text/javascript; charset: UTF-8");
if($('#my-options-page')) {
include(TEMPLATEPATH . '/js/plugins1.js');
}
include(TEMPLATEPATH . '/js/plugins2.js');
include(TEMPLATEPATH . '/js/plugins3.js');
?>
I’m not sure to understand , how you do that..- Sold between 250 000 and 1 000 000 dollars
- Community Moderator
- Author was Featured
- Item was Featured
- Bought between 50 and 99 items
- Referred between 1000 and 1999 users
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
First, you should not be including script tags directly into the footer.php template. You need to use wp_enqueue_script
Second, using PHP to generate a javascript or CSS file on every page load is a poor use of server resources. Instead, use PHP to write a static CSS or JS file whenever the settings change, which you can then serve on each page load – much more efficient.
- Sold between 1 000 and 5 000 dollars
- France
- Has been a member for 2-3 years
- Exclusive Author
- Bought between 1 and 9 items
@sevenspark Thank you for your advice ! I’ve never try this solution to write a static file with PHP , have you any exemple ? 
- Sold between 250 000 and 1 000 000 dollars
- Community Moderator
- Author was Featured
- Item was Featured
- Bought between 50 and 99 items
- Referred between 1000 and 1999 users
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
Check out file_put_contents – makes things easy
Just be aware of file write permission issues and either alert the customer or have a fallback.
Mwea said
Thank you for your answerHum.. Are you saying that you do something like that :
<?php header("Content-type: text/javascript; charset: UTF-8"); if($('#my-options-page')) { include(TEMPLATEPATH . '/js/plugins1.js'); } include(TEMPLATEPATH . '/js/plugins2.js'); include(TEMPLATEPATH . '/js/plugins3.js'); ?>I’m not sure to understand , how you do that..
No. You include all plugins, but in javascript code call plugins only when needed. You’re mixing jquery and php in your example. There is no $ on the server side.
sevenspark said
Second, using PHP to generate a javascript or CSS file on every page load is a poor use of server resources. Instead, use PHP to write a static CSS or JS file whenever the settings change, which you can then serve on each page load – much more efficient.
Actually if this is just for the admin part it will be fine. Writing to a static file can cause trouble for some users. I didn’t have that experience, but remember on the forum someone mentioned this issue.
- Sold between 250 000 and 1 000 000 dollars
- Community Moderator
- Author was Featured
- Item was Featured
- Bought between 50 and 99 items
- Referred between 1000 and 1999 users
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
rvision_ said
Actually if this is just for the admin part it will be fine. Writing to a static file can cause trouble for some users. I didn’t have that experience, but remember on the forum someone mentioned this issue.
Good point, I missed that originally. If the stylesheet and script are used solely in the admin area, then it’s not as a big a deal if they are being built by PHP. I’d definitely generate static files for the front-end however, even if it takes a bit of extra config up front to get the permissions right 
