I have been trying to get malihu's Simple jQuery fullscreen image gallery ( http://manos.malihu.gr/simple-jquery-fullscreen-image-gallery ) to work with my Wordpress theme, but for some reason I'm having trouble getting the script to run. I'm calling both the CSS and javascript normally as with other plugins in the functions.php file, but the javascript doesn't seem to be taking effect to make the gallery. I currently have the following code to call the CSS in the header and the javascript in the footer. Am I missing something?
function malihu_gallery() {
if (!is_admin()) {
// Enqueue Malihu Gallery JavaScript
wp_register_script('malihu-jquery-image-gallery', get_template_directory_uri(). '/js/malihu-jquery-image-gallery.js', array('jquery'), 1.0, true );
wp_enqueue_script('malihu-jquery-image-gallery');
// Enqueue Malihu Gallery Stylesheet
wp_register_style( 'malihu-style', get_template_directory_uri() . '/CSS/malihu_gallery.css', 'all' );
wp_enqueue_style('malihu-style' );
}
}
}
add_action('init', 'malihu_gallery');
I'm thinking that I may need to ready the script with something similar to the following, but not sure if I'm on the right track.
function gallery_settings () { ?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#container').malihu_gallery();
});
</script><?php
Any help greatly appreciated!
Thanks
If you want any event to work in jQuery you will want it inside document ready. This will load it after the DOM is loaded and before the page content is loaded.
$(document).ready(function() {
// your stuff inside of here
});
Not sure based on what you have shown above but try some basic debugging, see if you can call functions when you paste your code into the console. Or if you want to create a fiddle I will take a look.
Related
I've tested the jQuery addClass : fiddle
That works fine.
The jQuery adds a class called lazy to the image.
Now I want to do the same in a wordpress site.
So I've added following code in my functions.php
function smart_magazine_scripts_styles() {
wp_enqueue_script('lazyloadscriot', get_template_directory_uri() . '/js/jquery.lazyload-any.min.js', array(), '1.0', true);
wp_enqueue_script('custom-by', get_template_directory_uri() . '/js/custom-by.js', array(), '1.0', true);
}
When I see the site source code I see both lazyload-any.min.js and custom-by.js are loaded fine.
custom-by.js has following line only :
jQuery('img').addClass('lazy');
But when I check the images there is no class called lazy is added to them.
Why is that?
Here is the site
Are you sure your document is ready? Try the following:
jQuery(document).ready(function() {
jQuery('img').addClass('lazy');
});
Have you tried this instead
$(function()
{
$('img').addClass('lazy');
});
If you are trying to target an element with the class img you need to use .img or if you are trying to target an element with the id img you need to use #img
To load jQuery from Google just put this line in your HTML file
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
When your function is executed jQuery may no have not been loaded yet.
First, try to put your JavaScript inside a $(function() like this:
jQuery(function(){
jQuery('img').addClass('lazy');
});
Of course if you wait for the DOM to be ready and then add the class lazy to your images there is no point. The best would be to add the class in the static templates or with PHP.
I want to use a read-more jQuery script on my wordpress website. The script I want to use can be found on https://github.com/jedfoster/Readmore.js. I've used several stackExchange topics and the following tutorial http://www.ericmmartin.com/5-tips-for-using-jquery-with-wordpress/. But I still don't have it working. What have I done?
Created a folder in the theme directory called "custom_js". In this folder I copied the script called "readmore.js".
Added the following piece of code on the top of the functions.php file located in the theme directory:
//this goes in functions.php near the top
function my_scripts_method() {
// register your script location, dependencies and version
wp_register_script('custom_script',
get_template_directory_uri() . '/custom_js/read_more.js',
array('jquery'),
'1.0' );
// enqueue the script
wp_enqueue_script('custom_script');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
The code in the readmore.js file is surrounded by this code:
;(function($) {
})(jQuery);
Did I implement the script the right way? If no, then what did I do wrong? And if I did implement the script the right way, how do I call the script so there will be a read-more button on my wordpresspage?
Everything looks fine apart from how you wrap the readmore.js code. You need to wrap it like this:
jQuery(function ($) {
/* You can safely use $ in this code block to reference jQuery */
// readmorejs contents here.
});
as opposed to
;(function($) {
})(jQuery);
As I can see from the plugin source it applies to article tag, so make sure your posts are wrapped in that tag.
Or you can change it from $('article').readmore(); to $('whatever article wrapper').readmore();, for example $('div.post').readmore();.
Also make sure that script is loaded and loaded after jQuery.
I am converting an HTML theme into WordPress. I am calling Wordpress jQuery using this function -
function insert_jquery(){
wp_enqueue_script('jquery');
}
add_filter('wp_enqueue_scripts','insert_jquery');
It's call WordpRess jQuery but not working all jquery elements properly. My main slider is not working, but others slider is working. How can can can I fix?
And I am using this to call other jQuery-
function load_custom_scripts() {
wp_enqueue_script('custom_script', get_template_directory_uri() . '/js/scripts.js', array('jquery'), false, true);
}
add_action('wp_enqueue_scripts', 'load_custom_scripts');
You most likely need to replace change $ in your jQuery to 'jQuery'. E.g.
jQuery(document).ready(function($) {
// code here
});
But your question doesn't give enough information to go on. What errors are you seeing in the console?
I am new to jQuery and have tried to get scripts to work to no avail. I would like to simplify my script to just see that my theme is in fact recognizing my scriptfile.js
My js file is just:
var success = 'SUCCESS!';
jQuery('nav .home a').hover(function()
{
echo(success);
});
my functions.php file has this:
<?php
function add_my_script() {
wp_enqueue_script(
'preview',
get_template_directory_uri() . '/js/scriptfile.js',
array('jquery')
);
}
add_action( 'wp_enqueue_scripts', 'add_my_script' );
?>
My header.php contains:
<script type="text/javascript"><!--//--><![CDATA[//><!--
jQuery(document).ready(function(jQuery){
});
//--><!]]></script>
I am not getting any errors in firebug, but no output either.
Not sure if this is the problem but first of all your selector is not exactly right.
var success = 'SUCCESS!';
jQuery('nav .home a').hover(function(){ ... });
nav should be .nav for class or #nav for id
echo(success);
Unless echo() is a function somewhere it will not work. If you want to show an alert message use alert(success)
I believe what's happening is that your script file is being included fine but the way it's written the hover event attachment will happen before the DOM is ready. Try this, simply say alert(success); in your js file and see if the alert pops up. If it does, then wrap the hover event handler in a function in your script file and call that function from the document.ready function handler in your header.php. By the way, what's the echo function? Probably you are looking for alert.
I have a custom jquery-based slider on homepage of a WordPress site. I'm loading jQuery (jquery.js) and the slider js (jquery.advanced-slider.js) using below code in my functions.php file of my theme.
function my_load_scripts() {
if (!is_admin()) {
wp_enqueue_script("jquery");
if (is_home()) {
wp_enqueue_script('theme-advanced-slider', get_template_directory_uri().'/js/jquery.advanced-slider.js', array('jquery'));
wp_enqueue_script('theme-innerfade', get_template_directory_uri().'/js/innerfade.js', array('jquery'));
}
}
}
add_action('wp_enqueue_scripts', 'my_load_scripts');
Now I put the below code in my header.php file BEFORE wp_head();
<script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function () {
//Slider init JS
$j(".advanced-slider").advancedSlider({
width: 614,
height: 297,
delay: 1000,
pauseRollOver: true
});
});
</script>
It's obvious here that my jquery.js and jquery.advanced-slider.js load after the JavaScript code above and thus my slider doesn't work. If I put it after wp_head() call in <head> the slider works.
But if I put it after wp_head() wouldn't that be a hack? I want my code to be clean. As twentyeleven theme clearly says
/* Always have wp_head() just before the closing </head>
* tag of your theme, or you will break many plugins, which
* generally use this hook to add elements to <head> such
* as styles, scripts, and meta tags.
*/
I'm very confused here. I might be missing something very simple clue here. But help me out guys. What would be the best way to put the JavaScript before wp_head() and yet have it load after jquery.js and slider.js have loaded?
Add your script block to the bottom of the HTML page, just before the </body>.
Your slider init JS code won't get executed until the full DOM has been parsed in the browser anyway so there is no disadvantage to this.
In fact web performance experts like Steve Souders suggest that script blocks should be added at the end of your HTML page as script blocks block rendering and the page appears to load faster this way.
As per wordpress function reference:
The safe and recommended method of adding JavaScript to a WordPress generated page is by using wp_enqueue_script(). This function includes the script if it hasn't already been included, and safely handles dependencies.
If you want to keep your "code clean" you might rethink how your js is organised.
My approach with wordpress is to (usually) keep a scripts.js for all initialisation and small scripts and separate file(s) for plugins. But everything depends how big and how many files you have - you want to avoid too many http requests and files that are hard to manage.
As well, wp_enqueue_script lets you place the scripts in the footer.
http://codex.wordpress.org/Function_Reference/wp_enqueue_script
Wait for loading jQuery with window.onload function, once jQuery file and other js / content loaded then window.onload function will be fire.
<script type="text/javascript">
window.onload = function(){
var $j = jQuery.noConflict();
$j(".advanced-slider").advancedSlider({
width:614,
height:297,
delay:1000,
pauseRollOver:true
});
}
</script>
The code that you are adding to the bottom of the page should be externalized to it's own file and added with the wp_enqueue_script(); function like shown below.
Note the use of the 'true' directive at the end of each call to wp_enqueue_script. That tells WordPress that you want them included at the wp_footer(); hook (instead of the wp_head(); hook) which should appear directly before the closing </body> tag - as per the performance best practices that others have mentioned.
function my_load_scripts() {
if (is_home()) {
wp_enqueue_script('theme-advanced-slider', get_template_directory_uri().'/js/jquery.advanced-slider.js', array('jquery'), true);
wp_enqueue_script('theme-innerfade', get_template_directory_uri().'/js/innerfade.js', array('jquery'), true);
wp_enqueue_script('your-custom-script', get_template_directory_uri().'/js/yourcustomscript.js', array('jquery'), true);
}
}
}
add_action('wp_enqueue_scripts', 'my_load_scripts');
I've removed the jQuery enqueue line you had included because you shouldn't need to register the jQuery script as it's already registered by the WP core and since you have defined it as one of the dependencies it will be included for you.
Since you have wrapped it all in an is_home() conditional statement the all 3 of these files - plus jQuery - will be included in just the homepage.
An easier - but maybe not so nice - hack is to simply add a 1-millisecond timeout
setTimeout(function () {
jQuery('#text-6').insertBefore('#header');
}, 1);
get_template_directory_uri() does give you the theme directory, but this is not what you want if you are using a Child Theme.
get_stylesheet_directory_uri() will give your the directory of your "current theme", so in either case, it is safest to use.