• Resolved FrederikDussault

    (@frederikdussault)


    Hello Chouby,

    I hope everything is well on your side.

    Following with my series of questions about Polylang-Rewrite interactions and recent exchanges about getting the laguage from the path.
    Ref: https://ww.wp.xz.cn/support/topic/getting-language-from-path?replies=4#post-7149454

    Still trying to get the permalink to define the language from $_GET[‘lang’].
    I cannot wrap my mind over it, it seems.

    I have find out we may use add_filter(‘post_link’, ..).
    Could you please tell me if the pll_language_defined hook is execute before or after the add_filter(‘post_link’, ..)?

    In the best of your knowledge, once Polylang modify the permalink, is the new permalink passed through the rewrite rules again?

    example:

    $regex = 'dl/.*/?';
    rewrite = 'index.php?pagename=dl';
    add_rewrite_rule( $regex, $rewrite, 'top' );
    
    $regex = '(en|fr)/dl/.*/?';
    rewrite = 'index.php?pagename=dl&lang=$matches[1]';
    add_rewrite_rule( $regex, $rewrite, 'top' );

    Considering the following (over simplified) function will add the language to the permalink:

    function dl_language() {
    	global $polylang;
    	$reqLangSlug = $_GET['lang'];
    	$polylang->curlang = $polylang->model->get_language($reqLangSlug);
    }
    add_action('pll_language_defined', 'dl_language');

    http://www.next.ca/dl/aa/aa/00/ri/00/?lang=fr
    would be caught by Polylang and added the language slug, generating
    http://www.next.ca/fr/dl/aa/aa/00/ri/00/?lang=fr

    Will this new permalink be reinterpreted by the rewrite rules (caught by the second rule) and be understood as index.php?pagename=dl&lang=fr ?

    https://ww.wp.xz.cn/plugins/polylang/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Chouby

    (@chouby)

    Could you please tell me if the pll_language_defined hook is execute before or after the add_filter(‘post_link’, ..)?

    Generally, filters are added when pll_language_defined is fired, but in this case (and this is the same for all links needed on admin side), the filter is added *before* pll_language_defined is fired.

    In the best of your knowledge, once Polylang modify the permalink, is the new permalink passed through the rewrite rules again?

    I don’t get your question. There is no link between the code modifying the link and the code modifying the rewrite rules.

    Thread Starter FrederikDussault

    (@frederikdussault)

    I don’t get your question. There is no link between the code modifying the link and the code modifying the rewrite rules.

    What I am trying to understand is how the WordPress is acting when Polylang change the permalink.

    Polylang changes the URL via the add_filter(‘post_link’,..). Then Worpress takes the new post permalink and serve the page.

    But before serving the page, does it check again against the rewrite rules, to see if the new permalink would match any rewrite rules?

    $regex = 'dl/.*/?';
    rewrite = 'index.php?pagename=dl';
    add_rewrite_rule( $regex, $rewrite, 'top' );
    
    $regex = '(en|fr)/dl/.*/?';
    rewrite = 'index.php?pagename=dl&lang=$matches[1]';
    add_rewrite_rule( $regex, $rewrite, 'top' );

    Once /fr/ is added to the permalink, will wordpress understand the second rewrite rule? $regex = ‘(en|fr)/dl/.*/?’;

    Please note I also am asking on rewrite support page.
    https://ww.wp.xz.cn/support/topic/post_link-filter-and-rewrite-rules

    Thanks in advance to any light your may bring.

    Thread Starter FrederikDussault

    (@frederikdussault)

    Hello Chouby,

    After a few tests I may say the updated permalink is passed to server rewrite rules again.

    The list order of rewrite rules is important. The more specific one, or ones not needing extra process, should be listed first.

    In my case, I should have listed

    $regex = '(en|fr)/dl/.*/?';
    rewrite = 'index.php?pagename=dl&lang=$matches[1]';
    add_rewrite_rule( $regex, $rewrite, 'top' );
    
    $regex = 'dl/.*/?';
    rewrite = 'index.php?pagename=dl';
    add_rewrite_rule( $regex, $rewrite, 'top' );

    The first rule would be picked up only once the second rules would have been fired, the permalink processing&update done.

    Marking the post as resolved.

    Have a nice evening 🙂

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

The topic ‘Order of hook&filter execution’ is closed to new replies.