How do I add a simple jQuery script to WordPress?

后端 未结 18 1708
心在旅途
心在旅途 2020-11-22 16:53

I read the Codex and a few blog posts about using jQuery in WordPress, and its very frustrating. I\'ve got as far as loading jQuery in functions.php file, but a

相关标签:
18条回答
  • 2020-11-22 17:52

    You can add jQuery or javascript in theme's function.php file. The code is as below :

    add_action( 'wp_enqueue_scripts', 'add_my_script' );
    
    function add_my_script() {
    wp_enqueue_script(
        'your_script_name', // your script unique name 
        get_template_directory_uri().'/js/your-script.js', //script file location
        array('jquery') //lists the scripts upon which your script depends
    );
    }
    

    For more detail visit this tutorial : http://www.codecanal.com/add-simple-jquery-script-wordpress/

    0 讨论(0)
  • 2020-11-22 17:52
    function xyz_scripts() {
        wp_enqueue_script('jquery');
    }
    add_action('wp_enqueue_scripts', 'xyz_scripts');
    
    0 讨论(0)
  • 2020-11-22 17:54

    Beside putting the script in through functions you can "just" include a link ( a link rel tag that is) in the header, the footer, in any template, where ever. You just need to make sure the path is correct. I suggest using something like this (assuming you are in your theme's directory).

    <script type="javascript" href="<?php echo get_template_directory_uri();?>/your-file.js"></script>
    

    A good practice is to include this right before the closing body tag or at least just prior to your footer. You can also use php includes, or several other methods of pulling this file in.

    <script type="javascript"><?php include('your-file.js');?></script>
    
    0 讨论(0)
  • 2020-11-22 17:55

    Do you only need to load jquery?

    1) Like the other guides say, register your script in your functions.php file like so:

    // register scripts
    if (!is_admin()) {
        // here is an example of loading a custom script in a /scripts/ folder in your theme:
        wp_register_script('sandbox.common', get_bloginfo('template_url').'/scripts/common.js', array('jquery'), '1.0', true);
        // enqueue these scripts everywhere
        wp_enqueue_script('jquery');
        wp_enqueue_script('sandbox.common');
    }
    

    2) Notice that we don't need to register jQuery because it's already in the core. Make sure wp_footer() is called in your footer.php and wp_head() is called in your header.php (this is where it will output the script tag), and jQuery will load on every page. When you enqueue jQuery with WordPress it will be in "no conflict" mode, so you have to use jQuery instead of $. You can deregister jQuery if you want and re-register your own by doing wp_deregister_script('jquery').

    0 讨论(0)
  • 2020-11-22 17:56

    "We have Google" cit. For properly use script inside wordpress just add hosted libraries. Like Google

    After selected library that you need link it before your custom script: exmpl

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    

    and after your own script

    <script type="text/javascript">
        $(document).ready(function () {
            $('.text_container').addClass("hidden");
        });
    </script>
    
    0 讨论(0)
  • 2020-11-22 17:57

    You can add custom javascript or jquery using this plugin.
    https://wordpress.org/plugins/custom-javascript-editor/

    When you use jQuery don't forget use jquery noconflict mode

    0 讨论(0)
提交回复
热议问题