OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

add_rewrite_rule but no value in the get_query_var

  • Thread starter Thread starter patrice pellier
  • Start date Start date
P

patrice pellier

Guest
I'm trying to clean the URL in WordPress. instead of this www.website.com/mypage/?myvariable=fr I want to have this www.website.com/fr/mypage/

The redirection works well, but I can not get the value I put in the URL as a $_GET value.

In the first part I used add_rewrite_rule seems to work well. The redirection is OK.

Code:
function add_rewrite(){
    flush_rewrite_rules();
    add_rewrite_rule(
        'fr/([a-z0-9-]+)',
        'index.php?pagename=$matches[1]&myvariable=fr',
        'top'
    );
}
add_action('init', 'add_rewrite');

But I can not get the value of myvariable which looks empty

echo get_query_var("myvariable");

even if I use the following code

Code:
function add_query_vars($aVars) {
    $aVars[] = "myvariable";
    return $aVars;
}
add_filter('query_vars', 'add_query_vars');
<p>I'm trying to clean the URL in WordPress.
instead of this <a href="http://www.website.com/mypage/?myvariable=fr" rel="nofollow noreferrer">www.website.com/mypage/?myvariable=fr</a>
I want to have this <a href="http://www.website.com/fr/mypage/" rel="nofollow noreferrer">www.website.com/fr/mypage/</a></p>
<p>The redirection works well, but I can not get the value I put in the URL as a $_GET value.</p>
<p>In the first part I used <em>add_rewrite_rule</em> seems to work well. The redirection is OK.</p>
<pre><code>function add_rewrite(){
flush_rewrite_rules();
add_rewrite_rule(
'fr/([a-z0-9-]+)',
'index.php?pagename=$matches[1]&myvariable=fr',
'top'
);
}
add_action('init', 'add_rewrite');
</code></pre>
<p>But I can not get the value of <em>myvariable</em> which looks empty</p>
<p><code>echo get_query_var("myvariable");</code></p>
<p>even if I use the following code</p>
<pre><code>function add_query_vars($aVars) {
$aVars[] = "myvariable";
return $aVars;
}
add_filter('query_vars', 'add_query_vars');
</code></pre>
Continue reading...
 

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top