Answer
There are a few things to take into account for a proper inclusion of script
on the page:
Correct file
The file functions.php
where you should address the inclusion of your script
is located in:
/themes/nome-do-tema/functions.php
jQuery to use
You should not remove jQuery that already comes with Wordpress unless you are absolutely sure that you will not break something because you’re changing the version to the one you want to include:
The appropriate function to gather the path to the theme and sub-themes so that there is a attention to the theme-father and theme-son is the get_stylesheet_directory_uri
:
Retrieve stylesheet directory URI for the Current Theme/Child Theme. Checks for SSL.
That translated:
Tracks the current theme/child theme style sheet. Checks for SSL.
In light of all this attention, your code based on the comment you left on reply from @Victor Mendonça would be as follows:
/**
* Função onde adicionamos à lista de inclusões
* todos os scripts que pretendemos incluir.
*/
function my_scripts() {
// script 01
wp_enqueue_script(
'widget_min',
get_stylesheet_directory_uri() . '/js/jquery/jquery.widget.min.js',
array('jquery')
);
// script 02
wp_enqueue_script(
'metro_min',
get_stylesheet_directory_uri() . '/js/metro/metro.min.js',
array('jquery')
);
// ...
}
// adicionamos uma acção que chama a nossa função
add_action( 'wp_enqueue_scripts', 'my_scripts' );
Note:
It should work assuming you have the script
where you are indicating that they are and assuming that you already have jQuery present.
You can also check the output of the paths in use to ensure that they are pointing to the desired location/file:
// caminho recolhido
echo get_stylesheet_directory_uri();
// caminho recolhido + restante até ao teu ficheiro
echo get_stylesheet_directory_uri() . '/js/metro/metro.min.js';
The function wp_enqueue_script
is responsible and recommended to include script
:
Description
Links a script file to the generated page at the right time According to the script dependencies, if the script has not been already included and if all the dependencies have been Registered. You could either link a script with a Handle Previously Registered using the wp_register_script() Function, or provide this Function with all the Parameters necessary to link a script.
This is the Recommended method of linking Javascript to a Wordpress generated page.
That translated:
Binds a script file to the generated page at the right time according to the dependencies of it, if the script has not been included, and if all dependencies have been registered. You could include a script with a previously registered identifier using wp_register_script Function(), or provide this function with all the necessary parameters to include a script.
This is the recommended method to include Javascript in a Wordpress generated page.
Parameters
It accepts a series of parameters to better control as the script
should be included:
<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>
Parameters:
$handle
Name used as an identifier of the script
.
$src
Path to the file containing the script
.
$deps
Matrix with indication of the dependencies that this script
has, an example will be:
array('jquery') // indica que o script precisa de ter o jquery carregado para funcionar
$ver
Optional field to indicate the version of script
.
$in_footer
Optional field to specify if the script is inside the HEAD
, must be immediately before the closure of the BODY
.
Example of Use
A simple example of using this function:
/**
* Função onde adicionamos à lista todos os scripts
* que pretendemos incluir.
*/
function my_scripts() {
wp_enqueue_script(
'nome-do-script',
get_template_directory_uri() . '/js/meu_ficheiro.js',
array('jquery'),
'1.0.0',
true
);
}
// adicionamos uma acção que chama a nossa função
add_action( 'wp_enqueue_scripts', 'my_scripts' );
Response credits
- Answer given by @b__on SOEN
- Answer given by @Chip Bennett on Wordpress
- Wordpress documentation for add_action
- Wordpress documentation for get_stylesheet_directory_uri
- Wordpress documentation for wp_enqueue_script
- Don’t Dequeue Wordpress' jQuery by Eric Mann
It didn’t work, here’s my file functions.php
function my_init_method() {
 wp_deregister_script( 'jquery' );
 wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js');
 wp_register_script( 'widget.min', get_template_directory_uri() . '/js/jquery/jquery.widget.min.js');
 wp_register_script( 'metro.min', get_template_directory_uri() . '/js/metro/metro.min.js');
 
} 
 
add_action('init', 'my_init_method');
– Edizzz