113 posts I know Chuck
  • Elite Author
  • Has been a member for 4-5 years
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Envato Studio (Microlancer) Beta Tester
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 50 and 99 users
  • Russian Federation
ZenThemes says

Yes, it is a standard.

Just make your custom javascript load on wp_footer .

113 posts I know Chuck
  • Elite Author
  • Has been a member for 4-5 years
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Envato Studio (Microlancer) Beta Tester
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
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.
213 posts
  • Has been a member for 5-6 years
  • Bought between 50 and 99 items
  • Referred between 1 and 9 users
  • 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);
}
113 posts I know Chuck
  • Elite Author
  • Has been a member for 4-5 years
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Envato Studio (Microlancer) Beta Tester
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
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.

4317 posts ThemeForest Reviewer
  • Envato Staff
  • Reviewer
  • Community Moderator
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Venezuela
  • Has been a member for 5-6 years
  • Envato Studio (Microlancer) Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
+9 more
Ivor Reviewer 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;

    }
}
113 posts I know Chuck
  • Elite Author
  • Has been a member for 4-5 years
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Envato Studio (Microlancer) Beta Tester
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
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
  • Bought between 1 and 9 items
  • Europe
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Sold between 10 000 and 50 000 dollars
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?

2441 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Envato Studio (Microlancer) Beta Tester
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
  • Has been a member for 4-5 years
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • United States
  • Has attended an Envato Live event
  • Author had a Free File of the Month
  • Envato Studio (Microlancer) Beta Tester
  • Contributed a Blog Post
  • Beta Tester
+6 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