Disable Automatic Translation of WordPress Theme Elements
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_headaction to add a bit of CSS that tells browsers not to translate anything with thenotranslateclass. - Content: It uses the
the_contentfilter to automatically add thenotranslateclass to all paragraph tags (<p>) in your posts and pages. - Menus: The
wp_nav_menu_itemsfilter is used to add thenotranslateclass to all menu items, ensuring your navigation stays as you intended. - Widgets: By using the
widget_textfilter, the code wraps all widget text in adivwith thenotranslateclass, 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
Instructions
File Location: Add the code to your theme's functions.php file.
Prerequisites: None required.
Implementation Steps:
- Access your WordPress admin dashboard.
- Navigate to Appearance > Theme Editor. If you see a warning about editing files directly, proceed with caution.
- In the right sidebar, locate and click on
functions.phpto open it for editing. - Scroll to the bottom of the
functions.phpfile and paste the provided code snippet. - Click the Update File button to save your changes.
- 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.