Wordpress Function Enqueue - I can’t add Jquery to the theme

Asked

Viewed 299 times

0

I am trying to add jquery in the theme, all css and js I put in Function are added in the code normally, but jquery that is in the directory of my theme is not being added. The file exists in the same folder as the /js/bootstrap.min.js

function enqueue_jquery() {
     // retira o jquery padrão do wordpress, ok retirou
     wp_deregister_script('jquery'); 

    // Não esta adicionando este jquery, que é o que preciso, no código
wp_enqueue_script(
    'jquery',
    get_template_directory_uri() . '/js/jquery.js',
    array(), // don't make jquery dependent on jquery...!
    '1.11.1',
    true
);

} 
add_action('wp_enqueue_scripts', 'enqueue_jquery');

function enqueue_styles_scripts() {

    //OK adicionou normal
    wp_enqueue_style(
        'style-theme',
        get_stylesheet_uri(),
        array('bootstrap-css')
    );

    //OK adicionou normal
    wp_enqueue_style(
        'bootstrap-css',
        get_template_directory_uri() . '/css/bootstrap.min.css'
    );

    //OK adicionou normal
    wp_enqueue_style(
        'stylish-portfolio',
        get_template_directory_uri() . '/css/othercss.css'
    );

    //OK adicionou normal
    wp_enqueue_style(
        'font-awesome',
        get_stylesheet_directory_uri() . '/css/font-awesome.css'

    ); 


    //Adicionou normal, mas como depende do jquery não esta funcionando
    wp_enqueue_script(
        'bootstrap-js',
        get_template_directory_uri() . '/js/bootstrap.min.js',null
    );


} 
add_action('wp_enqueue_scripts', 'enqueue_styles_scripts');


//ok adicionou normal 
function wpse_ie_conditional_scripts() { ?>
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <?php
}
add_action( 'wp_head', 'wpse_ie_conditional_scripts' );

?>

On the console only appears the error that bootstrap.min.js depends on jquery to work.

I appreciate help

1 answer

1


This happens because after using the wp_deregister_script('jquery') if it makes it necessary to 'register it' again before putting it back in the queue. See documentation.

So you could do something like:

function enqueue_jquery() {
    // retira o jquery padrão do wordpress
    wp_deregister_script('jquery' ); 

    // registra o novo jquery
    wp_register_script( 
         'jquery', 
         get_template_directory_uri() . '/js/jquery.js',
         array(), 
         '1.11.1', 
         true
    );

    // enfileira o novo jquery registrado 
    wp_enqueue_script( 'jquery');
} 
add_action('wp_enqueue_scripts', 'enqueue_jquery');
  • Thanks for the reply, I had already managed to solve with the following code directly, without using Function wp_deregister_script('jquery');&#xA; wp_register_script('jquery', get_template_directory_uri() . '/js/jquery.js', false, '1.11.1');&#xA; wp_enqueue_script('jquery');. Your explanation gave me a basis of understanding, would tell me what the effect of registering jquery outside or inside a Function?

  • When you speak outside, it would put the code you quoted above, without being within a function in the functions.php or straight to the template?

  • this is not within a function, and in the file Function.php

  • What happens is this: when you use one function allied to the add_action() or add_filter(), like up there, we’re saying the function enqueue_jquery is executed with the hook wp_enqueue_scripts, for example. It is common to see directly in the code, called direct configuration of the theme itself. But to be honest, I don’t know at what point (and the positive or negative consequences) the lines would happen without the use of the hook. Guess the added code would run in the action init of the theme.

  • It makes sense, thanks for the contribution. I posted one more question Here, If you can help me again thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.