125 posts I know Chuck
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
  • Sells items exclusively on Envato Market
+5 more
kotofey
says

Hi all,

I don’t like to write about mine problems in forum, but not at this time…

Today I’ve got another soft reject. The reason is:

You’ve included some of your JavaScript in header.php which is a bad practice and may lead to some errors.
.

You’ll say ok, but where is a problem? The problem is that I use a wp_enqueue_script () for my scripts. This cusom script (custom.js) have jquery functions for tipsy / nivoslider / prettyPhoto / and so on…

Maybe there is a new wordpress standarts? Or it’s a reviewer’s joke? Or something else? :|

118 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 50+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
ZenThemes
says

Yes, it is a standard.

Just make your custom javascript load on wp_footer .

125 posts I know Chuck
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
  • Sells items exclusively on Envato Market
+5 more
kotofey
says
KiwiStudio said
Yes, it is a standard. Just make your custom javascript load on wp_footer .
I think you don’t understand me.
I use a wp_enqueue_script () for my scripts.
223 posts
  • Has referred 1+ members
  • Has collected 50+ items on Envato Market
  • Has been part of the Envato Community for over 6 years
  • Sells items exclusively on Envato Market
agusmu
says
kotofey said
I think you don’t understand me.

wp_enqueue_script() should be called using a WP action hook. Don’t add it directly to your header.php

for example, you can add this code to your functions.php :

add_action( 'get_header', 'my_custom_script');
function my_custom_script() {
    wp_enqueue_script('custom', get_stylesheet_directory_uri().'/js/custom.js', array('jquery'), '0.1', true);
}
125 posts I know Chuck
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
  • Sells items exclusively on Envato Market
+5 more
kotofey
says
agusmu said
kotofey said
I think you don’t understand me.

wp_enqueue_script() should be called using a WP action hook. Don’t add it directly to your header.php

for example, you can add this code to your functions.php :

add_action( 'get_header', 'my_custom_script');
function my_custom_script() {
    wp_enqueue_script('custom', get_stylesheet_directory_uri().'/js/custom.js', array('jquery'), '0.1', true);
}

Ok. Many thanks. Let’s try.

4458 posts ThemeForest Reviewer
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Member of the Envato Team
+14 more
Ivor
Envato team
says

It’s a bad practice, custom scripts should be included with wp_enqueue_scripts via functions.php – Including JavaScript in header.php directly can lead to some problems.

Why functions.php? -> WordPress standards.

Usage

wp_enqueue_script( 
     $handle
    ,$src
    ,$deps
    ,$ver
    ,$in_footer 
);

Example

function prefix_add_javascript() {
wp_enqueue_script( 'script', get_bloginfo('template_directory').'/includes/js/script.js', array( 'jquery' ), '0.9.2', 'true' );
}    

add_action('wp_enqueue_scripts', 'prefix_add_javascript');

Also you should include your scripts into specific theme pages with conditionals so that the JS doesn’t impact other areas of the theme. For example if you’re using Nivo Slider only in the homepage:

if (is_page_template('custom-home-page.php')) {
    wp_enqueue_script('nivoSlider', get_bloginfo('template_directory').'/includes/js/nivo.js', array('jquery'), '1.0.2', 'true');

    add_action('wp_footer', 'nivo_script');

    function nivo_script() {

        $output = " 
         <script type="text/javascript">
           jQuery(window).load(function() {
                        jQuery('#slider').nivoSlider();
                    });
          </script>";

        echo $output;

    }
}
125 posts I know Chuck
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
  • Sells items exclusively on Envato Market
+5 more
kotofey
says

Thanks Ivor.

Already done that. New experience was learned. But your information will be very helpful for other guys. :)

I think this thread can be closed.

153 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 50+ members
  • Has sold $10,000+ on Envato Market
  • Located in Belarus
+3 more
Ilyon
says

The fact is the condition

is_page_template('custom-home-page.php')
doesn’t work in functions.php, and always it returns ‘false’.

Any suggestions?

2468 posts
  • Has been part of the Envato Community for over 6 years
  • Has sold $100+ on Envato Market
  • Has referred 100+ members
  • Has been a beta tester for an Envato feature
+3 more
digitalimpact
says
Ilyon said
The fact is the condition
is_page_template('custom-home-page.php')
doesn’t work in functions.php, and always it returns ‘false’. Any suggestions?

Exactly, neither does is_page() ...

1950 posts Do the Needful
  • Had an item that became a weekly top seller
  • Has sold $125,000+ on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has been part of the Envato Community for over 5 years
+12 more
JamiGibbs
says

Here’s how I do conditional wp_enque in functions.php:

function jg_scripts() {

    if ( is_page_template('template-custom.php')  ) {

    wp_enqueue_script('scripts', get_template_directory_uri() . '/js/scripts.js', 'jquery');

    }
}
add_action('template_redirect', 'jg_scripts');
by
by
by
by
by
by