Boson-Elementary
Boson-Elementary Recent Posts Threads Started
19 posts
  • Sold between 1 000 and 5 000 dollars
  • France
  • Has been a member for 3-4 years
  • Exclusive Author
  • Bought between 10 and 49 items
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
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Most Wanted Bounty Winner
  • Bought between 50 and 99 items
  • Poland
  • Referred between 10 and 49 users
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
19 posts
  • Sold between 1 000 and 5 000 dollars
  • France
  • Has been a member for 3-4 years
  • Exclusive Author
  • Bought between 10 and 49 items
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 ;)

814 posts
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
  • Serbia
  • Has been a member for 5-6 years
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
19 posts
  • Sold between 1 000 and 5 000 dollars
  • France
  • Has been a member for 3-4 years
  • Exclusive Author
  • Bought between 10 and 49 items
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..
3424 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 100 and 499 items
  • Referred more than 2000 users
  • United States
  • Has been a member for 4-5 years
+5 more
sevenspark Volunteer 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
19 posts
  • Sold between 1 000 and 5 000 dollars
  • France
  • Has been a member for 3-4 years
  • Exclusive Author
  • Bought between 10 and 49 items
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 ? :)

3424 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 100 and 499 items
  • Referred more than 2000 users
  • United States
  • Has been a member for 4-5 years
+5 more
sevenspark Volunteer 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.

814 posts
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
  • Serbia
  • Has been a member for 5-6 years
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.

3424 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 100 and 499 items
  • Referred more than 2000 users
  • United States
  • Has been a member for 4-5 years
+5 more
sevenspark Volunteer 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