I want to add href to all img tags on my drupal post. My bellow code is working fine on html page but on drupal doesn't work(do not do anything):
Code:

  <?php
$doc = new DOMDocument;
$doc->load($node->body[LANGUAGE_NONE][0]['value']);
$images = $doc->getElementsByTagName('img');
foreach ($images as $img) {
$link = $doc->createElement('a'); 
$link->setAttribute('href', 'http://www.example.com');
$img->parentNode->insertBefore($link, $img);
$link->appendChild($img);
}
echo $doc->saveHTML();
 ?> 

what could be wrong?

Thank you!

Comments

Jaypan’s picture

What's the resulting output?

panakour’s picture

I have put link to all img tags with jquery with bellow code

<script> 
  jQuery(document).ready(function($) {
  $(document).ready(function() {
  $('img').wrap("<a href='<?php print $node_url; ?>' </a>");
});
});
  </script>

My problem now is that all node_url are the same. The above code take the last node url and puting in all img while i want put separate link for each image. Is there a solution?

Jaypan’s picture

Well, you are setting the URL for every link the same here:

$link->setAttribute('href', 'http://www.example.com');

You'll have to set the URL dynamically, rather than hard-coding it like you have.

Jaypan’s picture

This isn't related to the original topic, as you are not using DOMDocument, so you should open a new thread for your question.