I’m confused as to what I should do regarding jQuery and WordPress (and also Bootstrap, kind of). I read this article on why I shouldn’t be loading my own jQuery because WordPress already has it and loading another can cause a mess of problems. But here’s a real example. I had to load jQuery version 2.1.0 in order to use my function below (it wasn’t working otherwise; it actually is only half working because it’s supposed to be a file input where you can either click to choose a file or drag and drop a file to upload, but the clicking to add a file doesn’t bring up the image preview). I had to use this version because the version that comes with WordPress wasn’t doing the job. Is that enough of a reason to justify? Or is there a way to make my function work without having to reference v. 2.1.0? Is there any harm in loading another version of jQuery nowadays? The article was written in 2013.
In addition, I am using Bootstrap to make a child theme and will be making a plugin as well. Is there any conflict with using Bootstrap to make a child theme and plugin? My rookie opinion is to make everything work with what is already given in WordPress, but I’d like to hear some more experienced people.
functions.php
<?php
function my_scripts() {
wp_register_script('jquery1', '//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js', false, null);
wp_enqueue_script('jquery1');
wp_enqueue_script( 'bootstrap-js', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array('jquery'), '3.3.5', true );
wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/js/custom-js.js');
}
add_action('wp_enqueue_scripts', 'my_scripts');
function my_styles() {
wp_enqueue_style( 'bootstrap-css', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css' );
wp_enqueue_style( 'my-style', get_stylesheet_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'my_styles');
?>
JS
$(window).load(function(){
$('#drop').click(function(){
console.log('click');
$('#fileBox').trigger('click');
});
//Remove item
$('.fileCont span').click(function(){
$(this).remove();
});
});
if (window.FileReader) {
var drop;
addEventHandler(window, 'load', function () {
var status = document.getElementById('status');
drop = document.getElementById('drop');
var list = document.getElementById('list');
function cancel(e) {
if (e.preventDefault) {
e.preventDefault();
}
return false;
}
// Tells the browser that we *can* drop on this target
addEventHandler(drop, 'dragover', cancel);
addEventHandler(drop, 'dragenter', cancel);
addEventHandler(drop, 'drop', function (e) {
e = e || window.event; // get window.event if e argument missing (in IE)
if (e.preventDefault) {
e.preventDefault();
} // stops the browser from redirecting off to the image.
var dt = e.dataTransfer;
var files = dt.files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
var reader = new FileReader();
//attach event handlers here...
reader.readAsDataURL(file);
addEventHandler(reader, 'loadend', function (e, file) {
var bin = this.result;
var fileCont = document.createElement('div');
fileCont.className = "fileCont";
list.appendChild(fileCont);
var fileNumber = list.getElementsByTagName('img').length + 1;
status.innerHTML = fileNumber < files.length ? 'Loaded 100% of file ' + fileNumber + ' of ' + files.length + '...' : 'Done loading. processed ' + fileNumber + ' files.';
var img = document.createElement("img");
img.file = file;
img.src = bin;
img.className = "thumb";
fileCont.appendChild(img);
var newFile = document.createElement('div');
newFile.innerHTML = file.name;
newFile.className = "fileName";
fileCont.appendChild(newFile);
var fileSize = document.createElement('div');
fileSize.className = "fileSize";
fileSize.innerHTML = Math.round(file.size/1024) + ' KB';
fileCont.appendChild(fileSize);
var progress = document.createElement('div');
progress.className = "progress";
progress.innerHTML = '<div aria-valuemax="100" aria-valuemin="0" aria-valuenow="100" class="progress-bar progress-bar-success" role="progressbar" style="width: 100%"></div>';
fileCont.appendChild(progress);
var remove = document.createElement('div');
remove.className = "remove";
remove.innerHTML = '<span class="glyphicon glyphicon-remove"></div>';
list.appendChild(remove);
}.bindToEventHandler(file));
}
return false;
});
Function.prototype.bindToEventHandler = function bindToEventHandler() {
var handler = this;
var boundParameters = Array.prototype.slice.call(arguments);
//create closure
return function (e) {
e = e || window.event; // get window.event if e argument missing (in IE)
boundParameters.unshift(e);
handler.apply(this, boundParameters);
}
};
});
} else {
document.getElementById('status').innerHTML = 'Your browser does not support the HTML5 FileReader.';
}
function addEventHandler(obj, evt, handler) {
if (obj.addEventListener) {
// W3C method
obj.addEventListener(evt, handler, false);
} else if (obj.attachEvent) {
// IE method.
obj.attachEvent('on' + evt, handler);
} else {
// Old school method.
obj['on' + evt] = handler;
}
}
//Not plugged yet
var bar = $('.progress-bar');
$(function(){
$(bar).each(function(){
bar_width = $(this).attr('aria-valuenow');
$(this).width(bar_width + '%');
});
});
You probably had to add your own version because the existing version, per WordPress best practices , is using
noConflict()
which removes the$
alias.Since you are using
$
you would have gotten errors saying$ is undefined
.Adding a new version can have serious implications with any plugins that are assigned to the initial version and you should only need jQuery.js loaded once.
To use in a
noConflict()
environment just wrap all of your code in: