[ACCEPTED]-How to load jQuery into WordPress properly-wordpress

Accepted answer
Score: 14

jQuery is included in WordPress core and 19 when you include your javascript, you can 18 declare that your script is dependent on 17 jquery being loaded as the third parameter 16 of wp_enqueue_script: http://codex.wordpress.org/Function_Reference/wp_enqueue_script.

The proper way to 15 use wp_enqueue_script is to include it in 14 a callback that is attached to a specific 13 action hook. That sounded incredibly complicated 12 to me when I started with WordPress, but 11 it's actually a really good system because 10 it allows you to specify when your code should 9 run during the initialization of WordPress.

So, in 8 your functions.php or a plugin, you add your hook.

add_action( 'wp_enqueue_scripts', 'my_js_include_function' );

Then, in 7 that function, you running the actual inclusion, and 6 declare that your script is dependent on 5 jquery.

function my_js_include_function() {
    wp_enqueue_script( 'my_script.js', '/path/to/myscript.js', array('jquery') );

The first parameter of wp_ennqueue_script() is 4 an arbitrary name you are assigning to your 3 script. If you were to then declare another 2 script was dependent on the original one, that's 1 how you would refer to it in the third parameter.

Score: 0

If you can't use any WordPress methods, you 2 can still include your Jquery plugin manually 1 inside your extension_name.php, right after your wp_enqueue_script("jquery");

echo "<script src='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js'></script>";
Score: 0

Best way to do this is through the functions.php 13 file,

URL: http://codex.wordpress.org/Function_Reference/wp_enqueue_script


 * Proper way to enqueue scripts and styles
function theme_name_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

What this code reads is 12 that you are creating a function called 11 "theme_name_scripts", now you 10 are calling wp_enqueue_script(), this takes 9 5 parameters "Script Name", "URL","ARRAY","Version","True/False" (True 8 loads in the Header, and False in the Footer)...

Add_action 7 part is a hook to attach this to the WordPress. It 6 is self explanatory, you need it in order 5 to make the function work. It says wp_enqueue_scripts 4 from function 'theme_name_scripts' or the 3 function name..

Best of luck, if you need 2 more help try Googleing "WordPress, enqueue 1 Scripts tutorial"

Score: 0

in my functions.php:

function mytheme_scripts() {
    wp_enqueue_script( 'myscript', get_template_directory_uri() . '/js/myscript.js', array('jquery'), false, true );
add_action( 'wp_enqueue_scripts', 'mytheme_scripts' );

function custom_shortcode3() {

echo "wp_enqueue_scripts();";
add_shortcode( 'totaldeprodutos', 'custom_shortcode3' );

in my /js/myscript.js:

<div id="show"></div>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

                    function total() {
        setInterval(function () {
        }, 1000);

I 1 need shortcode show to function total()

More Related questions