When using the WYSIWYG editor to create links, and selecting an existing page or post from the selection list, those links will have a title attribute by default.

Is there a simple way of turning off this behaviour? I would like turning it off at its root (i.e., in the WYSIWYG editor) rather than hooking into the final result when it is output.

Is there any way to do this that doesn’t require JavaScript (here is a solution that does) and doesn’t require manually parsing the attribute out of the final HTML end result?

Googling stuff like wordpress remove title attribute post content doesn’t seem to yield any helpful information – what they show is mostly removing the attribute from menus and page lists.

1 Answer
1

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 Reply

Your email address will not be published. Required fields are marked *