html - Removing the title attribute from links in the post content

admin2025-06-02  2

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.

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.

Share Improve this question edited May 23, 2017 at 12:40 CommunityBot 1 asked Feb 28, 2012 at 12:41 PekkaPekka 5422 gold badges9 silver badges33 bronze badges 3
  • The title attribute is a useful for SEO and Accessibility as it gives more meaning to the link than just 'click here!' for example. I would leave it in and avoid removing it otherwise you'll be hurting some of your visitors with bad links etc. – Alex Older Commented Feb 28, 2012 at 15:06
  • Thanks, but I have pressing reasons for getting rid of it. Chrome's tendency to display them on hover, for one; the link title not matching the linked text and creating confusion, for another. – Pekka Commented Feb 28, 2012 at 15:20
  • The title attribute is not useful for SEO. It was in the past, but it was discounted by most engines due to abuse. Source: I do SEO for a living. – chrisguitarguy Commented Sep 22, 2012 at 16:36
Add a comment  | 

1 Answer 1

Reset to default 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/
*/

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' ) );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748813936a313941.html

最新回复(0)