Customize Permalinks for Each Language in WordPress

How to customize permalinks for different languages in wordpress; Wordpress multilingual permalink structure; Create custom permalinks for each language wordpress; Wordpress permalinks for multiple languages; Set up language-specific permalinks in wordpress; Wordpress custom permalinks by language; How to translate permalinks in wordpress; Wordpress language code in permalinks; Multilingual wordpress permalinks setup; Wordpress permalinks for language versions;

Explanation

To customize permalinks for different languages in WordPress, this code helps you set up language-specific URLs for your posts and pages. Here's how it works:

  • Language in Permalinks: The code checks if a post has a language set using a custom field. If it does, it adds the language code to the URL, making it look like yourwebsite.com/en/post-name/.
  • Rewrite Rules: It creates rules so WordPress knows how to handle these new URLs. You can specify which languages you support by adding their codes (like 'en', 'fr', 'de') in the code.
  • Query Variables: The code adds a 'language' variable to WordPress's query system, allowing it to recognize and filter content based on the language in the URL.
  • Filtering Content: When WordPress fetches posts, it checks the language variable and only shows posts that match the language in the URL.
  • Permalink Flushing: The code ensures that WordPress updates its permalink structure when the plugin is activated or deactivated, so your new URLs work correctly.

This setup allows you to have neat, language-specific URLs for your multilingual site, improving both user experience and SEO.

Code

<?php
// Add custom permalinks for different languages in WordPress

function wp_dudecom_custom_language_permalinks( $permalink, $post, $leavename ) {
    // Check if the post has a language set
    $language = get_post_meta( $post->ID, 'language', true );

    if ( $language ) {
        // Append the language code to the permalink
        $permalink = home_url( '/' . $language . '/' . $post->post_name . '/' );
    }

    return $permalink;
}
add_filter( 'post_link', 'wp_dudecom_custom_language_permalinks', 10, 3 );
add_filter( 'page_link', 'wp_dudecom_custom_language_permalinks', 10, 3 );

// Add rewrite rules for language-specific permalinks
function wp_dudecom_add_language_rewrite_rules() {
    $languages = array( 'en', 'fr', 'de' ); // Add your supported languages here

    foreach ( $languages as $language ) {
        add_rewrite_rule(
            '^' . $language . '/([^/]+)/?$',
            'index.php?name=$matches[1]&language=' . $language,
            'top'
        );
    }
}
add_action( 'init', 'wp_dudecom_add_language_rewrite_rules' );

// Add query vars for language
function wp_dudecom_add_language_query_vars( $vars ) {
    $vars[] = 'language';
    return $vars;
}
add_filter( 'query_vars', 'wp_dudecom_add_language_query_vars' );

// Modify the main query to filter by language
function wp_dudecom_filter_query_by_language( $query ) {
    if ( ! is_admin() && $query->is_main_query() && $language = get_query_var( 'language' ) ) {
        $meta_query = array(
            array(
                'key'     => 'language',
                'value'   => $language,
                'compare' => '='
            )
        );
        $query->set( 'meta_query', $meta_query );
    }
}
add_action( 'pre_get_posts', 'wp_dudecom_filter_query_by_language' );

// Ensure permalinks are flushed on activation
function wp_dudecom_flush_rewrite_rules() {
    wp_dudecom_add_language_rewrite_rules();
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'wp_dudecom_flush_rewrite_rules' );

// Ensure permalinks are flushed on deactivation
function wp_dudecom_deactivate_plugin() {
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'wp_dudecom_deactivate_plugin' );
?>

Instructions

To implement the code for customizing permalinks for each language in WordPress, follow these steps:

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

Prerequisites:

  • Ensure you have a custom field named 'language' for each post/page to store the language code (e.g., 'en', 'fr', 'de').
  • Basic understanding of how to access and edit WordPress files.

Implementation Steps:

  1. Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation.
  2. Locate the File: Navigate to wp-content/themes/your-theme/functions.php or create a new plugin file in wp-content/plugins/.
  3. Add the Code: Copy and paste the provided code into the chosen file.
  4. Save Changes: Save the file and ensure there are no syntax errors.
  5. Flush Permalinks: Go to your WordPress dashboard, navigate to Settings > Permalinks, and click Save Changes to flush the rewrite rules.
  6. Test the Setup: Create or edit a post/page, set the 'language' custom field, and check if the URL reflects the language code (e.g., yourwebsite.com/en/post-name/).

If you need help with implementation or require more advanced functionality, consider using the services of wp-dude.com.