Snippet

Assign Different Language Versions to WordPress Pages and Posts

How to create multilingual wordpress siteWordpress translate posts into different languagesAdd multiple languages to wordpress pagesWordpress multilingual plugin setupAssign different languages to wordpress postsWordpress language switcher setupTranslate wordpress custom post typesWordpress multilingual support guideSet up multilingual wordpress blogWordpress site in multiple languages

Explanation

To make your WordPress site multilingual, this code helps you assign different languages to your posts and pages. Here's how it works:

Custom Language Categories:

  • The code creates a new category called "Languages" that you can use to label your posts and pages with different languages.
  • This is done by registering a custom taxonomy, which is just a fancy way of saying a new way to categorize your content.

Language Switcher:

  • A language switcher is added to your site, allowing visitors to choose which language they want to view content in.
  • This switcher is displayed as a list of links, each representing a different language.

Shortcode for Language Switcher:

  • You can easily add the language switcher anywhere on your site using the shortcode [language_switcher].
  • Just paste this shortcode into any post, page, or widget area where you want the switcher to appear.

Filtering Content by Language:

  • The code also includes a feature to filter posts based on the selected language.
  • When a user selects a language, only posts and pages tagged with that language will be shown.

This setup allows you to manage and display content in multiple languages, making your site accessible to a broader audience. Just remember, you'll need to manually translate your content and assign the appropriate language tags to each post or page.

Code

1<?php 2// Function to register a custom taxonomy for languages 3function wp_dudecom_register_language_taxonomy() { 4 $labels = array( 5 'name' => 'Languages', 6 'singular_name' => 'Language', 7 'search_items' => 'Search Languages', 8 'all_items' => 'All Languages', 9 'edit_item' => 'Edit Language', 10 'update_item' => 'Update Language', 11 'add_new_item' => 'Add New Language', 12 'new_item_name' => 'New Language Name', 13 'menu_name' => 'Languages', 14 ); 15 16 $args = array( 17 'hierarchical' => true, 18 'labels' => $labels, 19 'show_ui' => true, 20 'show_admin_column' => true, 21 'query_var' => true, 22 'rewrite' => array('slug' => 'language'), 23 ); 24 25 register_taxonomy('language', array('post', 'page', 'custom_post_type'), $args); 26} 27add_action('init', 'wp_dudecom_register_language_taxonomy'); 28 29// Function to add a language switcher to the site 30function wp_dudecom_language_switcher() { 31 $languages = get_terms(array( 32 'taxonomy' => 'language', 33 'hide_empty' => false, 34 )); 35 36 if (!empty($languages) && !is_wp_error($languages)) { 37 echo '<ul class="language-switcher">'; 38 foreach ($languages as $language) { 39 echo '<li><a href="' . esc_url(get_term_link($language)) . '">' . esc_html($language->name) . '</a></li>'; 40 } 41 echo '</ul>'; 42 } 43} 44 45// Shortcode to display the language switcher 46function wp_dudecom_language_switcher_shortcode() { 47 ob_start(); 48 wp_dudecom_language_switcher(); 49 return ob_get_clean(); 50} 51add_shortcode('language_switcher', 'wp_dudecom_language_switcher_shortcode'); 52 53// Function to filter posts by selected language 54function wp_dudecom_filter_posts_by_language($query) { 55 if (!is_admin() && $query->is_main_query() && (is_post_type_archive() || is_tax('language'))) { 56 if (isset($_GET['language']) && !empty($_GET['language'])) { 57 $query->set('tax_query', array( 58 array( 59 'taxonomy' => 'language', 60 'field' => 'slug', 61 'terms' => sanitize_text_field($_GET['language']), 62 ), 63 )); 64 } 65 } 66} 67add_action('pre_get_posts', 'wp_dudecom_filter_posts_by_language'); 68?>

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure you have access to your WordPress theme files or the ability to create a custom plugin.
  • Basic understanding of WordPress admin panel navigation.

Implementation Steps:

  1. Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation.
  2. Edit the functions.php File: Navigate to wp-content/themes/your-theme-name/functions.php and open it for editing. Alternatively, create a new custom plugin file in wp-content/plugins/ and open it for editing.
  3. Paste the Code: Copy the provided code and paste it at the end of your functions.php file or in your custom plugin file.
  4. Save Changes: Save the changes to the file and close the editor.
  5. Verify Taxonomy Registration: Log in to your WordPress admin panel, go to Posts or Pages, and check if a new "Languages" taxonomy is available for categorizing content.
  6. Add Language Terms: Navigate to Posts > Languages and add the languages you want to use on your site.
  7. Assign Languages to Content: Edit your posts and pages, and assign the appropriate language from the "Languages" taxonomy.
  8. Use the Language Switcher: Add the shortcode [language_switcher] to any post, page, or widget area where you want the language switcher to appear.
  9. Test Language Filtering: Visit your site and use the language switcher to ensure content is filtered correctly based on the selected language.

For further assistance or advanced functionality, consider reaching out to the experts at wp-dude.com for professional WordPress support.