Twitter-Bootstrap-3 on Wordpress Jquery not loading - javascript

I am trying to hook up my custom theme created on Bootstrap 3 and installed onto the latest version of WordPress -working fine but there's an issue with it not being able to hook up to the Jquery library or my JS files.
I have usually been able to achieve this by doing:
<script src = "<?php echo get_template_directory_uri(); ?>/js/bootstrap.js"></script>
But it is just failing on me altogether.
So I have WordPress on the root of the Dir with my files sitting in the theme folder as expected. Short of ideas now :/
Side note: I have tried placing these into both header and footer files and also injecting the JavaScript straight into the HTML but nothing...

The recommended way to include both, jQuery and bootstrap.js would be a function in your functions.php.
function load_styles_and_scripts(){
wp_enqueue_script('jquery');
wp_enqueue_script('bootstrap-scripts', get_template_directory_uri().'/js/bootstrap.min.js');
}
and then calling it at the end of the file
add_action('wp_enqueue_scripts', 'load_styles_and_scripts');
this way, you load the jQuery file which already comes with wordpress. As the call to enqueuing bootstrap.js comes after jQuery, this should work. You can also add dependecies in the add_action-call. This allows you to make sure, that dependencies are loaded in the right order.
For further information, have a look at http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Related

How to integrate baguetteBox.js in the BST WordPress Starter theme?

I am trying to modify the starter WordPress theme BST for a personal project.
In it I want to have a custom page template with bootstrap elements. This page will show various images in the form of galleries.
I also need to import the baguetteBox.js plugin so that I can use its lightbox effect.
The pages have been created in html and they work. The custom page template is created and the design is the same as the html variant.
However when importing the scripts in WordPress (by using wp enque and wp register) the console cannot define baguetteBox. So I guess I am doing something wrong.
Here is the official guide on installing this Bootstrap plugin:
The enqueue code that I am using is placed in the bst-master/function/enqueues.php file:
For baguetteBox.min.js:
wp_register_script('baguetteBox-js', get_template_directory_uri() . '/js/baguetteBox.min.js', false, null, true, array('jquery'));
wp_enqueue_script('baguetteBox-js');
I have also tried without array('jquery') - no changes. I am also not sure where/how to place the "async" part (as recommended by the developers).
For baguetteBox.min.css:
wp_register_style('baguetteBox-css', get_template_directory_uri() . '/css/baguetteBox.min.css', false, null);
wp_enqueue_style('baguetteBox-css');
The problem is with the <script>baguetteBox.run('.tz-gallery', {filter: /.angelov.+\.(gif|jpe?g|png|webp)/i});</script> which I cannot figure out how and where to place so I just tried to add it inside the page template php file and also in the Insert Headers/Footers plugin.
So I figured out my own solution. First of all the classic (and recommended) way of adding scripts for WordPress was failing somehow. So I decided to place the script in the header.php file before the closing head tag and without async. I also moved the script in the js folder of the child theme.
It looks like this:
<script src="<?php echo get_stylesheet_directory_uri();?>/js/baguetteBox.min.js"> </script>
Next I placed the below code in the custom page template file right after the final closing <div> tag:
<script>baguetteBox.run('.tz-gallery', {filter:/.angelov.+\.(gif|jpe?g|png|webp)/i});</script>
And it all worked out like a charm.

Javascript for Responsive Menu in Wordpress

sorry if this is a basic question but I'm having trouble figuring out how to use a Javascript/jQuery script in my website. (If this is well documented and I just don't know the right terms, please share what to look for - I've had no luck yet online with what I know.)
I'd already designed a site with static pages, it just didn't have a blog. So I put Wordpress in for one of the pages, and figured out how to add in the HTML/CSS design for my existing site's menu using the header.php and footer.php files. However, my jQuery script that's supposed to load when the page is resized in order to change the menu design is not working.
I pasted the <script type="text/javascript" src="jquery.min.js"></script> code in the <head> part of the file, adjusted the body tag to <body onresize="myFunction()" <?php body_class(); ?>>, and added the "myFunction" script to the footer.php file, above the </body> tag. Above function myFunction() { I also have window.onload=myFunction(); which isn't working either - so no matter what size the page is loaded at or what size it's changed to the menu is unresponsive.
When I tried adding an enqueue code block to the functions.php file it broke the page entirely, giving a 500 error.
Any advice would be hugely appreciated!
Check that the path you specify to the jQuery library is correct. In your script tag src="jquery.min.js is a relative path. This means that the file jquery.min.js is in the same directory as your html/php/template according to your src path.
I would recommend using a CDN. You won't have a relative path, need to load your jquery files, and you'll benefit from browser caching if other pages/sites have loaded the same files.
3x
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
2x
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
When you add resources such as scrips and style sheets to WordPress you should enqueue them using the functions wp_enqueue_script() and wp_enqueue_style().
Doing this ensures that your scirpt/style is only included once, allows you to declare dependancies, and (in the case of scripts) allows you to add it to the footer should you require/desire.
You can find more information on these functions in the Codex -
wp_enqueue_script()
wp_enqueue_style()
In conjunction with these functions, you should use the wp_enqueue_scripts action, ensuring that your scripts styles get enqueued at the correct time.
So for example, you should place something similar to this in your functions.php file. Because jQuery already exists within WP, you don't even need to specify a location, you just tell WP that you want to enqueue it -
add_action( 'wp_enqueue_scripts', 'my_enqueue_jquery' );
function my_enqueue_jquery(){
wp_enqueue_script( 'jquery' );
}

Where should custom javascript be injected into a Wordpress theme?

I am helping a friend build a wordpress site using a purchased custom theme as his parent theme. There is also a child theme.
I've written some javascript to listen for clicks on specific links, and to change CSS in response to them. I can make this work in another environment, but we're trying to figure out how to get it to execute in Wordpress.
We have tried putting it in the index.php (most logical source to me, since that's to me that makes the most sense, but obviously WordPress works differently).
So all I am trying to figure out is, where in wordpress do you put the script tag (where you put code, not referencing jQuery we already have that)?
the index.php script will only load the WordPress framework and run it, you need to add your js code in the theme/template files.
a little quote from the official word press site:
JavaScript in Template Files
The safe and recommended method of adding
JavaScript to a WordPress generated page, and WordPress Theme or
Plugin, is by using wp_enqueue_script(). This function includes the
script if it hasn't already been included, and safely handles
dependencies.
you can read more here
In my opinion, the best is that you should create a js file with the code you want to run and upload to your theme/template folder, and then add it with a script tag as the link i've provided explains.
And my advice is to not embed the code directly in the template file but load it from a javascript file.
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/pathto/yourscript.js"></script>
You can do that in different ways:
Including the script in your footer for example
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/pathto/yourscript.js"></script>
Or using the wp_enqueue_script function, more info.

Wordpress enqueuing scripts in post

this is a noobish question but I really tried several options, none of them seemed to work. I have a plugin that is a slideshow with some posts, it works just fine on homepage (index). It uses jquery 1.4.2 and a javascript file. The idea is that when I go inside a post, the javascript file is not loaded, imported (inspecting the sources using chrome's webdev I can see the plugin's style.css but not the javascript file).
I tried several options, apparently the jquery used by wordpress is a bit too old and I need 1.4.2.
I tried registering the 1.4.2 jquery (I will refer as jq), and then enqueuing the script file depending on the new jq, in plugin's php file, also enqueuing the style.css. I also tried including the next code inside functions.php, still not working:
add_action('wp_footer', 'load_slideshow_scripts');
function load_slideshow_scripts() {
wp_register_script('slideshow-jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', array(), '1.4.2', false);
wp_enqueue_script('slideshow-js', plugins_url('contest-slideshow/script.js'), array('slideshow-jquery'), '1.0', false);
wp_enqueue_style('slideshow-css', plugins_url('contest-slideshow/styles.css'));
}
I also tried enqueuing the js files in a function that I hooked to init, still no success. I also tried simply enqueuing these files along with the other scripts loaded by default by the theme in functions.php.
The real problem is that the javascript file is not loaded and the slideshow is not working while in single post, otherwise it works
If it's really a jQuery related problem, you should deregister it first. Try out with this:
function modify_jquery() {
if (!is_admin()) {
// comment out the next two lines to load the local copy of jQuery
wp_deregister_script('jquery');
wp_register_script('jquery', '//code.jquery.com/jquery-1.11.0.min.js', false, '1.11.0');
wp_enqueue_script('jquery');
}
}
add_action('init', 'modify_jquery');
This piece of code has to be inside the functions.php of your theme. I used the last 1.x jQuery version but edit it if you need/prefer another one.
Good luck!

Wordpress, Jquery, wp_enqueue_script

I am in need of help!
Can someone PLEASE walk me through the specifics, from start to finish, on how to add a jquery plugin script to my wordpress site?
I'm new to Jquery and I have searched the web for the last week. I've tried it myself several times and its just not working. I've asked for help on other forums and I haven't received any answers. Is this something fairly simple to do?
Just to let you know, I am in need step by step instructions. It's just that when I ask questions about this on forums, people are giving me answers and assuming that I already know certain things...I am a beginner with jquery and I need help with each step of the process of adding a jquery script, from start to finish.
The script I want to add is here:
http://tympanus.net/codrops/2010/03/09/a-fresh-bottom-slide-out-menu-with-jquery/
I want to add it on my homepage in the theme I'm using.
1) How do I call the jquery (wp_enqueue_script)
2) Where do I place this (above)
3) CSS - do I simply append the css from the script to my style.css file?
4) How do I call the script to action on the wordpress page?
5) How do I incorporate the html?
These are some of the questions that I have. I've been searching all over the net and usually people assume that people know how to incorporate javascript libraries into their themes....well some of us beginners need help :-)
Any assistance is appreciated!
Thanks!!!
~T
The slide-out menu you linked to consists of images, a stylesheet, a minified version of the jQuery library and and index.html containing the javascript of the menu.
The jQuery included in the archive you downloaded is not neccessary, as jQuery is by default included in wordpress. If you want to add the newest minified jQuery to WP do it from the google libraries in your functions.php like so:
function jq_load_from_google() {
if (!is_admin()) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery',
'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js');
wp_enqueue_script( 'jquery' );
}
}
add_action('init', 'jq_load_from_google');
You could append the css to your existing stylesheet or (as in the following example) add it to the theme separately. Rename the stylesheet to menu.css (or the like), place the images and stylesheet in your themes /css and /images folders and add the following to your theme's header.php before wp_head() and after your main stylesheet:
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/menu.css" type="text/css" />
Add the javascript (the second script that contains code, not the jQuery line) included in the downloaded index.html to your theme's header.php after wp_head().
Change the all occurences #menu in the js to your wordpress menu's ul's ID.
See the WP docs for details and examples: http://codex.wordpress.org/Function_Reference/wp_enqueue_script
Use Firebug to look for errors and conflicts: http://getfirebug.com/

Categories

Resources