Search code examples
wordpress.htaccessmod-rewritepermalinks

Add "?lang=en" to the end of my links in WordPress


I'm developing a website in WordPress and I'm having some troubles with the the permalinks...

I'm looking to add "?lang=en" to the end of my links like so... "http://example.com/?lang=en", "http://example.com/pageone?lang=en"

I have been messing around with the .htaccess file and failing with my results. Can anyone point me in the right direction to achieving this. Any help with this would be greatly appreciated.

htaccess code

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Thanks in advance.


Solution

  • If you're trying to physically change the URLs to include that string, .htaccess can't take care of that. What you'll need to do is write a WordPress plugin to alter the permalinks. For example:

    <?php
    /*
    Plugin Name: change permalink
    */
    
    function change_the_permalink( $url ) {
        return( sprintf(
            "%s?lang=%s",
            $url,
            "en"
        ));
    }
    
    // standard wordpress filter
    add_filter('post_link', 'change_the_permalink');
    
    ?>
    

    Place this file in /wp-content/plugins and activate the plugin. Note that this alters all permalinks, so, you will likely need finer logic in place.