128 posts I know Chuck
  • Elite Author
  • Author Level 9
  • 6 Years of Membership
  • Exclusive Author
+6 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
  • 5 Years of Membership
  • Affiliate Level 3
  • Collector Level 2
  • Exclusive Author
+1 more
ZenThemes
says

Yes, it is a standard.

Just make your custom javascript load on wp_footer .

128 posts I know Chuck
  • Elite Author
  • Author Level 9
  • 6 Years of Membership
  • Exclusive Author
+6 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
  • Affiliate Level 1
  • Collector Level 3
  • 7 Years of Membership
  • Exclusive Author
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);
}
128 posts I know Chuck
  • Elite Author
  • Author Level 9
  • 6 Years of Membership
  • Exclusive Author
+6 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.

4483 posts ThemeForest Senior Reviewer
  • Affiliate Level 1
  • Author Level 6
  • Collector Level 3
  • 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;

    }
}
128 posts I know Chuck
  • Elite Author
  • Author Level 9
  • 6 Years of Membership
  • Exclusive Author
+6 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
  • 5 Years of Membership
  • Affiliate Level 3
  • Author Level 5
  • Collector Level 1
+4 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
  • 7 Years of Membership
  • Exclusive Author
  • Author Level 2
  • Collector Level 2
+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
  • Envato Team
  • 6 Years of Membership
  • Author Level 8
  • Elite Author
+14 more
JamiGibbs
Envato team
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