I've got a Wordpress site and have a .js file that I would like linked up so I can call the script. After much research, I've found this must be done with a hook in functions.php. I've tried all sorts of variations but cannot get the official method to work, but I have found a way that does work but just breaks the rest of the site in the process.
What am I doing wrong? I know about the get_stylesheet_directory_uri() & get_template_directory_uri() differences with parent and child themes but neither seem to make any difference.
Here's the 'official' way that doesn't work:
function add_jquery_script() {
wp_enqueue_script(
'my-script', // name your script so that you can attach other scripts and de-register, etc.
get_stylesheet_directory_uri() . 'http://<site>.com/wp-content/themes/dt-the7/custom-scripts.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
}
The 'not recommended at all' way that works but breaks the rest of the site:
function add_jquery_script() {
echo '<script type="text/javascript" src="http://<site>/wp-content/themes/dt-the7/custom-scripts.js"></script>' . "\n";
}
add_action('wp_head', 'add_jquery_script');
As always, any help much appreciated. Thanks guys
UPDATE
After echoing out get_stylesheet_directory_uri() I can see that my URL needs to be relative, which should now be as follows, however it still will not work.
function add_jquery_script() {
wp_enqueue_script(
'my-script', // name your script so that you can attach other scripts and de-register, etc.
get_stylesheet_directory_uri() . '/custom-scripts.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
}
PHP
You need to call the function add_jquery_script(); before it can be expect to output anything - pretty simple, easily forgotten.
JQuery
The script was breaking your code because of a reference error, var price definition should be moved inside the function so it is in the function's scope.
http://learn.jquery.com/javascript-101/scope/
$(document).ready(function () {
refresh();
$('#bittrex-price').load(bittrexPrice);
});
function refresh() {
var price = "[domain hidden]/realtime/bittrex-realtime.php";
setTimeout(function (price) {
$('#bittrex-price').fadeOut('slow').load(price, params).fadeIn('slow');
$('#bittrex-vol').fadeOut('slow').load(price, params2).fadeIn('fast');
refresh();
}, 1000);
}
Related
For some reason my jQuery script is not being registered, I believe I am doing it correctly but maybe I'm missing something. Here's my code:
/*registering script*/
function register_my_script(){
wp_register_script('alliance_script', get_template_directory_uri() . '/js/script.js');
}
add_action('enqueue_scripts', 'register_my_script');
In WordPress used wp_enqueue_scripts() to register your scripts.
For more information click here
add_action('wp_enqueue_scripts', 'my_enqueue_scripts');
function my_enqueue_scripts() {
wp_register_script( 'alliance_script', get_template_directory_uri() . '/js/script.js');
wp_enqueue_script( 'alliance_script' );
}
wp_register_script vs. wp_enqueue_script
wp_register_script() function makes your scripts available for use while wp_enqueue_script() functions loads the script to the theme/plugin.
You can register without enqueuing. But to load the script on the page, you need to enqueue. If you register the script, you can then enqueue it by its handle alone. If you don’t register it ahead of time, you will need to provide the full parameters in the enqueue function.
You can skip the register function for the scripts that you are going to enqueue right away. Also, you don’t need to register scripts which are included in WordPress.
I think the problem is that you're using 'enqueue_scripts' as action, which should be 'wp_enqueue_scripts'.
function register_my_script(){
wp_register_script('alliance_script', get_template_directory_uri() . '/js/script.js');
}
add_action('wp_enqueue_scripts', 'register_my_script');
I am pretty new as WordPress developer and I have the following problem.
I have create the template of this website starting from 0 by myself: http://www.asper-eritrea.com
Now I have add this photo gallery plugin: https://wordpress.org/plugins/robo-gallery/
The problem is that now, when I access to a page that use this plugin, for example: http://www.asper-eritrea.com/photo-gallery/
the gallery is not shown and, into the FireBug console, I obtain this error message:
TypeError: $ is not a function
$(document).ready(function() {
So I think that maybe it could depende by the fact that it can't retrieve JQuery, but I am absolutly not sure about it because I have the same error also opening the others page of my website so I am not sure that the missing of JQuery into the photogallery page is the cause of my problem.
Into the function.php file of my template I have the following function that enqueue all the JavaScript resources (including JQuery), in this way:
/* Hooks a function on to a specific action (an action is a PHP function that is executed at specific
* points throughout the WordPress Core)
* #param 'wp_enqueue_scripts': The name of the action to which 'wpb_adding_styles' is hooked (wp_enqueue_scripts
* is the proper hook to use when enqueuing items that are meant to appear on the front end)
*/
add_action('wp_enqueue_scripts', 'wpb_adding_styles');
/* Function automatically executed by the hook 'load_java_scripts':
* 1) Load all my JavaScripts
*/
function load_java_scripts() {
// Load JQuery:
wp_enqueue_script('jquery');
// Load FlexSlider JavaScript
wp_enqueue_script('flexSlider-js', get_template_directory_uri() . '/assets/plugins/flexslider/jquery.flexslider.js', array('jquery'), 'v2.1', true);
// Load bootstrap.min.js:
wp_enqueue_script('bootstrap.min-js', get_template_directory_uri() . '/assets/bootstrap/js/bootstrap.min.js', array('jquery'), 'v3.0.3', true);
// Load FancyBox:
wp_enqueue_script('fancy-js', get_template_directory_uri() . '/assets/plugins/fancybox/jquery.fancybox.pack.js', array('jquery'), 'v2.1.5', true);
// Load scripts.js:
wp_enqueue_script('myScripts-js', get_template_directory_uri() . '/assets/js/scripts.js', array('jquery'), '1.0', true);
// Load Modernizer:
wp_enqueue_script('myodernizer-js', get_template_directory_uri() . '/assets/js/modernizr.custom.js', array('jquery'), '2.6.2', true);
}
add_action('wp_enqueue_scripts', 'load_java_scripts');
So what is wrong? What am I missing?
The strange thing is that to enqueue JQuery there is:
wp_enqueue_script('jquery');
but there is not specified the exact folder of the Jquery source file (as done for the other resources).
I have JQuery putted into the /assets/bootstrap/js/jquery.js file.
Wordpress generally includes jQuery.noConflict() which removes the $ alias to prevent collisions with other libraries that may also be using $
Change:
$(document).ready(function() {
To
jQuery(document).ready(function($) {
// OK to use `$` here
This is probably a simple question to answer but my Jquery wont work on my theme at all no matter what tutorial ive followed.
my js file is located as /js/jquery.js and the code inside is just for testing purposes:
jQuery(document).ready(function($) {
$('#banner').hide();
});
my functions.php file is located in the same location as the index is and the code ive done for that is:
<?php
function add_my_script() {
wp_enqueue_script(
'jquery', // name your script so that you can attach other scripts and de-register, etc.
get_template_directory_uri() . 'js/jquery.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
}
add_action( 'wp_enqueue_scripts', 'add_my_script' );
?>
and nothing happens... can anyone correct me in what I am doing wrong? This is what puts me off using wordpress.
Chances are you need to have a slash before "js":
get_template_directory_uri() . '/js/jquery.js'
With that said, you could check your site using the network monitor in your browser's developer tools. It will show you what URLs are being fetched. Or maybe just check your developer tool's console. It could show you a 404 error if it is unable to fetch your jQuery file.
wp_enqueue_script(
'call this something else!', // name your script so that you can attach other scripts and de-register, etc.
get_template_directory_uri() . '/js/jquery.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
you had a couple of typos.....also check if jquery is available by using your dev.console in chrome or firefox(right click on page, click inspect element, and then console) you'll get a error jquery undefined or similiar.
Also on your page in frontend:
get_header();
get_footer();
you dont use the head tags as these functions pull in the header file & footer files. without get_header() & get_footer - you'll have issues :)
i have asked the question before, but i still can not figure out the full solution.
i want to make my wordpress site menu keyboard accessible, so far the dropdowns will show on tab but will not tab to the elements in the dropdown.
here is my fiddle: jsfiddle.net/X96gX/10/show/
it looks fine here, but when i tried to add the js to my wordpress functions.php im guessing it did not take in ondomready, but i have no idea how to customize that in php
any hints are appreciated!
edit: here is the code added to functions.php (it does show when i load the file as script in the header)
function includes_header_tab()
{
wp_enqueue_script( 'header-tab', dirname( get_bloginfo('stylesheet_url')).'/js/header-tab.js', array( 'jquery' ));
}
add_action('wp_enqueue_scripts', 'includes_header_tab');
function includes_header_jquery()
{
wp_enqueue_script( 'header-jquery', dirname( get_bloginfo('stylesheet_url')).'/js/header-jquery.js', array( 'jquery' ));
}
add_action('wp_enqueue_scripts', 'includes_header_jquery');
Inside your JS file, wrap the code within the DOMReady handler:
$(function() {
// existing JS code here.
});
If I have the following code before </head> the php form sends but the css crashes and doesn't display the dropdown menu.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
If I don't have the code, the submit button refreshes the page if blank and never sends anything but the dropdown menu works. If any of the fields are filled, the submit button returns with 404.
How can I keep both?
Additional details:
Wordpress CMS
The JQuery script was inserted in a field for customized js in WP.
I know JS is default, but try putting the type="text/javascript" in the <script> tag. Also, firebug for firefox has a NET tab that will most likely tell you what is going on. I would suggest if you can't figure it out by yourself, to post a screen shot of what is loading.
Wordpress can get finicky with manually embedded js and style embeds. The correct way to inject them is to add_actions to init, print_scripts and print_sytles. Add the following, or something like it to your Functions.php:
// register scripts
if (! function_exists(register_scripts){
function register_scripts() {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery','http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
}
}
add_action('init', 'register_scripts');
//print the now registered scripts
if (! function_exists(print_scripts){
function print_scripts() {
wp_enqueue_script( 'jquery' );
}
}
add_action('wp_print_scripts', 'print_scripts');
// do the same for css
if (! function_exists(print_styles){
function print_styles() {
wp_register_style( 'stylesheet', 'path to css'.stylesheet.css ); //bloginfo(url); or something like it to obtain your path
wp_enqueue_style( 'stylesheet' );
}
}
add_action('wp_print_styles', 'print_styles');
Its a little more work, but ensures scripts and styles get inserted correctly, and at the same time wordpress chooses to do so...
I'm not positive if this is causing your issue, but its a great place to start.