• JCV

    (@psykonevro)


    Hi,

    I’m trying unsuccesfully to add a query var, the language, into my wordpress installation.
    I installed a plugin (GTranslate) and modified it so it adds a var (lang=) to the URL.
    The current example of URL is https://www.example.com/blog/about/?lang=en
    I would like to change it to
    https://www.example.com/blog/en/about/
    For all pages and posts.
    But I’m clueless, I’ve been trying many options.
    Currently, I added the following code to my child’s theme function.php:

    add_filter('query_vars', 'add_query_lang_filter') ;
    function add_query_lang_filter($query_vars) {
            $query_vars[] = filter_input( INPUT_GET, 'lang', FILTER_SANITIZE_STRING ) ?: 'fr';
    		
            return $query_vars ;
    };
    
    function custom_rewrite_tag() {
            add_rewrite_rule('^([^/]+)/([^/]+)', '?lang=$matches[1]', 'top');
    }
    add_action( 'init', 'custom_rewrite_tag' );

    But no result.

    Anybody could help me?

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    You’re WP installation is in /blog/ subfolder, right? add_rewrite_rule() is what you want, but you need to pass more information.
    add_rewrite_rule('^([^/]+)/([^/]+)/?', 'index.php?lang=$matches[1]&pagename=$matches[2]', 'top');

    Be sure to visit the permalinks settings screen after correcting your code so that the old, stale rules get flushed.

    Thread Starter JCV

    (@psykonevro)

    Thanks for you reply, @bcworkz !

    Unfortunately, it doesn’t work. The page remains the same, with the string “?lang=en” unchanged.

    I tried to check my url against the rewrite rule inspector (plugin that detect if a page is impacted by the rewrite rules) and it doesn’t detect it.

    In order to be more precise, an exemple of page is
    https://www.jcvignoli.com/blog/2020/cerro-mongon-ou-comment-jai-decouvert-des-ruines-au-perou/?lang=en

    and the complete code I’m trying in a functions.php file (child theme) according to your suggestion is

    function get_lang_string() {                   
        // Let ?lang= pass through to WP_Query            
      add_rewrite_tag( '%lang%', '([A-Za-z]+)' );  
      //$vars[] = 'lang';
       //return $vars;
    }
    add_action( 'init', 'get_lang_string' );     
                                                                                                                                          
    function stack7478067_pre_get_posts( $query ) {
    add_rewrite_rule('^([^/]+)/([^/]+)/([^/]+)/?', 'index.php?lang=$matches[1]&year=$matches[2]&pagename=$matches[3]', 'top');
    flush_rewrite_rules();
    }
    add_filter( 'init', 'change_lang_url_init' );

    The customised permalink structure (in permalink settings) is

    /%year%/%postname%/

    Any idea what’s going on?

    Moderator bcworkz

    (@bcworkz)

    Another similar rule might be matched before yours, despite the “top” parameter. You might have to include a specific, fixed base element in the URL which the rewrite rule can reliably match. An URL such as /blog/lang/en/about/ where “lang” is always present and part of the regexp. Similar to how category and tag URLs work.

    Thread Starter JCV

    (@psykonevro)

    well, just for the fun, I tried
    add_rewrite_rule('^(.*)/?', 'index.php?lang=$matches[1]&year=$matches[2]&pagename=$matches[3]', 'top');

    instead of
    add_rewrite_rule('^([^/]+)/([^/]+)/([^/]+)/?', 'index.php?lang=$matches[1]&year=$matches[2]&pagename=$matches[3]', 'top');

    And the new rule appeared as the first match. Unfortunately, the website didn’t display the pages/posts anymore ??

    I noticed if I take out the last slash, so I end up with
    add_rewrite_rule('^([^/]+)/([^/]+)/([^/]+)?', 'index.php?lang=$matches[1]&year=$matches[2]&pagename=$matches[3]', 'top');

    the rule is matched as first applying rule by rewrite rules plugins. But still no change when it comes to rewriting the URL.

    Moderator bcworkz

    (@bcworkz)

    I tried your code on my site. The year parameter seems to confuse WP. A rule that accepts a year parameter but ignores it works for me.
    add_rewrite_rule('^([^/]+)/[0-9]+/([^/]+)/?', 'index.php?lang=$matches[1]&pagename=$matches[2]', 'top');

    Thread Starter JCV

    (@psykonevro)

    That year parameter not only confuses WP but me too ??

    I tried your rewrite rule, but no luck. Thanks for trying to help me!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add language in the URL and fancy URL’ is closed to new replies.