C
C
coderisimo2015-10-28 00:30:40
JavaScript
coderisimo, 2015-10-28 00:30:40

Is there a way to load JS script for plugin in HEAD on WP without editing functions?

I am writing a plugin for WP.
I register in the plugin itself the connection of the necessary scripts through:

wp_enqueue_script('jquery-ui-tabs');
 wp_enqueue_script('jquery-ui-dialog');
//далее
 add_action( 'wp_enqueue_scripts', 'my_scripts' );

tried to cling to different hooks, but as a result, the scripts are always connected in the footer. Accordingly, my markup loads faster than the required JS, which in this case leads to clumsiness in displaying the same tabs. Is it possible to load the necessary scripts in HEAD using plugin tools only, without editing functions?
Thank you.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
W
WP Panda, 2015-10-28
@coderisimo

Jquery Ui is loaded in the footer by default, to reconnect you need to override this case.

// в админке осталяем как было
    if( ! is_admin() ) { 
//переопределяем загрузку ядра
    wp_deregister_script( 'jquery-ui-core' );
    wp_enqueue_script( 'jquery-ui-core', site_url(  '/wp-includes/js/jquery/ui/core.min.js' ), array('jquery') );
//переопределяем загрузку api виджетов
    wp_deregister_script( 'jquery-ui-widget' );
    wp_enqueue_script( 'jquery-ui-widget', site_url(  '/wp-includes/js/jquery/ui/widget.min.js' ), array('jquery') );
// табы
    wp_deregister_script( 'jquery-ui-tabs' );
    wp_enqueue_script( 'jquery-ui-tabs', site_url(  '/wp-includes/js/jquery/ui/tabs.min.js' ), array('jquery') );
//диалог
    wp_deregister_script( 'jquery-ui-dialog' );
    wp_enqueue_script( 'jquery-ui-dialog', site_url(  '/wp-includes/js/jquery/ui/dialog.min.js' ), array('jquery') );

}

A
Alex, 2015-10-28
@mr_ko

By default, scripts should be displayed in the Header https://codex.wordpress.org/Function_Reference/wp_... if you set the last parameter to true then it will be in the footer. Perhaps you have some kind of plugin, or the theme has an output in the footer.

C
coderisimo, 2015-10-28
@coderisimo

default theme is just the point (((
function my_scripts(){
wp_enqueue_style('fitness_calc_css',plugins_url('fitness_calc/ui/jquery-ui.css'));
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui -tabs');
wp_enqueue_script('jquery-ui-dialog');
wp_enqueue_script('jquery-ui-tooltip');
wp_enqueue_script('fitness_calc',plugins_url('fitness_calc/fitness_calc.js'),array(),false, true);
}
add_action( 'wp_head', 'my_scripts' ); I
've tried other hooks too. I don't know where to dig ((

A
Alexey Nikolaev, 2015-10-28
@Heian

Working option. Scripts \ styles should always be included via wp_enqueue_scripts \ wp_enqueue_styles respectively.

function my_scripts() {
    wp_enqueue_script(
        'fitness_calc', 
        plugins_url('fitness_calc/fitness_calc.js'), 
        array(), null,
        false // этот - последний - параметр указывает, в футере выводить или нет
    );
}
add_action('wp_enqueue_scripts',  'my_scripts', 100);

In general, this is normal, as well as placing scripts in the footer. Why not activate js-logic (tabs, as an example) only when the DOM is ready for it, and the scripts are loaded? preloader. For example, add the .loader class, and remove it when loading.
Putting the script in the header in the vast majority of cases is a crutch and wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question