Support » Plugin: Polylang » Polylang and add_rewrite_rule

  • Hello.
    I am trying to add polylang pll_get_post to work with a custom plugin I’ve created. My issue is the code below

    function custom_rewrite_basic() {
      add_rewrite_rule('^el/transfer-from-([^/.]+)-to-([^/.]+)', 'index.php?page_id='.pll_get_post(get_option('we_taxi_search_results_id')).'&from=$matches[1]&to=$matches[2]', 'top');
      //echo 'index.php?page_id='.pll_get_post(get_option('we_taxi_search_results_id')).'&from=$matches[1]&to=$matches[2]';
      add_rewrite_rule('^transfer-from-([^/.]+)-to-([^/.]+)', 'index.php?page_id='.pll_get_post(get_option('we_taxi_search_results_id')).'&from=$matches[1]&to=$matches[2]', 'top');
    }
    add_action('init', 'custom_rewrite_basic');

    If I echo the pll_get_post etc, it shows me the correct ID.

    When I try to access that page rewrite, it loads the frontpage. I’ve checked the debug toolbar and i see that the ID is not passed on the rewrite table. It goes like page_id=&from=$matches[1]

    If I remove the pll_get_post, I get the ID and everything working as should.

    Is there any way to tell wordpress to run this part after Polylang is initialized? It seems that the init cannot use polylang’s pll_get_post function

    https://wordpress.org/plugins/polylang/

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

    (@chouby)

    Hi!

    There are 2 issues with your code.

    1. pll_get_post() without the language parameter needs a current language. Most probably, you flush the rewrites rules on admin so it can’t work.
    2. If you add one rewrite rule for only one language, it will work only for this language. Remember that rewrite rules are cached.

    You must explicitely create your rewrite rules with all languages.

    Hello.
    According to this page pll_get_post requires only post ID which is provided by option page.

    The rules are for two languages (el and en) but I hide main language (en).
    My problem is that if I handcode page IDs (if I remove pll_get_post and say page_id=X) the above works perfectly.

    On the above snippet, if I echo the string, pll_get_post returns the correct ID

    But when building Rewrite table, its ignored.
    I am flushing rules everytime I check. (via admin). Is there any way to provide language from admin? As I want to generalize this plugin

    Plugin Author Chouby

    (@chouby)

    Yes, it’s optional when there is a current language. But the current language is not always defined on admin. And rules are flushed on admin.

    Also if the language is set from the content, the current language on frontend is defined in ‘wp’ hook with priority 5, too late for your ‘init’ hook.

    So how can I add language on admin ? Is there any way?

    Plugin Author Chouby

    (@chouby)

    You can specify the language code as second parameter. As described in the doc you were linking to.

    Thank you sir 🙂

    Hi guys,

    I am getting crazy about this stuff I don’t know if possibly Polylang is my problem.

    I want WordPress to accept this URL:

    http://projekte/patagonia/wordpress/es/busqueda/nube/or/buscar/

    instead of this one:

    http://projekte/patagonia/wordpress/es/busqueda/?s=nube&searchmethod=or&searchbutton=buscar

    and added the following function to the functions.php of my child theme:

    function custom_rewrite_rule() {
    add_rewrite_rule(‘^busqueda/([^/]*)/([^/]*)/([^/]*)/?’,’index.php?name=busqueda&s=$matches[1]&searchmethod=$matches[2]&searchbutton=$matches[3]’,’top’);
    }
    add_action(‘init’, ‘custom_rewrite_rule’, 100);

    The page has two languages, ES and EN. I also saved the permalinks to flush the cache. The language is set from the directory name in pretty permalinks.

    Any ideas or suggestions?

    Thanks in advance

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Polylang and add_rewrite_rule’ is closed to new replies.