Title: Permalink update process by Polylang &#8211; Please explain
Last modified: August 30, 2016

---

# Permalink update process by Polylang – Please explain

 *  [FrederikDussault](https://wordpress.org/support/users/frederikdussault/)
 * (@frederikdussault)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/permalink-update-process-by-polylang-please-explain/)
 * Hello Chouby,
 * Still trying to understand how Polylang proceed.
 * As discussed ealier ([https://wordpress.org/support/topic/getting-language-from-path](https://wordpress.org/support/topic/getting-language-from-path);
   [https://wordpress.org/support/topic/order-of-hookfilter-execution](https://wordpress.org/support/topic/order-of-hookfilter-execution)),
   I am trying to execute a code to switch language from an URL parameter lang.
 * I thought it was working but I was wrong. I really – and quickly – need to understand
   how Polylang is adding/replacing the language slug in permalink and how it tells
   worpress to reconsider the permalink.
 * NOTE: I am not concerned by rewrites anymore.
 * My URL: `www.next.ca/dl?lang=fr`
    A rewrite rule:
 *     ```
       function dl_rewrite_rules() {
       	add_rewrite_rule(
       		'dl.*',
       		'index.php?pagename=dl',
       		'top' );
       }
       add_action( 'init', 'dl_rewrite_rules' );
       ```
   
 * So far, I have this:
 * 1) Polylang scans the URL and does not find a language code (en|fr), so it sets
   language to default (en) and fires “pll_language_defined”.
 * 2) My function below is executed. It gets the lang parameter and if the lang 
   parameter is present, changes the language by setting $polylang->curlang.
 *     ```
       function detectDLPageLanguage() {
       	//Hooked on PLL event, no need to check PLL existence
       	//Over simplified code
   
       	global $polylang;
               // $_GET['lang'] == 'en' or 'fr'
       	$polylang->curlang = $polylang->model->get_language($_GET['lang']);
       }
       add_action('pll_language_defined', 'detectDLPageLanguage');
       ```
   
 * At this point, only the current language has changed.
 * What happens next?
 * My goal is to get the permalink to be modified from `www.next.ca/dl?lang=fr` 
   to `www.next.ca/fr/dl?lang=fr`
    but actualy does not. Whatever I do it simply
   goes to default language, `www.next.ca/en/dl?lang=fr`.
 * Is there extra instructions I should give Polylang to update the permalink according
   the updated $polylang->curlang?
    Is there other hook(s) involved?
 * Please advise,
    Thanks
 * [https://wordpress.org/plugins/polylang/](https://wordpress.org/plugins/polylang/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Thread Starter [FrederikDussault](https://wordpress.org/support/users/frederikdussault/)
 * (@frederikdussault)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/permalink-update-process-by-polylang-please-explain/#post-6370720)
 * Extra information.
 * The following does not change the language slug neither.
 *     ```
       function dl_switch_to_fr () {
       	global $polylang;
       	$polylang->curlang = $polylang->model->get_language( 'fr' );
       }
       add_action( 'pll_language_defined', 'dl_switch_to_fr', 0 );
       ```
   
 * It changes the current language but does not add /fr to the permalink.
 *  Thread Starter [FrederikDussault](https://wordpress.org/support/users/frederikdussault/)
 * (@frederikdussault)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/permalink-update-process-by-polylang-please-explain/#post-6370726)
 * Chouby,
 * Another question.
 * In frontend.php, function parse_query.
 *     ```
       $qv = $query->query_vars;
       [..]
       if (!empty($qv['lang'])) [...]
       ```
   
 * It seems you already have support for lang parameter.
 * Is this picking up the `lang=fr` in `www.next.ca/dl?lang=fr` ?
 *  Thread Starter [FrederikDussault](https://wordpress.org/support/users/frederikdussault/)
 * (@frederikdussault)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/permalink-update-process-by-polylang-please-explain/#post-6370753)
 * Here are my setting is case you need them.
 * **Polylang Settings:**
    **Default language:** en **URL modifications:** The language
   is set from the directory name in pretty permalinks Example: [http://next.localhost/en/my-post/](http://next.localhost/en/my-post/)
   Remove /language/ in pretty permalinks Example: [http://next.localhost/en/](http://next.localhost/en/)(
   Checked) The front page url contains the language code instead of the page name
   or page id (Checked) When the front page is visited, set the language according
   to the browser preference
 * **Permalink Settings**
    Day and name [http://next.localhost/2015/07/27/sample-post/](http://next.localhost/2015/07/27/sample-post/)
 *  Thread Starter [FrederikDussault](https://wordpress.org/support/users/frederikdussault/)
 * (@frederikdussault)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/permalink-update-process-by-polylang-please-explain/#post-6370756)
 * Sorry, I am adding and adding to the post.
 * I have found another portion of code that is looking to URL parameter lang presence:
 *     ```
       abstract class PLL_Choose_Lang {
       [..]
       public function __construct(&$polylang) {
          [..]
          if (PLL_AJAX_ON_FRONT || false === stripos($_SERVER['SCRIPT_FILENAME'], 'index.php'))
             $this->set_language(empty($_REQUEST['lang']) ? $this->get_preferred_language() : $this->model->get_language($_REQUEST['lang']));
       ```
   
 * Does this mean it would be possible to get Polylang set the language based on
   the presence of lang parameter?
 * What would be missing in my setting (or code) to make it work?
 *  Thread Starter [FrederikDussault](https://wordpress.org/support/users/frederikdussault/)
 * (@frederikdussault)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/permalink-update-process-by-polylang-please-explain/#post-6370770)
 * Okay, what about just using set_language()?
 * `$polylang->set_language( $polylang->model->get_language($_REQUEST['lang']) )`
   would be good.
 * Do I understand correctly the following code of line prevents the permalink to
   be re-evaluate by the rewrite rules?
    `remove_action('wp', array(&$this, 'wp'),
   5); // won't attempt to set the language a 2nd time`

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Permalink update process by Polylang – Please explain’ is closed to new
replies.

 * ![](https://ps.w.org/polylang/assets/icon-256x256.png?rev=3433336)
 * [Polylang](https://wordpress.org/plugins/polylang/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/polylang/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/polylang/)
 * [Active Topics](https://wordpress.org/support/plugin/polylang/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/polylang/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/polylang/reviews/)

## Tags

 * [hook](https://wordpress.org/support/topic-tag/hook/)

 * 5 replies
 * 1 participant
 * Last reply from: [FrederikDussault](https://wordpress.org/support/users/frederikdussault/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/permalink-update-process-by-polylang-please-explain/#post-6370770)
 * Status: not resolved