How to enqueue javascript to wordpress child theme - javascript

I seriously need some help before I loose my mind.
So I want to implement hamburger menu to my wordpress child theme. This one looks nice to me. https://github.com/joncoop/hamburger-menu
I downloaded the zip file, unpacked to to my computer tried it out, works like a charm.
I uploaded the files to my child theme, I did everything by the book.
Through my functions.php file I enqueued the styles and scripts appropriately.
This is the code I used:
function NovelLite_child_add_stylesheet() {
if (!is_admin()) {
wp_enqueue_style('hamburger', get_stylesheet_directory_uri() . "/css/main.css", '', '', 'all');
}
}
add_action('wp_enqueue_scripts', 'NovelLite_child_add_stylesheet');
function NovelLite_child_wp_enqueue_scripts() {
wp_enqueue_script('hamburger', get_stylesheet_directory_uri() . "/js/hamburger-menu.js");
wp_enqueue_script('neki', get_stylesheet_directory_uri() . "/js/jquery-2.1.4.min.js");
}
add_action('wp_enqueue_scripts', 'NovelLite_child_wp_enqueue_scripts');
But it's just not working. main.css stylesheet is working fine, menu looks ok, it gets the hamburger button on mobile device, but when clicked, it doesn't show the menu. This tells me that javascript is not working as it is supposed to. So I'm wondering what could be the problem. I spent 3 days figuring it out and I am stuck.
P.S. Page source shows that scripts are enqueued!
Please help
Thanks in advance

Maybe the problem is in jQuery - your menu needs for it, but you're include it after hamburger js. Try this :
wp_enqueue_script('hamburger', get_stylesheet_directory_uri() . "/js/hamburger-menu.js", array('jquery'));
to tell WP to load WP embeded jquery before hamburger.js and to use it for 'hamburger.js`.
Also if this works, no need to load external jquery here:
wp_enqueue_script('neki', get_stylesheet_directory_uri() . "/js/jquery-2.1.4.min.js");
But if you want to use your jquery file must change hamburger enqueue script to use your jquery file like this:
wp_enqueue_script('hamburger', get_stylesheet_directory_uri() . "/js/hamburger-menu.js", array('neki'));

Related

Insert a javascript background in wordpress theme

I want to put a javascript file as my background on my wordpress website. It is an animated background that is continuously runing (it's the matrix rain). I don't know where should i start to insert the code, i have been looking on internet but there is no tutorial about inserting an animated background on a wordpress theme.
Could you help me please? I think a lot of people are in my situation.
Thanks :)
I take this matrix-rain library as an example https://sigstart.github.io/matrix-rain/.
First you have to save the file https://raw.githubusercontent.com/sigstart/matrix-rain/master/matrix.js as matrix-rain.js in the js folder of the theme.
Then you have to include the JavaScript file.
In functions.php
// Import moment.js
wp_enqueue_script('moment-min-js', 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js', array(), '1.0.0', false);
// Import matrix-rain
wp_enqueue_script('matrix-rain', get_template_directory_uri() . '/js/matrix-rain.js', array(), '1.0.0', false);
Then in the template where you want to show the background, for example single-post.php put this:
<canvas id="matrix"></canvas>

Wordpress updating Script n Styles Extension Script does not work

Thanks in advance for any help provided guys.
I have a wordpress Script n styles extension on my wordpress, the previous developer added it.
However I noticed that when I update my script in "Script n Styles", that does not change anything.
Without cache, with cache, refreshing as many times as you want, switching navigators it does not update the script stays the same.
Just take a look:
Script n Styles extension's script here
Script displayed on the page whatever I do in script n style extension
Do you have any ideas where that could come from ? I can send the link of that website if you want! Its afh.asso.fr (the home page only uses that bit of code (its for the "mon afh" little red button on the home page)
Thanks in advance for your help guys, it is really appreciated ! :)
add_action('wp_enqueue_scripts', 'theme_enqueue_styles');
function theme_enqueue_styles() {
// Get the theme data
$the_theme = wp_get_theme();
// Add custom style here in custom.css
wp_enqueue_style('custom', get_stylesheet_directory_uri() . '/css/custom.css', array(), $the_theme->get('Version'));
wp_enqueue_script('jquery');
// add custom javascript here in custom.js
wp_enqueue_script('main', get_stylesheet_directory_uri() . '/js/custom.js', array(), $the_theme->get('Version'), true);
}
Can find more information here:
https://developer.wordpress.org/reference/functions/wp_enqueue_script/

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.

Wordpress WooCommerce Product Carousel doesn't show pictures

First of all, I'm not a coding wonder and a starter.
I've a problem that I'm trying to solve for a couple of days. It's about the carousel on WooCommerce. The website is tegelsenlaminaat.nl and an example is: http://www.tegelsenlaminaat.nl/product/hoomline-balmoral-xl-spa/
I get the following error: TypeError: v.carouFredSel is not a function. (In 'v.carouFredSel(w)', 'v.carouFredSel' is undefined)
How can I solve this and what's the problem.
That error means that you're making a call to the carouFredSel function before that function has been registered.
Basically you're trying to call that function before that function exists. It would appear the JavaScript file that creates that function isn't being output to your site.
If you're implementing that directly into your theme you need to make sure you're including the script that registers the function before your script.min.js script.
The github for that plugin has a script titled jquery.carouFredSel-6.2.1-packed.js. You could include that with your enqueued scripts, typically in your functions.php.
That would probably look something like this:
wp_enqueue_script( 'caroufredsel', get_template_directory_uri() . '/assets/js/jquery.carouFredSel-6.2.1-packed.js', array ( 'jquery' ), 6.2.1, true);
wp_enqueue_script( 'script', get_template_directory_uri() . '/assets/js/script.min.js', array ( 'caroufredsel' ), 1.0, true);
That would enqueue both the caroufredsel script if included in your theme's assets/js directory and caroufredsel is dependent on jQuery and your min scripts is dependent on both jQuery and caroufredsel.
For more on enqueing scripts check out this article from my site covering the topic.

How to load a custom jquery mobile locally

I've been trying to add a jquery mobile module to my wordpress website - the collapsible content block module. It works great when I enqueue jquery mobile via CDN in functions, ie.
function get_jqm() {
wp_enqueue_script(
'jqm_js',
'http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js',
array('jquery'),
'1.2.0'
);
..but that adds the entire library, and it breaks my links and navigations, and the wordpress admin bar. I've downloaded a custom version of jquery mobile that really only includes the collapsible content block module, but I'm wondering what is the best way to enqueue a local version of jquery mobile plus custom jquery mobile CSS files. I've tried a number of suggestions by googling it but cannot find anything that gets it to work. It only works via CDN.
To register your script, use:
wp_register_script('jqm_js', get_template_directory_uri() . '/path/to/your/local/jquery.js', array(), null, false);
Set the last parameter as true to put the script in the footer and false to put it in the header of your site. 'get_template_directory_uri() returns something like
http://yourlocalsite:8888/wp-content/themes/your-theme/
and then from there you just need to provide the rest of the path to your js.
Then queue it up with:
wp_enqueue_script('jqm_js');
Similarly, you can do this with your CSS:
wp_enqueue_style('app-css', get_template_directory_uri() . '/path/to/your/app-css.css', false, '');

Categories

Resources