Assign Different Language Versions to WordPress Pages and Posts

How to create multilingual wordpress site; Wordpress translate posts into different languages; Add multiple languages to wordpress pages; Wordpress multilingual plugin setup; Assign different languages to wordpress posts; Wordpress language switcher setup; Translate wordpress custom post types; Wordpress multilingual support guide; Set up multilingual wordpress blog; Wordpress 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

<?php
// Function to register a custom taxonomy for languages
function wp_dudecom_register_language_taxonomy() {
    $labels = array(
        'name'              => 'Languages',
        'singular_name'     => 'Language',
        'search_items'      => 'Search Languages',
        'all_items'         => 'All Languages',
        'edit_item'         => 'Edit Language',
        'update_item'       => 'Update Language',
        'add_new_item'      => 'Add New Language',
        'new_item_name'     => 'New Language Name',
        'menu_name'         => 'Languages',
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'language'),
    );

    register_taxonomy('language', array('post', 'page', 'custom_post_type'), $args);
}
add_action('init', 'wp_dudecom_register_language_taxonomy');

// Function to add a language switcher to the site
function wp_dudecom_language_switcher() {
    $languages = get_terms(array(
        'taxonomy'   => 'language',
        'hide_empty' => false,
    ));

    if (!empty($languages) && !is_wp_error($languages)) {
        echo '<ul class="language-switcher">';
        foreach ($languages as $language) {
            echo '<li><a href="' . esc_url(get_term_link($language)) . '">' . esc_html($language->name) . '</a></li>';
        }
        echo '</ul>';
    }
}

// Shortcode to display the language switcher
function wp_dudecom_language_switcher_shortcode() {
    ob_start();
    wp_dudecom_language_switcher();
    return ob_get_clean();
}
add_shortcode('language_switcher', 'wp_dudecom_language_switcher_shortcode');

// Function to filter posts by selected language
function wp_dudecom_filter_posts_by_language($query) {
    if (!is_admin() && $query->is_main_query() && (is_post_type_archive() || is_tax('language'))) {
        if (isset($_GET['language']) && !empty($_GET['language'])) {
            $query->set('tax_query', array(
                array(
                    'taxonomy' => 'language',
                    'field'    => 'slug',
                    'terms'    => sanitize_text_field($_GET['language']),
                ),
            ));
        }
    }
}
add_action('pre_get_posts', 'wp_dudecom_filter_posts_by_language');
?>

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.