How to load javascript file on homepage in WordPress in order?

I am using a quicktags plugin, which was not loading on the home page, now it does, but it does it on the wrong place. How can I solve this?

I want the quicktags.min.js file to load first, rather than the basic-comment quicktags…

Read More

I am using this script:

add_action( 'wp_enqueue_scripts', 'ron_scripts' );
function ron_scripts(){
   if(is_home()){
     wp_register_script( 'quicktags', '/wp-includes/js/quicktags.min.js?ver=3.5.1');
     wp_enqueue_script( 'quicktags' );
     wp_register_script( 'basic-comment-quicktags', '/wp-content/plugins/basic-comment-quicktags/quicktags.js' );
     wp_enqueue_script( 'basic-comment-quicktags' );
     } else {

    }
}

What should I do to get it work like I want it to be?

Edit: so now I am using:

function quicktags_script() {
    wp_register_script( 'quicktags-min', '/wp-includes/js/quicktags.min.js?ver=3.5.1');
    wp_enqueue_script( 'quicktags-min' );
wp_register_script( 'basic-comment-quicktags', '/wp-content/plugins/basic-comment-quicktags/quicktags.js' );
     wp_enqueue_script( 'basic-comment-quicktags' );
}

add_action( 'wp_enqueue_scripts', 'quicktags_script' );

….

Can I get the basic-comment-quicktags beneath all scripts?

Edit 2:

If I change quicktags-min to quicktags, it does not give me the l10n error and it shows it to me in my page source. This time I get this error: Permission denied to access property 'toString'

Related posts

2 comments

  1. In your functions.php:
    edit: First register the quicktags script under a different name than already registered in wp “quicktags” , see Codex

    function quicktags_script() {
        wp_register_script( 'quicktags-min', '/wp-includes/js/quicktags.min.js?ver=3.5.1','','',true);
        wp_enqueue_script( 'quicktags-min' );
    }
    
    add_action( 'wp_enqueue_scripts', 'quicktags_script' );
    

    This will load the script for all the pages on your front-end.
    To load it only on front-page :

    function quicktags_script() {
        if(is_front_page()){
            wp_register_script( 'quicktags-min', '/wp-includes/js/quicktags.min.js?ver=3.5.1','','',true);
            wp_enqueue_script( 'quicktags-min' );
        }
    }
    
    add_action( 'wp_enqueue_scripts', 'quicktags_script' );
    

    Just add the last parameter $in_footer for the wp_register_script to true (see the edited answer) . Or add ‘quicktags-min’ as the third parameter for the basicktags register function

    wp_register_script( 'basic-comment-quicktags', '/wp-content/plugins/basic-comment-quicktags/quicktags.js','quicktags-min' );
    

    This will first load quicktags-min and then the basictags.

  2. The third parameter of wp_register_script is $deps— dependencies. wp_enqueue_script also accepts a $deps parameter, by the way.

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

    $deps is an array of scripts upon which the script being registered depends. Dependencies load before the scripts upon which they depend. WordPress will juggle things around for you if you register things correctly.

    function quicktags_script() {
        wp_register_script( 
           'quicktags-min', 
           '/wp-includes/js/quicktags.min.js?ver=3.5.1'
        );
    
        wp_register_script( 
           'basic-comment-quicktags', 
           '/wp-content/plugins/basic-comment-quicktags/quicktags.js',
           array('quicktags-min')
        );
    
        wp_enqueue_script( 'basic-comment-quicktags' );
        wp_enqueue_script( 'quicktags-min' );
    }
    
    add_action( 'wp_enqueue_scripts', 'quicktags_script' );
    

    That is what your original question sounded like you wanted. Now, you have rewritten asking how to get “basic-comment-quicktags beneath all scripts”. To do that, you probably want to enqueue the script very late with a priority on the wp_enqueue_scripts hook. You can register the script as normal and enqueue it like this:

    function enqueue_bcquicktags() {
        wp_enqueue_script( 'basic-comment-quicktags' );
    }
    add_action( 'wp_enqueue_scripts', 'enqueue_bcquicktags', 1000 );
    

    You may also want to look into the fifth parameter, $in_footer, as that will load your script in the footer of the page. Without that, it should load last in the <head> of the page.

Comments are closed.