I´ve spent a good while trying to figure this out. Im trying to add a script that need an external jquerylibrary. I can make it work by inserting my script between scripttags, however i understand thats not the correct way to do it, and it breaks another script on the site.
I spent a fair amount of time tonight trying to figure out how to add the script correct, and i just cant get it.
I understand something like this is the correct way to enqueue the script:
function my_scripts_method() {
wp_register_script( 'jquery', 'http://code.jquery.com/jquery-1.9.1.js');
wp_enqueue_script( 'jquery' );
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
My main question is, how do i write my functions so it calls the library, and at the same time failsafe it so it only loads once, and doesnt crash with other scripts? This is the script:
$(document).ready(function () {
$("#menu-item-16").hover(
function () {
$('body').css("background", "#ff9900");
},
function () {
$('body').css("background", "#ccc");
}
);
$("#menu-item-17”).hover(
function () {
$('body').css("background", "red");
},
function () {
$('body').css("background", "#ccc");
}
);
$("#menu-item-18”).hover(
function () {
$('body').css("background", "yellow");
},
function () {
$('body').css("background", "#ccc");
}
);
});
Edit:
Second question, several librarys and a stylesheet.
As said above i have a little more complex script that you guys might be able to help me out.
I have this code in the header right now, and it works.
`<link rel="stylesheet" type="text/css" href="/wp-content/themes/neighborhood/js/jquery.fullPage.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"> </script>
<script type="text/javascript" src="/wp-content/themes/neighborhood/js/vendors/jquery.slimscroll.min.js"></script>
<script type="text/javascript" src="/wp-content/themes/neighborhood/js/jquery.fullPage.js"></script>
<script>
$(document).ready(function() {
$.fn.fullpage({
verticalCentered: true,
resize : true,
slidesColor : ['#AF1700', '#359973', '#F46321', '#A6C7DB'],
scrollingSpeed: 700,
easing: 'easeInQuart',
menu: false,
navigation: false,
navigationPosition: 'right',
navigationTooltips: ['firstSlide', 'secondSlide'],
slidesNavigation: true,
slidesNavPosition: 'bottom',
loopBottom: false,
loopTop: false,
loopHorizontal: true,
autoScrolling: true,
scrollOverflow: true,
css3: false,
paddingTop: '3em',
paddingBottom: '10px',
fixedElements: '#element1, .element2',
normalScrollElements: '#element1, .element2',
keyboardScrolling: true,
touchSensitivity: 15,
continuousVertical: false,
animateAnchor: false,
setScrollingSpeed: 1000,
});
});
</script>
`
From my newfound insights, i tried this, and it didnt work:
`
function fullpage() { wp_enqueue_script('jquery');
wp_register_style( ’fullpage-css', get_template_directory_uri() . '/js/jquery.fullPage.css','','', 'screen' );
wp_register_script( 'jquery.1.8.3', get_template_directory_uri() . 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', array('jquery'),'',true );
wp_register_script( 'jquery.1.9.1', get_template_directory_uri() . 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js', array('jquery'),'',true );
wp_register_script( 'fullpage', get_template_directory_uri() . '/js/jquery.fullPage.js', array('jquery'),'',true );
wp_register_script( 'fullpagecode', get_template_directory_uri() . '/js/fullpagecode.js', array('jquery'),'',true );
wp_register_script( 'slimscroll', get_template_directory_uri() . '/js/vendors/jquery.slimscroll.min.js', '', null,'' );
wp_enqueue_style( 'fullpage-css' ); // Enqueue our stylesheet
wp_enqueue_script( 'jquery.1.8.3' ); // Enqueue our first script
wp_enqueue_script( 'jquery.1.9.1' ); // Enqueue our second script
wp_enqueue_script( 'fullpage' ); // Enqueue our third script
wp_enqueue_script( 'fullpagecode' ); // Enqueue fourth script
wp_enqueue_script( ’slimscroll’ ); // Enqueue fifth script
}add_action( 'wp_enqueue_scripts', ’fullpage’ ); `
Don't run your jquery in a script. This will break other scripts in wordpress.
Make a .js file (eg. example.js and put this in your theme folder under a /js directory) and 'enqueue' it in your functions.php file like this.
function theme_styles() {
wp_register_style( 'fullpage-css', get_template_directory_uri() . '/css/jquery.fullPage.css' );
wp_enqueue_style( 'fullpage-css' );
}
add_action('wp_enqueue_scripts', 'theme_styles');
function theme_scripts() {
wp_register_script( 'fullpage', get_template_directory_uri() . '/js/jquery.fullPage.js', array('jquery'),'1.0.0',true );
wp_enqueue_script( 'fullpage' );
wp_register_script( 'fullpagecode', get_template_directory_uri() . '/js/fullpagecode.js', array('jquery'),'1.0.0',true );
wp_enqueue_script( 'fullpagecode' );
wp_register_script( 'slimscroll', get_template_directory_uri() . '/js/vendors/jquery.slimscroll.min.js', array('jquery'), null, true );
wp_enqueue_script( 'slimscroll' );
}
add_action('wp_enqueue_scripts', 'theme_scripts');
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
$src is the path to the js file.
$deps is where you define this script requires jquery, like this. array('jquery')
You don't need to include jquery as wordpress has this built in.
Then in your .js file in your theme folder wrap your code in a No Conflict wrapper. Using $() will return undefined otherwise.
jQuery(document).ready(function($) {
$("#menu-item-16").hover(
function () {
$('body').css("background", "#ff9900");
},
function () {
$('body').css("background", "#ccc");
}
);
$("#menu-item-17").hover(
function () {
$('body').css("background", "red");
},
function () {
$('body').css("background", "#ccc");
}
);
$("#menu-item-18").hover(
function () {
$('body').css("background", "yellow");
},
function () {
$('body').css("background", "#ccc");
}
);
});
Related
I'm trying to develop my own theme and i've run in the following issue:
I use WP-RECIPE and I can't use the button "print recipe", but if I install other theme that button is working. So I thought it's because of the plugin, so I installed something random like 'social share', same output. What's wrong?
PRINT
This is my functions.php for styles and scripts:
<?php
//JAVASCRIPT SI CSS
function pezona_enqueue_styles() {
wp_enqueue_style(
'layout',
get_stylesheet_directory_uri() . '/assets/css/layout.css',
array(),
false,
'all'
);
wp_enqueue_style(
'stil',
get_stylesheet_directory_uri() . '/assets/css/stil.css',
array(),
false,
'all'
);
wp_enqueue_style(
'stil-responsive',
get_stylesheet_directory_uri() . '/assets/css/stil-responsive.css',
array(),
false,
'all'
);
}
add_action( 'wp_enqueue_scripts', 'pezona_enqueue_styles' );
function pezona_enqueue_scripts() {
wp_enqueue_script(
'main-js',
get_stylesheet_directory_uri() . '/assets/javascript/lazysizes.min.js',
array(),
'1.0.0',
true
);
}
add_action( 'wp_enqueue_scripts', 'pezona_enqueue_scripts' );
?>
Any ideea guys? thanks.
I did a search and found this method here using wp_add_inline_script. And it works.
But what if I wanted to keep my initialization script into an other .js file? That's what I wanted at first. How I can I load correctly?
I tried this but the loading of my init-fancybox must be too early because I get an error.
(my init-fancybox script is not the problem because it works when it is loaded correctly using wp_add_inline_script)
function fancybox_enqueues() {
wp_register_script( 'fancybox', get_theme_file_uri( '/assets/js/jquery.fancybox.min.js' ), array('jquery'), '3.5.7', true);
wp_register_script( 'init-fancybox', get_theme_file_uri( '/assets/js/init-fancybox.js' ), array('jquery','fancybox'), '1.0', true);
wp_register_style( 'fancybox-css', get_theme_file_uri( '/css/jquery.fancybox.min.css' ), '1.0');
if ( is_singular( 'item' ) ) {
wp_enqueue_script( 'jquery');
wp_enqueue_script( 'fancybox' );
wp_enqueue_script ('init-fancybox');
wp_enqueue_style( 'fancybox-css' );
}
}
add_action( 'wp_enqueue_scripts', 'fancybox_enqueues');
Here is the error I get TypeError: $ is not a function
And here is the init-fancybox.js
$('[data-fancybox="single-item__gallery"]').fancybox({
buttons : [
"zoom",
"share",
//"slideShow",
"fullScreen",
//"download",
"thumbs",
"close"
],
thumbs : {
autoStart : false
}
});
It seems jQuery is not loaded by the time your code is running. Try adding the following function around it to make sure jQuery exists before your code runs:
jQuery(document).ready(function ($) {
// Your function goes here:
$('[data-fancybox="single-item__gallery"]').fancybox({
buttons: [
"zoom",
"share",
//"slideShow",
"fullScreen",
//"download",
"thumbs",
"close"
],
thumbs: {
autoStart: false
}
});
});
Thanks for reading,
How would I load my JS files after the footer instead of being in the header
example:
<head>
<script type="text/javascript"></script>
<script type="text/javascript"></script>
<script type="text/javascript"></script>
</head>
Changed to
</footer>
<script type="text/javascript"></script>
<script type="text/javascript"></script>
<script type="text/javascript"></script>
</body>
</htmL>
I've tried
wp_register_script( 'cycle', 'http://malsup.github.com/jquery.cycle2.js', '', '', true);
wp_enqueue_script( 'cycle' );
wp_register_script( 'site', get_template_directory_uri().'/js/site.js', '', '', true);
wp_enqueue_script( 'site' );
The last parameter dictates where to enqueue the script, in footer (true) or in header (false - default):
<?php wp_register_script( $handle, $src, $deps, $ver, $in_footer ); ?>
You have to hook the scripts righteously (please follow the inline comments):
To hook to the front-end:
add_action( 'wp_enqueue_scripts', 'function_name' );
To hook to the admin panel:
add_action( 'admin_enqueue_scripts', 'function_name' );
Here's how you should proceed:
<?php
function themeslug_load_scripts() {
//registering the scripts, the last parameter will dictate where they should enqueue
//and you are saying: yes, in_footer
wp_register_script( 'cycle', 'http://malsup.github.com/jquery.cycle2.js', array(), '', true);
wp_register_script( 'site', get_template_directory_uri().'/js/site.js', array('jquery'), '', true); //we are setting a dependency - yes depend on jQuery - means load jQuery first
//actually rendering the scripts
wp_enqueue_script( 'cycle' );
wp_enqueue_script( 'site' );
}
add_action( 'wp_enqueue_scripts', 'themeslug_load_scripts' );
or you can simply try:
<?php
function themeslug_load_scripts() {
//actually rendering the scripts
wp_enqueue_script( 'cycle', 'http://malsup.github.com/jquery.cycle2.js', array(), '', true );
wp_enqueue_script( 'site', get_template_directory_uri().'/js/site.js', array('jquery'), '', true );
}
add_action( 'wp_enqueue_scripts', 'themeslug_load_scripts' );
And, clear your browser cache when working with JavaScripts. Load the page and see page source. It'll do the thing for you. :)
Reference:
wp_register_script()
wp_enqueue_script()
if you are trying to add js to a page or post, may I suggest a simple and free plugin? it is called CTJ. you put code blocks in it and add to header or footer as you like.
I'm developing a Wordpress theme and can't get JavaScript to run. When I view the source code the scripts have loaded and the file paths are correct.
Here is the functions.php code that loads the scripts:
function theme_js() {
// Conditional scripts
global $wp_scripts;
wp_register_script( 'html5_shiv', 'https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js',
'', '', false );
wp_register_script( 'respond_js', 'https://oss.maxcdn.com/respond/1.4.2/respond.min.js',
'', '', false );
$wp_scripts->add_data( 'html5_shiv', 'conditional', 'lt IE 9');
$wp_scripts->add_data( 'respond_js', 'conditional', 'lt IE 9');
// Footer scripts
wp_enqueue_script( 'bootstrap_js', get_template_directory_uri() . '/bootstrap/js/bootstrap.min.js',
array('jquery'), null, true );
wp_enqueue_script( 'color_js', get_template_directory_uri() . '/bootstrap/js/jquery.color.js',
array('jquery'), null, true );
wp_enqueue_script( 'header_js', get_template_directory_uri() . '/bootstrap/js/header.js',
array('jquery'), null, true );
wp_enqueue_script( 'rollover_js', get_template_directory_uri() . '/bootstrap/js/rollover.js',
array('jquery'), null, true );
}
add_action ( 'wp_enqueue_scripts', 'theme_js' );
Can anybody tell me why they won't run?
For reference - header.js:
$(window).scroll(function () {
//After scrolling 300px from the top...
if ($(window).scrollTop() >= 300) {
$(".navbar").stop().animate({backgroundColor: "#1e3b4e"}, 600);
$(".navbar-nav > li > a").css("color", "#ffffff");
$("#logo").attr("src", "img/logo.png");
//Otherwise remove inline styles and thereby revert to original stying
} else {
$(".navbar").stop().animate({backgroundColor: "rgb(255, 255, 255, 1)"}, 400);
$(".navbar-nav > li > a").css("color", "#1e3b4e");
$("#logo").attr("src", "img/newlogo.png");
}
});
Said website: http://sebastiangraz.com/projects/kbt
I wanted to add a script (FitScroll) to my wordpress template: http://codepen.io/ozgursagiroglu/full/tdpDr
This is how I enqueued it:
function kbt_scripts() {
wp_enqueue_style( 'kbt-style', get_stylesheet_uri() );
wp_enqueue_script( 'kbt-fitscroll', get_template_directory_uri() . '/js/fitscroll.js', array('jquery'), '20120206', true );
wp_enqueue_script( 'kbt-js', get_template_directory_uri() . '/js/js.js', array('jquery'), '20120206', true );
wp_enqueue_script( 'kbt-validate', get_template_directory_uri() . '/js/jquery.validate.min.js', array('jquery'), '20120206', true );
wp_enqueue_script( 'kbt-script', get_template_directory_uri() . '/js/script.js', array('jquery'), '20120206', true );
wp_enqueue_script( 'kbt-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );
wp_enqueue_script( 'kbt-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
I called the script with a noconflict wrapper
jQuery(document).ready(function ($) {
$('body').FixedScroll({
elements: '.module', // required
delay: 500 // millisecond, optional
});
});
I made sure I don't load jQuery twice. I've checked everywhere.
I still get this message
Uncaught TypeError: Object [object Object] has no method 'FixedScroll'
Any ideas? Thank you
Its FitScroll not FixedScroll try this,
jQuery(document).ready(function ($) {
$('body').FitScroll({ // FitScroll not FixedScroll
elements: '.module', // required
delay: 500 // millisecond, optional
});
});
jQuery(document).ready(function ($) {
$('body').FitScroll({
elements: '.module', // required
delay: 500 // millisecond, optional
});
});
You need to use Fitscroll instead of FixedScroll:
jQuery(document).ready(function ($) {
$('body').FitScroll({
elements: '.module', // required
delay: 500 // millisecond, optional
});
});
Next time, if you confused whether the name is correct or not, just digging directly into your plugin which is in your case located at the URL: http://sebastiangraz.com/projects/kbt/wp-content/themes/kbt/js/fitscroll.js?ver=20120206
And from this you can figure out the correct name of the function which is this:
$.fn.FitScroll = function(opt){
// Plugin code here
}