user3360944
user3360944

Reputation: 558

How do you add dynamic anchor text in wordpress links.

We would like to have links in a word press site that have the current meta description of the target site as the anchor text of the link.

I understand this requires either javascript or php and am not sure which is the appropriate approach and which is most easily supported within word press.

Upvotes: 2

Views: 1138

Answers (2)

Barry Meijer
Barry Meijer

Reputation: 760

Interesting question and yes it is possible. You can't do it with javascript or AJAX because the browsers' cross-domain policy won't allow you to do this. I think it has to be a combination of both.

The first solution that i can think of is creating some kind of proxy with PHP, that returns the contents of the targeted URL (the one you link to):

<?php
  $url=$_POST['url'];
  if($url!="")
    echo file_get_contents($url);
?>

Lets say we call this little script "getit.php". Now you can get a AJAX call going, that sends the target url to your .php file and the .php file returns the content of the targeted page. Then you are going to extract the description meta-tag from the returned data.

Of course you could get it in the PHP file and only return the meta description, because that would even be a better solution. You could try something like this in the PHP:

<?php
   $url=$_POST['url'];
   $tags = get_meta_tags($url);

   return $tags['description'];
?>

PS. Apologies for my bad English, it's not my native language.

Upvotes: 1

Tomas Ramirez Sarduy
Tomas Ramirez Sarduy

Reputation: 17471

If you have Wordpress then you should have cURL installed and activated (or find the way). Also, there is a PHP function called get_meta_tags(). So, you could do something like this assuming you have an array of links with each URL called $links_array:

foreach($links_array as $link){
    $tags = get_meta_tags($link);
    $description = @$tags['description'];
    //Printing each link
    echo "<a href='$link'>$description</a>";
}

Upvotes: 1

Related Questions