WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I link to http://www.glumbo.com on one of my posts. I want wordpress to automatically change the anchor text of the link to glumbo.com's title. How can I do this?

share|improve this question
Two issues with this - you will need to cache a lot of those titles and it will look spammy (page titles for anchors is common technique in spam). Are you sure you want to titles for anchors? Why not site's domain or something? – Rarst Dec 28 '10 at 8:24
Maybe you're right. I'm only asking this because vbSEO does this for vBulletin installations and I see it being used on many websites. – Matthew Hui Dec 28 '10 at 8:39
up vote 6 down vote accepted

Decided to entertain the idea. Adapted from my snippet that changes anchors to collapsed domain names. Little too verbose, but seems to work.

add_filter( 'the_content', 'anchors_to_page_titles' );

function anchors_to_page_titles( $content ) {

    preg_match_all( '/<a.*?href="(.*?)".*?>(.*?)<\/a>/', $content, $matches );
    array_shift( $matches );

    foreach( $matches[0] as $key => $url ) {

    $anchor = $matches[1][$key];

    if( $url == $anchor ) {

        $transient_key = 'page_title_'.md5($url);
        $anchor = get_transient($transient_key);

        if( !$anchor ) {

            $response = wp_remote_request($url);
            $body = wp_remote_retrieve_body($response);
            $pattern = '/title>(.*?)</';
            $title = array();
            preg_match( $pattern, $body, $title);

            if( !empty( $title ) ) {

                $title = $title[1];
                $anchor = $title;
                set_transient( $transient_key, $anchor, 60*60*24 );
            else {
                $anchor = $url;
                set_transient( $transient_key, $anchor, 60*60 );

        $content = str_replace( ">{$url}</a>", ">{$anchor}</a>", $content );


    return $content;

PS maybe it would make sense to modify post on save rather than filtering on display... Well, as per my comment I don't think page titles are good for this anyway.

share|improve this answer
Thanks for the help – Matthew Hui Dec 28 '10 at 22:01

I found a great page that give numerous downloadable examples of how to change the text inside an anchor using jQuery. They are really helpful for web 2.0 and web 3.0 sites.


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.