Linking JS/jQuery to WordPress functions.php - javascript

Following a tutorial that recommends the following code to link JS/jQuery to WordPress, within functions.php:
<?php
function wpbootstrap_scripts_with_jquery()
{
// Register the script like this for a theme:
wp_register_script( 'custom-script', get_template_directory_uri() . '/bootstrap/bootstrap.min.js', array( 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js' ) );
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery' );
?>
But something is wrong, either the jQuery or the bootstrap.min.js did not link properly, possibly both. How can I ensure that both are linked?

Related

jQuery(...). is not a function in Wordpress Custom Templates

I'm using the AnimatedModal JS library to allow users to login on my Wordpress site.
In my functions.php I have enqueued as follows:
function smallium__enqueue_scripts() {
//lets put in the animated modal
wp_enqueue_script( 'medium-modal', get_template_directory_uri() . '/js/animatedModal.min.js', array( 'jquery') );
wp_enqueue_style('animated-modal', '//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.min.css');
wp_enqueue_script( 'medium-js', get_template_directory_uri() . '/js/medium-front.js', array( 'jquery','medium-modal' ) );
}
add_action('wp_enqueue_scripts', 'smallium__enqueue_scripts');
In the medium-front.js I call the animatedModal() function.
jQuery(document).ready(function($) {
jQuery("#login-anchor").animatedModal();
}
This works fine on most pages except for Custom Wordpress templates I get the error
Uncaught TypeError: jQuery(...).animatedModal is not a function
In the source code on all pages, the library is correctly encoded before I make the function call, so I'm not sure why only on custom templates this doesn't work.
I've also created an empty template file with only the header and footer included, to see if any code could cause the error, to no avail.
<?php
/* Template name: Write */
get_header(); ?>
<?php get_footer(); ?>
Your local animatedModal.min.js should be loaded before </body> instead of in the <head>, as documented here:
function smallium__enqueue_scripts() {
//lets put in the animated modal
wp_enqueue_style('animated-modal', '//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.min.css');
wp_enqueue_script( 'medium-js', get_template_directory_uri() . '/js/medium-front.js', array( 'jquery','medium-modal' ) );
wp_enqueue_script( 'medium-modal', get_template_directory_uri() . '/js/animatedModal.min.js', array( 'jquery'),false, true );
}
add_action('wp_enqueue_scripts', 'smallium__enqueue_scripts');

Running Javascript in wordpress

I'm creating a word press theme , this includes a responsive jquery menu.
However, for some reason, I can't get the menu to work. In my functions.PHP file I have included the script below. Please may someone help with advice and a code snippet, i'm trying to:
1) Call Jquery
2) Run the script slimmenu.js script
<?php
function menu_function() {
wp_enqueue_script( 'myscript', get_template_directory_uri() . '/js/jquery.slimmenu.js', array( 'jquery'));
}
add_action('wp_enqueue_scripts','menu_function');
?>
Many thanks,
P
You have not enqueued jquery which is a dependancy for the script you've enqueued. Also consider naming your file something a bit more unique to avoid conflicts. try...
function menu_scripts() {
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'slimMenu', get_template_directory_uri() . '/js/jquery.slimmenu.js', 'jquery' );
}
add_action('wp_enqueue_scripts','menu_scripts');
Better add the script in footer.php before :
<?php
wp_footer();
wp_enqueue_script( 'myscript', get_template_directory_uri() . '/js/jquery.slimmenu.js', 'jquery', true);
?>

jQuery is not defined in Wordpress, but my script is enqueued properly

I am trying to load a separate javascript file mobile-menu.js to my Wordpress theme. When I look at the console, it says, "jQuery is not defined." However, I know that I enqueued my script files correctly. Any ideas?
HTML file:
<!--this line wasn't here originally-->
<div id="switchmenu"><!--switchmenu begin-->
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
</div><!--switchmenu end-->
functions.php file:
function lapetitefrog_scripts() {
wp_enqueue_style( 'lapetitefrog-style', get_stylesheet_uri() );
wp_enqueue_script( 'lapetitefrog-mobile-menu', get_template_directory_uri() . '/js/mobile-menu.js', array(), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'lapetitefrog_scripts' );
mobile-menu.js file:
jQuery(document).ready(function($) {
$('#menu-icon').click(function() {
$('#switchmenu').slideToggle("fast");
});
});
Add wp_enqueue_script('jquery'); before you enqueue your scripts.
First Make sure that jquery file is include in the header, and your file requied jQuery
wp_enqueue_script(
'lapetitefrog-mobile-menu',
get_template_directory_uri() . '/js/mobile-menu.js',
array('jquery'),
'1.0',
true
);
Second you should start your javascript file like:
(function($) {
$(document).ready(function() {
.......
});
})(jQuery);
OR
// Use jQuery in place of $
jQuery(document).ready(function() {
.....
});
You can try:
enqueue Jquery first.
wp_enqueue_script('jquery');
and then enqueuing the latter script with JQuery dependency in 3rd argument i.e array('jquery') that's what mostly people forget.
wp_enqueue_script( 'lapetitefrog-mobile-menu', get_template_directory_uri() . '/js/mobile-menu.js', array('jquery'), '1.0', true );
Wordpress ships with jQuery by default
// Adds jQuery
add_action('init', 'childoftwentyeleven_down');
function childoftwentyeleven_down() {
// register your script location, dependencies and version
wp_register_script('down',
get_stylesheet_directory_uri() . '/js/down.js',
array('jquery') );
// enqueue the script
wp_enqueue_script('down');
}
The error jQuery is not defined might also appear with jQuery code embedded in your template parts.
This happens when jQuery is added to the footer of the page, while the embedded script tries to execute in the middle of the page.
If this is the case, you need to load jQuery in the header with wp_enqueue_script().
So the last parameter needs to be false here:
wp_enqueue_script( 'my-jquery', get_template_directory_uri() . '/js/jquery.js', array(), time(), false );
Now the code executes in the right order and jQuery will be defined.
Another solution would be to move the embedded script to a Javascript file that will also be added with wp_enqueue_script() to the footer of the page. This is up to you.

WP - difficulty with enqueueing scripts

I am trying to add javascript files with wp_enqueue_script. The scripts do not seem to load at all in the page. What I have is::
function uberpasscode_scripts() {
wp_enqueue_script( 'bootstrap', get_template_directory_uri() . "/assets/js/bootstrap.min", array('jquery'));
wp_enqueue_script( 'jquery', get_template_directory_uri() . "/assets/js/jquery-1.11.1.min" );
}
add_action( 'wp_enqueue_scripts', 'uberpasscode_scripts' );
This is because you are missing the javascript (js) extension after .min.
function uberpasscode_scripts() {
wp_enqueue_script( 'bootstrap', get_template_directory_uri() . "/assets/js/bootstrap.min.js", array('jquery'),'', true );
wp_enqueue_script('jquery');
}
add_action( 'wp_enqueue_scripts', 'uberpasscode_scripts' );
Try adding those two parameters. Also, that version of jQuery you are trying to load is too old for bootstrap. Just use the jQuery built into WordPress. Also, make sure that you are putting this into functions.php
If this is still not working, please make sure you have the <?php wp_footer(); ?> function in your footer.php file.
<?php wp_footer(); ?>
</body>
</html>

How to load bootstrap script and style in functions.php (wordpress)?

I converted bootstrap theme to wordpress. Now, I have a problem in loading the bootstrap scripts and style.
This is my code in functions.php
function wpbootstrap_scripts_with_jquery()
{
// Register the script like this for a theme:
wp_register_script( 'custom-script', get_template_directory_uri() . '/js/bootstrap.js', array( 'jquery' ) );
wp_enqueue_script( 'samplejs', get_template_directory_uri() . '/js/jquery.min.js', array( '' ) );
wp_enqueue_script( 'samplejs', get_template_directory_uri() . '/js/clean-blog.min.js', array( '' ) );
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery' );
and i add this in my header
<?php wp_enqueue_script("jquery"); ?>
Additional question:
What is the difference between wp_register and wp_enqueue?
Please help me. Im beginner in using bootstrap.
All you need to do is enqueue them. Also, I recommend getting rid of your jquery import (the second wp_enqueue). WordPress includes jQuery by default, and you're already including it in the first script (because you have listed it as a dependency). Here's an example, but this enqueues jquery twice (the native jquery, and the bootstrap jquery):
function wpbootstrap_scripts_with_jquery()
{
// Register the script like this for a theme:
wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/js/bootstrap.js', array( 'jquery' ) );
wp_enqueue_script( 'bootstrap-jquery', get_stylesheet_directory_uri() . '/js/jquery.min.js' );
wp_enqueue_script( 'blog-scripts', get_stylesheet_directory_uri() . '/js/clean-blog.min.js' );
}
add_action( 'wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery' );
The only reason you'd need to register the scripts before enqueue-ing them, is if one of the scripts is a dependency. From the docs:
wp_register_script() registers a script file in WordPress to be linked
to a page later using the wp_enqueue_script() function, which safely
handles the script dependencies.
Scripts that have been pre-registered using wp_register_script() do
not need to be manually enqueued using wp_enqueue_script() if they are
listed as a dependency of another script that is enqueued. WordPress
will automatically include the registered script before it includes
the enqueued script that lists the registered script's handle as a
dependency.
you cas use "wp_enqueue_style" and "wp_enqueue_script".
for example:
function reg_scripts() {
wp_enqueue_style( 'bootstrapstyle', get_template_directory_uri() . '/css/bootstrap.min.css' );
wp_enqueue_style( 'bootstrapthemestyle', get_template_directory_uri() . '/css/bootstrap-theme.min.css' );
wp_enqueue_script( 'bootstrap-script', get_template_directory_uri() . '/js/bootstrap.min.js', array(), true );
}
add_action('wp_enqueue_scripts', 'reg_scripts');

Categories