One of your most difficult jobs as a site administrator is soliciting inbound links to boost your traffic and your search engine position. Deep links, in context, are the most valuable.

Using the 'Path of Least Resistance' philosophy, provide users with link text at the bottom of every page using the following code snippet (D7) in a block.

<?php
if ((arg(0) == 'node') && is_numeric(arg(1))) {
  $args[0] = arg(1);
  $node_id = $args[0];
  $node = node_load($node_id);
  $title = variable_get("site_name") . " - " .  $node->title;
  $uri = '/node/' . $node->nid;
} else {
  $title = variable_get("site_name") . " - " .  variable_get("site_slogan");
  $uri = "";
}
$link = $GLOBALS['base_url'] . $uri;
print ( htmlspecialchars(l( $title, $link)));
?> 

Wrap the snippet in whatever text you wish to encourage readers and bloggers to deep link to your site.

Extended version

  • works also on non-node pages (e.i. Views, taxonomy)
  • adds some styling
  • selects all code with a single mouse click.

Now as a D7 module: Permalink Block

DEMO

Consider to...

  • ..extract the inline styling to put it at the bottom of your theme's style.css
  • ..put the whole code in the theme's node.tpl.php instead if your block doesn't display as desired (position or style, depending on the used theme). You'll loose the flexibility a block offers.
  • ..delete the <h3>..</h3> section if you use a block title.
<!--Selects all code with a single mouse click-->
<script type="text/javascript">
  function selectText(containerid) {
    if (document.selection) {
      var range = document.body.createTextRange();
      range.moveToElementText(document.getElementById(containerid));
      range.select();
    } else if (window.getSelection) {
      var range = document.createRange();
      range.selectNode(document.getElementById(containerid));
      window.getSelection().addRange(range);
    }
  }
</script>
<!--A simple translatable header-->
<?php $embed_code_header = t("Permalink to this page (embed code)"); ?>
<h3><?php print $embed_code_header; ?>:</h3>
<!--DIV with inline styling-->
<div id="embed-link" onclick="selectText('embed-link')" style="
  border: solid 1px DarkGrey;
  background: GhostWhite;
  font-family: Courier;
  padding: 3px;
"
>
<!--The embed code-->
<?php
// Get the title of the current page
$page_title = drupal_get_title();
// If there is no title, use the site slogan
$page_title = (empty($page_title)) ? variable_get("site_slogan") : $page_title;
// Prefix the page title with the site name
$title = variable_get("site_name") . " - " . $page_title;
// Get the url of the current page
$link = $GLOBALS['base_url'] . base_path() . current_path();
// Construct the title as a link to the source page
print ( htmlspecialchars(l( $title, $link))); 
?></div>


Visibility settings

Probably you don't want a permalink on your account page (/user) or admin pages. Usually you have a different theme for the administration pages but contributed modules might add tabs that use the normal theme . To cover most cases a snippet to use for the "Show on all pages except those listed" field could look like:

admin
admin/*
user
user/*/*
node/*/*
contact
search404



@lolandese

Nice change. I have actually implemented it on my own site. I do not agree with the in-line script and style. This can be aggregated and compressed in the page header to improve performance. The technique I use is here.

Comments

lolandese’s picture

Hi Roger,

.. in-line script and style. This can be aggregated and compressed in the page header to improve performance. The technique I use is here.

You're absolutely right. The script and style is inline only to make it work right out-of-the-box. The link you provide should then be the next step.

Thanks for sharing it. Implemented that one right away on my own sites.