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:


The URL I need to print should contain this and then automatically append the string "/buy" at the end, so:


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:



http://www.mydomain.com/fruit/apples/buy (No node title)

Any ideas please?

Thanks very much.


arh1’s picture

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.)


arh1’s picture

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.

mrtbc’s picture

arh1 thank you - Are there security implications to using PHP if I am the only user that can create and edit nodes?

cog.rusty’s picture


$path = drupal_get_path_alias($_GET['q']) . '/buy';
print l("Buy it!", $path);
mrtbc’s picture

Thanks very much Cog Rusty, that works perfectly!

mrtbc’s picture

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:


rather than:


Thanks very much.

cog.rusty’s picture

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:

$path = base_path() . drupal_get_path_alias($_GET['q']) . '/buy';

Or even better, to cover all cases, clean URLs or not:

$path = url($_GET['q']) . '/buy';
mrtbc’s picture


reinis.berzins’s picture

For me $base_path only rendered / (slash), therefore I amended the code as follows:

<?php global $base_url; print $base_url . url($_GET['q']); ?>

If you need to print title, too, this is however very simple:

<?php print $title; ?>