Snippet

Customize Permalinks for Each Language in WordPress

How to customize permalinks for different languages in wordpressWordpress multilingual permalink structureCreate custom permalinks for each language wordpressWordpress permalinks for multiple languagesSet up language-specific permalinks in wordpressWordpress custom permalinks by languageHow to translate permalinks in wordpressWordpress language code in permalinksMultilingual wordpress permalinks setupWordpress 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

1<?php 2// Add custom permalinks for different languages in WordPress 3 4function wp_dudecom_custom_language_permalinks( $permalink, $post, $leavename ) { 5 // Check if the post has a language set 6 $language = get_post_meta( $post->ID, 'language', true ); 7 8 if ( $language ) { 9 // Append the language code to the permalink 10 $permalink = home_url( '/' . $language . '/' . $post->post_name . '/' ); 11 } 12 13 return $permalink; 14} 15add_filter( 'post_link', 'wp_dudecom_custom_language_permalinks', 10, 3 ); 16add_filter( 'page_link', 'wp_dudecom_custom_language_permalinks', 10, 3 ); 17 18// Add rewrite rules for language-specific permalinks 19function wp_dudecom_add_language_rewrite_rules() { 20 $languages = array( 'en', 'fr', 'de' ); // Add your supported languages here 21 22 foreach ( $languages as $language ) { 23 add_rewrite_rule( 24 '^' . $language . '/([^/]+)/?$', 25 'index.php?name=$matches[1]&language=' . $language, 26 'top' 27 ); 28 } 29} 30add_action( 'init', 'wp_dudecom_add_language_rewrite_rules' ); 31 32// Add query vars for language 33function wp_dudecom_add_language_query_vars( $vars ) { 34 $vars[] = 'language'; 35 return $vars; 36} 37add_filter( 'query_vars', 'wp_dudecom_add_language_query_vars' ); 38 39// Modify the main query to filter by language 40function wp_dudecom_filter_query_by_language( $query ) { 41 if ( ! is_admin() && $query->is_main_query() && $language = get_query_var( 'language' ) ) { 42 $meta_query = array( 43 array( 44 'key' => 'language', 45 'value' => $language, 46 'compare' => '=' 47 ) 48 ); 49 $query->set( 'meta_query', $meta_query ); 50 } 51} 52add_action( 'pre_get_posts', 'wp_dudecom_filter_query_by_language' ); 53 54// Ensure permalinks are flushed on activation 55function wp_dudecom_flush_rewrite_rules() { 56 wp_dudecom_add_language_rewrite_rules(); 57 flush_rewrite_rules(); 58} 59register_activation_hook( __FILE__, 'wp_dudecom_flush_rewrite_rules' ); 60 61// Ensure permalinks are flushed on deactivation 62function wp_dudecom_deactivate_plugin() { 63 flush_rewrite_rules(); 64} 65register_deactivation_hook( __FILE__, 'wp_dudecom_deactivate_plugin' ); 66?>

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.