admin管理员组

文章数量:1125559

I am redirecting a specific url format using add_rewrite_rule after adding an additional query_var (mlang) to it.

Example:

http://localhost/periodicals/sri-bn is to be redirected to http://localhost/index.php?name=sri&mlang=bn

(periodicals is declared as a custom post type)

The redirection is working fine except I am missing the query_var value in the theme/template page for the post type 'periodicals' (single-periodicals.php).

Here is my redirection code:

class PxMultiLang{
public function __construct()
{
    define('PX_MULTI_LANG', [
            ['code'=>'', 'lang'=> 'English'],
            ['code'=>'bn', 'lang'=> 'Bengali'],
            ['code'=>'hn', 'lang'=> 'Hindi']
    ]);

    add_filter('query_vars', [$this, 'px_multilang_query_var']);
    add_action('init', [$this, 'px_multilang_multi_redirect']);
}

function px_multilang_multi_redirect()
{
    add_rewrite_tag('%mlang%', '([^/]+)');
    foreach(PX_MULTI_LANG as $tlk => $tlv)
    {
        if($tlk > 0)
        {
            add_rewrite_rule(
                    '^periodicals/([^/]+)-'.$tlv['code'].'[/]?$', 
                    'index.php?name=$matches[1]&mlang='.$tlv['code'],
                    'top'
            );
        }
    }
}

function px_multilang_query_var($query_vars)
{
    $query_vars[]= 'mlang';
    return $query_vars;
}
}

In the theme file (single-periodicals.php), I have tried to capture the mlang query_var without success. The code I used is as:

$multilang= isset($_REQUEST['mlang'])? $_REQUEST['mlang']:'';
$multilang2= get_query_var('mlang');

global $wp_query;
$multilang3= $wp_query->query_vars['mlang'];

echo '$multilang= '.$multilang; //empty value
echo ' / $multilang 2= '.$multilang2; //empty value
echo ' / $multilang 3= '.$multilang3; //empty value

echo '<pre>'; print_r($wp_query->query_vars); echo '</pre>'; //No trace of "mlang" in the output

Please advice. Thank you for reading this far.

I am redirecting a specific url format using add_rewrite_rule after adding an additional query_var (mlang) to it.

Example:

http://localhost/periodicals/sri-bn is to be redirected to http://localhost/index.php?name=sri&mlang=bn

(periodicals is declared as a custom post type)

The redirection is working fine except I am missing the query_var value in the theme/template page for the post type 'periodicals' (single-periodicals.php).

Here is my redirection code:

class PxMultiLang{
public function __construct()
{
    define('PX_MULTI_LANG', [
            ['code'=>'', 'lang'=> 'English'],
            ['code'=>'bn', 'lang'=> 'Bengali'],
            ['code'=>'hn', 'lang'=> 'Hindi']
    ]);

    add_filter('query_vars', [$this, 'px_multilang_query_var']);
    add_action('init', [$this, 'px_multilang_multi_redirect']);
}

function px_multilang_multi_redirect()
{
    add_rewrite_tag('%mlang%', '([^/]+)');
    foreach(PX_MULTI_LANG as $tlk => $tlv)
    {
        if($tlk > 0)
        {
            add_rewrite_rule(
                    '^periodicals/([^/]+)-'.$tlv['code'].'[/]?$', 
                    'index.php?name=$matches[1]&mlang='.$tlv['code'],
                    'top'
            );
        }
    }
}

function px_multilang_query_var($query_vars)
{
    $query_vars[]= 'mlang';
    return $query_vars;
}
}

In the theme file (single-periodicals.php), I have tried to capture the mlang query_var without success. The code I used is as:

$multilang= isset($_REQUEST['mlang'])? $_REQUEST['mlang']:'';
$multilang2= get_query_var('mlang');

global $wp_query;
$multilang3= $wp_query->query_vars['mlang'];

echo '$multilang= '.$multilang; //empty value
echo ' / $multilang 2= '.$multilang2; //empty value
echo ' / $multilang 3= '.$multilang3; //empty value

echo '<pre>'; print_r($wp_query->query_vars); echo '</pre>'; //No trace of "mlang" in the output

Please advice. Thank you for reading this far.

Share Improve this question asked Feb 8, 2024 at 20:44 sariDonsariDon 2651 gold badge2 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Changing the add_rewrite_rule did the trick:

add_rewrite_rule('^periodicals/([^/]+)-'.$tlv['code'].'[/]?$', 'index.php?periodicals=$matches[1]&post_type=periodicals&name=$matches[1]&mlang='.$tlv['code'], 'top');

So, I assume for custom post types, using add_rewrite_rule with index.php as query need some extra query variables. A sample will be as:

add_rewrite_rule('^<custom_post_type>/([^/]+)-'.$tlv['code'].'[/]?$', 'index.php?<custom_post_type>=$matches[1]&post_type=<custom_post_type>&name=$matches[1]&mlang='.$tlv['code'], 'top');

本文标签: url rewritingqueryvar values empty in theme file after addrewriterule redirection