4 added 9 characters in body
source | link
function wpex_auto_add_link_titles( $content ) {

    // No need to do anything if there isn't any content
    if ( empty( $content ) ) {
        return;return $content;
    }

    // Define links array
    $links = array();

    // Get page content
    $html = new DomDocument;
    $html->loadHTML( $content );
    $html->preserveWhiteSpace = false;

    // Loop through all content links
    foreach( $html->getElementsByTagName( 'a' ) as $link ) {

        // If the title attribute is already defined no need to do anything
        if ( ! empty( $link->getAttribute( 'title' ) ) ) {
            continue;
        }

        // Get link text
        $link_text = $link->textContent;

        // Save links and link text in $links array
        if ( $link_text ) {
            $links[$link_text] = $link->getAttribute( 'href' );
        }

    }

    // Loop through links array and update post content to add link titles
    if ( ! empty( $links ) ) {
        foreach ( $links as $text => $link ) {
            if ( $link && $text ) {
                $text    = esc_attr( $text ); // Sanitize
                $text    = ucwords( $text );  // Captilize words (looks better imo)
                $replace = $link .'" title="'. $text .'"'; // Add title to link
                $content = str_replace( $link .'"', $replace, $content ); // Replace post content
            }

        }
    }

    // Return post content
    return $content;

}
add_filter( 'the_content', 'wpex_auto_add_link_titles' );
function wpex_auto_add_link_titles( $content ) {

    // No need to do anything if there isn't any content
    if ( empty( $content ) ) {
        return;
    }

    // Define links array
    $links = array();

    // Get page content
    $html = new DomDocument;
    $html->loadHTML( $content );
    $html->preserveWhiteSpace = false;

    // Loop through all content links
    foreach( $html->getElementsByTagName( 'a' ) as $link ) {

        // If the title attribute is already defined no need to do anything
        if ( ! empty( $link->getAttribute( 'title' ) ) ) {
            continue;
        }

        // Get link text
        $link_text = $link->textContent;

        // Save links and link text in $links array
        if ( $link_text ) {
            $links[$link_text] = $link->getAttribute( 'href' );
        }

    }

    // Loop through links array and update post content to add link titles
    if ( ! empty( $links ) ) {
        foreach ( $links as $text => $link ) {
            if ( $link && $text ) {
                $text    = esc_attr( $text ); // Sanitize
                $text    = ucwords( $text );  // Captilize words (looks better imo)
                $replace = $link .'" title="'. $text .'"'; // Add title to link
                $content = str_replace( $link .'"', $replace, $content ); // Replace post content
            }

        }
    }

    // Return post content
    return $content;

}
add_filter( 'the_content', 'wpex_auto_add_link_titles' );
function wpex_auto_add_link_titles( $content ) {

    // No need to do anything if there isn't any content
    if ( empty( $content ) ) {
        return $content;
    }

    // Define links array
    $links = array();

    // Get page content
    $html = new DomDocument;
    $html->loadHTML( $content );
    $html->preserveWhiteSpace = false;

    // Loop through all content links
    foreach( $html->getElementsByTagName( 'a' ) as $link ) {

        // If the title attribute is already defined no need to do anything
        if ( ! empty( $link->getAttribute( 'title' ) ) ) {
            continue;
        }

        // Get link text
        $link_text = $link->textContent;

        // Save links and link text in $links array
        if ( $link_text ) {
            $links[$link_text] = $link->getAttribute( 'href' );
        }

    }

    // Loop through links array and update post content to add link titles
    if ( ! empty( $links ) ) {
        foreach ( $links as $text => $link ) {
            if ( $link && $text ) {
                $text    = esc_attr( $text ); // Sanitize
                $text    = ucwords( $text );  // Captilize words (looks better imo)
                $replace = $link .'" title="'. $text .'"'; // Add title to link
                $content = str_replace( $link .'"', $replace, $content ); // Replace post content
            }

        }
    }

    // Return post content
    return $content;

}
add_filter( 'the_content', 'wpex_auto_add_link_titles' );
3 added 165 characters in body
source | link

*Edit: New modified version on Github that supports images with links - https://github.com/wpexplorer/wpex-auto-link-titles/blob/master/wpex-auto-link-titles.php

*Edit: New modified version on Github that supports images with links - https://github.com/wpexplorer/wpex-auto-link-titles/blob/master/wpex-auto-link-titles.php

2 added 493 characters in body
source | link
function wpex_auto_add_link_titles( $content ) {

    // No need to do anything if there isn't any content
    if ( empty( $content ) ) {
        return;
    }

    // Define links array
    $links = array();

    // Get page content
    $html = new DomDocument;
    $html->loadHTML( $content ); 
    $html->preserveWhiteSpace = false;

    // Loop through all content links
    foreach( $html->getElementsByTagName( 'a' ) as $link ) {

        // GetIf linkthe texttitle attribute is already defined no need to do anything
        $link_textif =( ! empty( $link->textContent>getAttribute( 'title' ) ?) $link->textContent) :{
 '';           continue;
        }

        // Get link text
        $link_text = $link->textContent;

        // Save links and link text in $links array
        if ( empty( $link->getAttribute( 'title' ) )$link_text ) {
            $links[$link_text] = $link->getAttribute( 'href' );
        }

    }

    // GetLoop through links array and update post content to add link titletitles
    if ( ! empty( $links ) ) {
        foreach ( $links as $text => $link ) {
            if ( $link && $text ) {
                $text    = esc_attr( $text ); // Sanitize
                $text    = ucwords( $text );  // Captilize words (looks better imo)
                $replace = $link .'" title="'. $text .'"'; // Add title to link
                $content = str_replace( $link .'"', $replace, $content ); // Replace post content
            } 

        }
    }

    // Return post content
    return $content;

}
add_filter( 'the_content', 'wpex_auto_add_link_titles' );
function wpex_auto_add_link_titles( $content ) {

    // Define links array
    $links = array();

    // Get page content
    $html = new DomDocument;
    $html->loadHTML( $content ); 
    $html->preserveWhiteSpace = false;

    // Loop through all links
    foreach( $html->getElementsByTagName( 'a' ) as $link ) {

        // Get link text
        $link_text = ! empty( $link->textContent ) ? $link->textContent : '';

        // Save links and link text in $links array
        if ( empty( $link->getAttribute( 'title' ) ) ) {
            $links[$link_text] = $link->getAttribute( 'href' );
        }

    }

    // Get and add link title
    foreach ( $links as $text => $link ) {
        if ( $link && $text ) {
            $replace = $link .'" title="'. $text .'"';
            $content = str_replace( $link .'"', $replace, $content );
        }

    }

    // Return post content
    return $content;

}
add_filter( 'the_content', 'wpex_auto_add_link_titles' );
function wpex_auto_add_link_titles( $content ) {

    // No need to do anything if there isn't any content
    if ( empty( $content ) ) {
        return;
    }

    // Define links array
    $links = array();

    // Get page content
    $html = new DomDocument;
    $html->loadHTML( $content );
    $html->preserveWhiteSpace = false;

    // Loop through all content links
    foreach( $html->getElementsByTagName( 'a' ) as $link ) {

        // If the title attribute is already defined no need to do anything
        if ( ! empty( $link->getAttribute( 'title' ) ) ) {
            continue;
        }

        // Get link text
        $link_text = $link->textContent;

        // Save links and link text in $links array
        if ( $link_text ) {
            $links[$link_text] = $link->getAttribute( 'href' );
        }

    }

    // Loop through links array and update post content to add link titles
    if ( ! empty( $links ) ) {
        foreach ( $links as $text => $link ) {
            if ( $link && $text ) {
                $text    = esc_attr( $text ); // Sanitize
                $text    = ucwords( $text );  // Captilize words (looks better imo)
                $replace = $link .'" title="'. $text .'"'; // Add title to link
                $content = str_replace( $link .'"', $replace, $content ); // Replace post content
            } 

        }
    }

    // Return post content
    return $content;

}
add_filter( 'the_content', 'wpex_auto_add_link_titles' );
1
source | link