I need to construct a link in WordPress based on a variable out of the URL of that page.
Although in PHP this might be easy, in WP this seems to be difficult (I understood that WP does not allow it for security reasons).
So, I added a filter in my WordPress installation (functions.php) according to this link in order to get that variable and use it programatically in my WP post page in the construction of my link.
However, the link forms with two spaces (one before and one after I call the relevant function).
So, the code behind the link is:
<?php echo ("<a href='www.mydomain.com/purchases/?refid="); ?> <?php echorefid(); ?> <?php echo ("'>download this amazing file</a>") ?>
and the output code (in HTML) when I try to access the webpage at http://www.mydomain.com/?refid=3267 is:
<a href='www.mydomain.com/purchases/?refid= 3267 '>download this amazing file</a>
Please note the space before 3267 string and the space after this string.
Obviously, the link is broken.
The function echorefid() appears in the following code I added to functions.php:
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'refid';
return $qvars;
}
function echorefid()
{
global $wp_query;
if (isset($wp_query->query_vars['refid']))
{
print $wp_query->query_vars['refid'];
}
}
Any help would be highly appreciated.
The cause of the spaces it the fact that you have separated multiple
<?php ?> <?php ?>with spaces separating them. Those spaces outside<?php ?>tags are sent as output to the browser.You only need one of them for all the PHP code. Combine them by concatenating the three groups and switch the outer quotes to single quotes or escape the inner quotes.