How to add Google translate to wordpress without plugin?
If you want to add Google translate functionality to your wordpress site without using a plugin, here are the steps:
header.php
<ul class="top-menu translation-links">
<li><a href="#" class="English language" data-lang="English">ENGLISH</a></li>
<li><a href="#" class="Spanish" data-lang="Spanish">ESPAÑOL</a></li>
<li><a href="#" class="French" data-lang="French">FRANÇAIS</a></li>
<li><a href="#" class="Arabic" data-lang="Arabic">ARABIC</a></li>
</ul>
footer.php
<?php function icw_googleTranslate_js() { ?>
<!-- Code provided by Google -->
<div id="google_translate_element" class="hide"></div>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'en,es,fr,ar', layout: google.translate.TranslateElement.InlineLayout.SIMPLE, autoDisplay: false},'google_translate_element');
}
</script>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit" type="text/javascript"></script>
<!-- Flag click handler -->
<script type="text/javascript">
jQuery('.translation-links a').click(function() {
var lang = jQuery(this).data('lang');
var $frame = jQuery('.goog-te-menu-frame:first');
if (!$frame.size()) {
alert("Error: Could not find Google translate frame.");
return false;
}
$frame.contents().find('.goog-te-menu2-item span.text:contains('+lang+')').get(0).click();
jQuery('.translation-links a').removeClass('language');
jQuery(this).addClass('language');
return false;
});
jQuery(document).ready(function ($) {
$(window).load(function () {
setTimeout(function(){
$('#preloader').fadeOut('slow', function () {
});
},1500); // set the time here
});
});
jQuery(window).bind("load", function() {
setTimeout(function(){
language = jQuery("#google_translate_element .goog-te-menu-value > span").html();
jQuery('.translation-links a').removeClass('language');
jQuery('.translation-links a.'+language).addClass('language');
},2000); // set the time here
});
</script>
<?php
} add_action( 'wp_footer', 'icw_googleTranslate_js', 20, 1 ); ?>
.translation-links a.language {text-decoration:underline;}
Author
Jayesh Patel
Jayesh Patel is a Professional Web Developer & Designer and the Founder of InCreativeWeb.
As a highly Creative Web/Graphic/UI Designer - Front End / PHP / WordPress / Shopify Developer, with 14+ years of experience, he also provide complete solution from SEO to Digital Marketing. The passion he has for his work, his dedication, and ability to make quick, decisive decisions set him apart from the rest.
His first priority is to create a website with Complete SEO + Speed Up + WordPress Security Code of standards.