47 posts
  • Has been part of the Envato Community for over 1 year
  • Has sold $10,000+ on Envato Market
  • Had an item featured in an Envato Bundle
  • Located in Europe
+3 more
mustachethemes says

Hello, I am starting with WordPress and I am watching some video tutorials. In this video tutorials the bootstrap styles and bootstrap jquery links are in the index.php file.
But I see in some Themes here that the styles and js files links are in the function.php file.
My question is: What is the benefit of having them in the function.php file instead of the index.php? Is this some “WordPress Theme Submission Requirements”?
I need help, thanks! :)

41 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
fitwp says

Scripts and styles must be enqueued using “wp_enqueue_script” and “wp_enqueue_style” functions. You can choose to put these functions in functions.php or another PHP file, but not index.php. Output HTML for JS & CSS file directly into template file is a bad practice, unless you have to, like outputting conditional Javascript file (html5shiv) for IE < 9, where those functions can’t output conditional tag.

47 posts
  • Has been part of the Envato Community for over 1 year
  • Has sold $10,000+ on Envato Market
  • Had an item featured in an Envato Bundle
  • Located in Europe
+3 more
mustachethemes says

Thank you so much for your answer. Another simple question:
In one page themes: what is the benefit of having each section of the page in a separate php file. For example: about.php, portfolio.php, contact.php and call them from the index.php?
Is this better than having all the sections in the index.php?

41 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
fitwp says

Code organizing!

What happens if you keep your index.php file 1000 lines of code? Is it: - easy to read? - easy to debug when something happends? - easy to maintenance when you need to find/update/change/optimize something?

No, absolutely no!

Don’t keep everything in 1 file. You always should separate part of your code into files. When something goes wrong, you know where you have to look for. A good practice is keep 1 file about 200 lines of code, but in some cases, it can be 300-400. > 600 is not recommended and in that case, you need to think about optimization, or more precise, refactoring code.

47 posts
  • Has been part of the Envato Community for over 1 year
  • Has sold $10,000+ on Envato Market
  • Had an item featured in an Envato Bundle
  • Located in Europe
+3 more
mustachethemes says

Maybe I can use this code to add the html5shiv code into the funcion.php file?

 
// add ie conditional html5 shim to header
function add_ie_html5_shim () {
    global $is_IE;
    if ($is_IE)
        wp_register_script ('html5shim', "http://html5shim.googlecode.com/svn/trunk/html5.js");
        wp_enqueue_script ('html5shim');
}
add_action('wp_head', 'add_ie_html5_shim');

I was reading: http://css-tricks.com/snippets/wordpress/html5-shim-in-functions-php/ to find a solution for that.
47 posts
  • Has been part of the Envato Community for over 1 year
  • Has sold $10,000+ on Envato Market
  • Had an item featured in an Envato Bundle
  • Located in Europe
+3 more
mustachethemes says
Wow, thanks for the recommendation, I must see how many code lines I have now and find they way to split them into php files. Thanks

Code organizing!

What happens if you keep your index.php file 1000 lines of code? Is it: - easy to read? - easy to debug when something happends? - easy to maintenance when you need to find/update/change/optimize something?

No, absolutely no!

Don’t keep everything in 1 file. You always should separate part of your code into files. When something goes wrong, you know where you have to look for. A good practice is keep 1 file about 200 lines of code, but in some cases, it can be 300-400. > 600 is not recommended and in that case, you need to think about optimization, or more precise, refactoring code.
41 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
fitwp says

Maybe I can use this code to add the html5shiv code into the funcion.php file?
 
// add ie conditional html5 shim to header
function add_ie_html5_shim () {
    global $is_IE;
    if ($is_IE)
        wp_register_script ('html5shim', "http://html5shim.googlecode.com/svn/trunk/html5.js");
        wp_enqueue_script ('html5shim');
}
add_action('wp_head', 'add_ie_html5_shim');
I was reading: http://css-tricks.com/snippets/wordpress/html5-shim-in-functions-php/ to find a solution for that.

This is a good snippet. But keep in mind that websites mostly use cache plugin (like W3 Total Cache, WP Super Cache). When you ouput this for 1 user (IE, for example), it would be used for 1000+ users, which is not cool. That’s why we should use conditional tag for that.

Anyway, good snippet :)

by
by
by
by
by
by