Snippet

Disable Automatic Translation of WordPress Theme Elements

How to stop automatic translation on wordpressDisable translation for specific wordpress elementsPrevent google translate on wordpress siteExclude parts of wordpress site from translationStop automatic translation of wordpress theme elementsHow to prevent browser translation on wordpressWordpress disable translation for certain contentHow to exclude wordpress pages from translationPrevent automatic translation on wordpressWordpress stop google translate from activating

Explanation

If you want to stop certain parts of your WordPress site from being automatically translated by services like Google Translate, this code snippet is your friend. It adds a special class called notranslate to specific elements, telling translation services to skip them.

Here's how it works:

  • Custom CSS: The code hooks into the wp_head action to add a bit of CSS that tells browsers not to translate anything with the notranslate class.
  • Content: It uses the the_content filter to automatically add the notranslate class to all paragraph tags (<p>) in your posts and pages.
  • Menus: The wp_nav_menu_items filter is used to add the notranslate class to all menu items, ensuring your navigation stays as you intended.
  • Widgets: By using the widget_text filter, the code wraps all widget text in a div with the notranslate class, keeping your sidebar content untouched by translation.

This approach is handy if you have specific content that should remain in its original language, like brand names or technical terms. Just pop this code into your theme's functions.php file, and you're good to go!

Code

1<?php 2/** 3 * Prevent automatic translation for specific WordPress elements. 4 * This snippet adds a 'notranslate' class to specified elements to stop translation services like Google Translate. 5 */ 6 7// Hook into 'wp_head' to add custom CSS for disabling translation 8add_action('wp_head', 'wp_dudecom_add_no_translate_css'); 9 10function wp_dudecom_add_no_translate_css() { 11 echo '<style> 12 .notranslate { 13 translate: no; 14 } 15 </style>'; 16} 17 18// Add 'notranslate' class to specific elements using 'the_content' filter 19add_filter('the_content', 'wp_dudecom_add_no_translate_class_to_content'); 20 21function wp_dudecom_add_no_translate_class_to_content($content) { 22 // Example: Add 'notranslate' class to all <p> tags 23 $content = preg_replace('/<p>/', '<p class="notranslate">', $content); 24 return $content; 25} 26 27// Add 'notranslate' class to specific elements using 'wp_nav_menu_items' filter 28add_filter('wp_nav_menu_items', 'wp_dudecom_add_no_translate_class_to_menu_items', 10, 2); 29 30function wp_dudecom_add_no_translate_class_to_menu_items($items, $args) { 31 // Example: Add 'notranslate' class to all menu items 32 $items = preg_replace('/<li/', '<li class="notranslate"', $items); 33 return $items; 34} 35 36// Add 'notranslate' class to specific widgets using 'widget_text' filter 37add_filter('widget_text', 'wp_dudecom_add_no_translate_class_to_widgets'); 38 39function wp_dudecom_add_no_translate_class_to_widgets($text) { 40 // Example: Add 'notranslate' class to all widget text 41 $text = '<div class="notranslate">' . $text . '</div>'; 42 return $text; 43} 44?>

Instructions

File Location: Add the code to your theme's functions.php file.

Prerequisites: None required.

Implementation Steps:

  1. Access your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor. If you see a warning about editing files directly, proceed with caution.
  3. In the right sidebar, locate and click on functions.php to open it for editing.
  4. Scroll to the bottom of the functions.php file and paste the provided code snippet.
  5. Click the Update File button to save your changes.
  6. Visit your website to ensure that the specified elements are no longer automatically translated.

If you encounter any issues or need further assistance with this implementation or more advanced functionality, consider reaching out to wp-dude.com for expert help.