Best way to pass arguments to another page in Wordpress
-
16-10-2019 - |
Question
What is the best way to pass arguments to another page in wordpress. I did it this way :
<a href="get_permalinka(id_of_page).'/&i=2&j=3&k=4'">Link/a>
I get this arguments with $_GET['i'],$_GET['j'],$_GET['k'],problem is : this works just with default permalinks,but when I change it to some other permalink type,it does not work any more.Note - I'm passing these arguments from homepage to another page(template page).
Thank you for your time.
Solution
Use add_query_arg() to do this.
Here's a useful function if you need to get the current page URL (when get_permalink is inaccesible, like on Archives):
function get_current_page_url() {
$request = esc_url($_SERVER["REQUEST_URI"]);
$pageURL = (is_ssl() ? 'https' : 'http').'://';
if ($_SERVER["SERVER_PORT"] != "80") $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$request; else $pageURL .= $_SERVER["SERVER_NAME"].$request;
if (false === strpos(get_option('home'), '://www.')) $pageURL = str_replace('://www.', '://', $pageURL);
if (false !== strpos(get_option('home'), '://www.') && false === strpos($pageURL, '://www.')) $pageURL = str_replace('://', '://www.', $pageURL);
return $pageURL;
}
OTHER TIPS
add_filter('query_vars', 'p_query');
function p_query($qvars) {
$qvars[] = 'i';
return $qvars;
}
<a href="<?= add_query_arg('i', '2', get_permalink(some_page_id)); ?>">Link</a>
and on the page I call
$qvar = get_query_var('i');
echo $qvar;
$qvar is empty!
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange