Add class to links in wordpress easily, easy way to add class to all links in post automatically, simple wordpress function and easy to use.

How To Add Class To Links

Very easy, using the content filter, we will add class to all links in post automatically for new posts or old posts, and we can do something like removing empty paragraphs or add content after post, and we can do more. You can use Extend Link plugin, All-In-One, add class to links, rel nofollow, link id, and more.

Add Class To Links

Copy function code and paste it in your functions.php file, function code:

function WPTime_add_class_to_all_links($content){
    /* Filter by Qassim Hassan - */
    $my_class = 'class="my-custom-class another-class"'; // your link class
    $add_class = str_replace('<a ', "<a $my_class ", $content); // add class
   return $add_class; // display class in links
add_filter('the_content', 'WPTime_add_class_to_all_links');

This is “my-custom-class another-class” your class, change it, please note if you want one class, enter class name without space, like this “my-class”, but if you want multi class, enter space between class, like this “class1 class2”.

ID To Links

Just replace this line:

$my_class = 'class="my-custom-class another-class"';

To this line:

$my_class = 'id="my-id"';