I’m using JQVMaps to render a map in a WordPress site. Testing the code outside of WordPress, everything runs perfectly. When I added it to WordPress I got this console error:
[Error] TypeError: ‘undefined’ is not a function (evaluating ‘jQuery(‘#vmap’).vectorMap’)
Here is the code:
header.php:
if (is_page(2)){ ?>
<link href="jqvmap.css" media="screen" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="<?php bloginfo('template_url'); ?>/js/jquery.vmap.js" type="text/javascript"></script>
<script src="<?php bloginfo('template_url'); ?>/js/jquery.vmap.world.js" type="text/javascript"></script>
<script src="<?php bloginfo('template_url'); ?>/js/jquery.vmap.un_regions.js" type="text/javascript"></script>
<script src="<?php bloginfo('template_url'); ?>/js/Chart.js" type="text/javascript"></script>
<?php }?>
<?php wp_head(); ?>
footer.php:
if (is_page(2)){ ?>
<script type="text/javascript">
// pie chart options
var pieOptions = {
segmentShowStroke : true,
animateScale : false
}
// get pie chart canvas
var pie= document.getElementById("pie").getContext("2d");
jQuery(document).ready(function() { //this is where the error is
jQuery('#vmap').vectorMap({
map: 'world_en',
backgroundColor: '#fff',
borderColor: '#bbb',
borderOpacity: 1,
borderWidth: .2,
color: '#bbb',
colors: colored_regions,
hoverOpacity: 0.8,
selectedColor: '#666666',
enableZoom: false,
showTooltip: false,
onRegionOver : function (element, code, region)
{
highlightRegionOfCountry(code);
},
onRegionOut : function (element, code, region)
{
unhighlightRegionOfCountry(code);
},
onRegionClick: function(element, code, region)
{
highlightRegionOfCountry(code);
$.ajax('/get_chart_data.php', {
data: {region: region},
dataType: 'json',
success: function(response) {
new Chart(pie).Doughnut(response.pieData, pieOptions);
}
});
}
});
});
</script>
<?php }?>
And I have #vmap and #pie in the content-page.php file. I’ve already tried several jQuery.noConflict(); solutions, including adding $ into the ready(function($) and adding the noConflict function right after my script tag. Could it still be an issue with how WordPress loads jQuery or is there another problem? You can find the site here. Thanks.
Search your code the wp_head(); and place your code after:
It’s better to add this as a plugin, so when changing themes, the needed changes are minimal or even zero.
And the correct way is to enqueue the styles/scripts, so as to avoid conflicts with other plugins. Also, jQuery should not be loaded from external sources, always use the bundled version that ships with WordPress, this avoids conflicts as well (a whole lot of them).
This is a simplified version of your code. I’ve removed files not available, and vars/functions referenced but not included in your sample code.
And the file
start.js
:And the page with ID #2 contains:
Related:
wp_localize_script()
(pass PHP vars to enqueued JS file)I think the problem lies in the path of your javascripts
Remember to upload those files to your current theme ..