Removing the title attribute from links in the post content

You can create a small plugin to filter the posts and pages content, search for all the links in it and remove their title tag. This code will do it for you:

<?php

/*
    Plugin Name: Remove link's title tag
    Description: Remove link's title tag from post contents
    Version: 1.0
    Author: Your name
    Author URI: http://www.yoursite.com/
*/

function wp151111_remove_link_title_attribute_from_post_content( $content ) {

    $post_types = array( 'post', 'page' );

    if ( is_singular( $post_types ) ) :

        $doc = DOMDocument::loadHTML( $content );

        $anchors = $doc->getElementsByTagName( 'a' ); 

        foreach ( $anchors as $anchor ) :

            $anchor->removeAttribute( 'title' );

        endforeach;

        $content = $doc->saveHTML();

    endif;

    return $content;

}

add_filter ( 'the_content', 'wp151111_remove_link_title_attribute_from_post_content' );

?>

Save it as “remove-links-title-tag.php” and upload it to your plugins folder, usually “/wp-content/plugins/”. You can also paste the function and the filter hook directly into your theme’s “functions.php” file.

The code can be easily adapted to filter also custom post types or to remove other attributes from other tags.

If you are having encoding issues after applying the filter, change this line

$doc = DOMDocument::loadHTML( $content );

to

$doc = DOMDocument::loadHTML( mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' ) );

Leave a Comment