--------------------------- nodereference_url.module ---------------------------
index 3993d3d..fc6fa00 100644
@@ -7,6 +7,36 @@
  */

 /**
+ * Implementation of hook_menu().
+ *
+ * Sample to demonstrate behaviour of new custom URL feature.
+ * 1. Uncomment and modify the hook below.
+ * 2. Add your defined path to custom URL textarea in the
+ *    configuration form of your nodereference URL CCK field
+ *    (e.g. my-node-add/story/%nid).
+ * 3. Build links which point to the path above and should end
+ *    with the referenced node ID.
+ * 4. Enjoy...
+ */
+/*
+function nodereference_url_menu() {
+  $items = array();
+  $content_type = 'story';
+  $type_url_str = str_replace('_', '-', $content_type);
+  $items['my-node-add/'. $type_url_str] = array(
+    'title' => 'Add my story referring to another node',
+    'page callback' => 'node_add',
+    'page arguments' => array(1), // or pass content type directly, e.g. array('story'),
+    'access callback' => 'node_access',
+    'access arguments' => array('create', $content_type),
+    'file' => 'node.pages.inc',
+    'file path' =>  drupal_get_path('module', 'node'),
+  );
+  return $items;
+}
+*/
+
+/**
  * Implementation of hook_theme().
  */
 function nodereference_url_theme() {
@@ -193,6 +223,15 @@ function nodereference_url_widget_settings($op, $widget) {
           '#element_validate' => array('nodereference_url_fallback_validate'),
         );

+        // Handle custom paths.
+        $form['custom_urls'] = array(
+          '#type' => 'textarea',
+          '#title' => t('Custom URLs'),
+          '#description' => t("Enter one URL per line with '%nid' at the place where the referenced node ID will be found (e.g. custom/path/prefix/%nid/custom/path/suffix). You don't have to add 'node/add/CONTENT_TYPE/%nid' paths because these are handled by default. You can use simple '%' as wildcard (for at least one alpha-numeric sign or '-' or '_') in your entered paths."),
+          '#default_value' => isset($widget['custom_urls']) ? $widget['custom_urls'] : FALSE,
+          '#element_validate' => array('nodereference_url_custom_urls_validate'),
+        );
+
         $form['node_link'] = array(
           '#tree' => TRUE,
           '#type' => 'fieldset',
@@ -237,7 +276,7 @@ function nodereference_url_widget_settings($op, $widget) {
       return $form;

     case 'save':
-      return array('node_link', 'fallback');
+      return array('node_link', 'fallback', 'custom_urls');
   }
 }

@@ -261,6 +300,27 @@ function nodereference_url_node_link_validate($element, &$form_state) {
 }

 /**
+ * Element validation function that checks custom paths for correct syntax.
+ */
+function nodereference_url_custom_urls_validate($element, &$form_state) {
+  $custom_urls = $form_state['values']['custom_urls'];
+  if (isset($custom_urls) && drupal_strlen($custom_urls)) {
+    $custom_urls = explode("\n", $custom_urls);
+    foreach ($custom_urls as $path) {
+      $path = trim($path); // Delete whitespaces.
+      if (drupal_strlen($path)) {
+        $parts = explode('%nid', $path);
+        // A valid path must contain '%nid' (at the beginning, in the middle or at the end) and must not start or end with a slash.
+        if (count($parts) != 2 || ($parts[0] != '' && (substr($parts[0], 0, 1) == '/' || substr($parts[0], -1) != '/')) || ($parts[1] != '' && (substr($parts[1], 0, 1) != '/' || substr($parts[1], -1) == '/'))) {
+          form_error($element, t('A custom URL must contain %nid (only one time per path) and there should be only one custom URL per line and no leading or trailing slashes. E.g. node/%nid/revisions/%/view'));
+          break;
+        }
+      }
+    }
+  }
+}
+
+/**
  * Implementation of hook_widget().
  */
 function nodereference_url_widget(&$form, &$form_state, $field, $items, $delta = 0) {
@@ -273,6 +333,28 @@ function nodereference_url_widget(&$form, &$form_state, $field, $items, $delta =
   elseif (arg(0) == 'node' && arg(1) == 'add') {
     $referenced_nid = is_numeric(arg(3)) ? arg(3) : NULL;
   }
+  // Handle custom paths.
+  elseif (isset($field['widget']['custom_urls']) && drupal_strlen($field['widget']['custom_urls'])) {
+    $custom_urls = explode("\n", $field['widget']['custom_urls']);
+    $current_path = $_GET['q'];
+    if (drupal_strlen($current_path)) {
+      foreach ($custom_urls as $path) {
+        $path = trim($path); // Delete whitespaces.
+        if (!drupal_strlen($path)) {
+          continue;
+        }
+        // '/^node\/([0-9]+)\/[a-zA-Z_\-0-9]+\/add$/' will find 'node/%nid/%/add'
+        $path = '/^'. str_replace('/', '\/', $path) .'$/'; // Replace slashes and add start and end delimiter.
+        $path = str_replace('%nid', '([0-9]+)', $path); // Replace %nid.
+        $path = str_replace('%', '[a-zA-Z_\-0-9]+', $path); // Replace other wildcards (%).
+        $matches = array();
+        if (preg_match($path, $current_path, $matches)) {
+          $referenced_nid = ((isset($matches[1]) && is_numeric($matches[1])) ? $matches[1] : NULL);
+          break;
+        }
+      }
+    }
+  }

   // Check that the NID is a valid reference.
   if (isset($referenced_nid)) {