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 - https://wp-time.com */ $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"';
Enjoy.
3 Comments
Mario
Hi, this is nice and clean, thanks. I have a question: how about adding a class to links to specific domains? not all links. Thanks!
Morgan
Seems this doesn’t work anymore on latest version of wordpress
Qassim Hassan
Try Extend Link plugin.