Boson-Elementary
Boson-Elementary Recent Posts Threads Started
20 posts
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in France
  • Has been part of the Envato Community for over 3 years
+1 more
Boson-Elementary says

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.

741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says

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).

Boson-Elementary
Boson-Elementary Recent Posts Threads Started
20 posts
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in France
  • Has been part of the Envato Community for over 3 years
+1 more
Boson-Elementary says

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).

Actually i don’t care to get some variables coming from Wordpress, but thanks this links could help me for some others stuff ;)

815 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Contributed a free file of the month
+3 more
rvision_ says

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 :)

Boson-Elementary
Boson-Elementary Recent Posts Threads Started
20 posts
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in France
  • Has been part of the Envato Community for over 3 years
+1 more
Boson-Elementary says
Thank you for your answer :) 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..
3447 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark Moderator says

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.

Boson-Elementary
Boson-Elementary Recent Posts Threads Started
20 posts
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in France
  • Has been part of the Envato Community for over 3 years
+1 more
Boson-Elementary says

@sevenspark Thank you for your advice ! I’ve never try this solution to write a static file with PHP , have you any exemple ? :)

3447 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark Moderator says

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.

815 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Contributed a free file of the month
+3 more
rvision_ says

Thank you for your answer :) 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..

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.


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.

3447 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark Moderator says

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 :)

by
by
by
by
by
by