Reputation: 51
I am attempting to call a website root by using a PHP function within a HTML link.
I have created the function bloginfo() below and the correct link output is http://www.example.com/subdirectory/file.php.
The two methods of calling the function are similar but method 1 does not work and method 2 works.
Please can somebody explain why method 1 does not work and suggest a solution. Thanks.
<?php
function bloginfo($show) {
switch ($show) {
case 'template_url':
echo "www.example.co.uk";
break;
}
}
// Method 1 - does not work
echo "<a href=\"http://<?php bloginfo('template_url'); ?>/subdirectory/file.php\">test link 1</a>";
?>
<html>
<body>
<!-- Method 2 - works! -->
<a href="http://<?php bloginfo('template_url'); ?>/subdirectory/file.php">test link 2</a>
</body>
</html>
Update
echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">
Thanks for your help everyone. Unfortunately I could not get the common answer (above line of code) to work as for some reason 'www.example.com' would be printed but not as a link, and the link direction simply became '/subdirectory/file.php'.
To solve the problem I gave up on incorporating a function and decided to simply use the PHP Define method below which works for both methods.
<?php
//this line of code can be put into an external PHP file and called using the PHP Include method.
define("URL", "www.example.com", true);
// Method 1 - works!
echo "<a href=\"http://".URL."/subdirectory/file.php\">test link 1</a>";
?>
<html>
<body>
<!-- Method 2 - works! -->
<a href="http://<?php echo URL; ?>/subdirectory/file.php">test link 2</a>
</body>
</html>
Upvotes: 1
Views: 933
Reputation: 5605
It was because you were using within tags and not telling it to echo either. The below code will work - I have commented out your old line and added a new line that will work
<?php
function bloginfo($show) {
switch ($show) {
case 'template_url':
echo "www.example.co.uk";
break;
}
}
// Method 1 - does not work
//echo "<a href=\"http://<?php bloginfo('template_url'); ?>/subdirectory/file.php\">test link 1</a>";
echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">test link 1</a>"; <--- changed the above line to this... now both this and the lower line work
?>
<html>
<body>
<!-- Method 2 - works! -->
<a href="http://<?php bloginfo('template_url'); ?>/subdirectory/file.php">test link 2</a>
</body>
</html>
Upvotes: 0
Reputation: 577
or this:
echo "<a href=\"http://{bloginfo('template_url')}/subdirectory/file.php\">test link 1</a>";
Upvotes: 0
Reputation: 2967
You've nested some php tags
echo "<a href=\"http://" .bloginfo('template_url') . "/subdirectory/file.php\">test link 1</a>";
Upvotes: 1
Reputation: 41428
The double quoted string is making your php block in the first method just a plain old text string. Try this:
echo "<a href=\"http://".bloginfo('template_url')."/subdirectory/file.php\">test link 1</a>";
Upvotes: 6