Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

If I link to an article on a website, is there a way to automatically include a title attribute with the value being the title of the linked page?

share|improve this question

1 Answer 1

Don't know if this works but it should,

function get_page_title($url){
        if( !class_exists( 'WP_Http' ) )
            include_once( ABSPATH . WPINC. '/class-http.php' );
        $request = new WP_Http;
        $result = $request->request( $url );
        if( is_wp_error( $result ) )
            return false;

        if( preg_match("#<title>(.+)<\/title>#iU", $result, $t))  {
            return trim($t[1]);
        } else {
            return false;


    function auto_add_title_to_link($content){
        $html = new DomDocument;
        $html->preserveWhiteSpace = false; 
        //get all links
        foreach($html->getElementsByTagName('a') as $link) {
        //make sure it dosent have a title
            if ($link->getAttribute('title') == '' || empty($link->getAttribute('title')))
                $links[] = $link->getAttribute('href');
        //get title and add it
        foreach ($links as $link){
            $title = get_page_title($link);
            if (false !== $title){
                $replace = $link.' title="'.$title.'"';
                $content = str_replace($link,$replace,$content);

        return $content;
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.