By mrtbc on
I'm trying to use PHP to print a URL in the body field of a node. I am using the PathAuto module and my node URLs are in this format:
http://www.mydomain.com/fruit/apples/coxs-apples
The URL I need to print should contain this and then automatically append the string "/buy" at the end, so:
http://www.mydomain.com/fruit/apples/coxs-apples/buy
I have tried several combinations of the code below, but each time some portion of the URL is removed:
<a href="<?php print $node->path ?>/get">
Quite often I get:
or
http://www.mydomain.com/fruit/apples/buy (No node title)
Any ideas please?
Thanks very much.
Comments
you could use any number of
you could use any number of PHP's predefined variables to get this value, including
$_SERVER['PHP_SELF'], which would return '/fruit/apples/coxs-apples' in your example above.i'm sure you're aware of the security implications of using php in your nodes' bodies -- don't do it if you can avoid it! (e.g., if possible, generate the link w/ PHP in your template files instead.)
(and a handy reference if you don't know it already: the variables available in the node template file are visible on the node.tpl.php page on the api site. not sure if 'path' is in the node object off the top of my head.)
hth!
oops, i might have lied. off
oops, i might have lied. off the top of my head, i think
$_SERVER['PHP_SELF']will return a piece of the "non-clean" URL (e.g. '/index.php?q=/fruit/apples/coxs-apples'). i think $_SERVER['REQUEST_URI'] will get you closer.also check out the drupal_lookup_path function if you find yourself needing to figure out Drupal system paths from path aliases, or vice versa.
arh1 thank you - Are there
arh1 thank you - Are there security implications to using PHP if I am the only user that can create and edit nodes?
Try <?php$path =
Try
Thanks very much Cog Rusty,
Thanks very much Cog Rusty, that works perfectly!
Sorry, one more question
Sorry, one more question please.
As well as making a text link I'm also trying to create a link using an image. I've tried this:
<a href="<?php $path = drupal_get_path_alias($_GET['q']) . '/buy'; print $path; ?>" target="_blank"><img src="http://www.adomain.com/animage.jpg" class="myclass"/></a>But I get:
http://www.mydomain.com/fruit/apples/fruit/apples/coxs-apples/buy
rather than:
http://www.mydomain.com/fruit/apples/coxs-apples/buy
Thanks very much.
Oops! Browsers see that as a
Oops! Browsers see that as a path relative to where you are. In my suggestion, the l() function was fixing that. Try an absolute path instead:
Or even better, to cover all cases, clean URLs or not:
Thanks!
Thanks!
For me $base_path only
For me $base_path only rendered / (slash), therefore I amended the code as follows:
If you need to print title, too, this is however very simple: