How To Add a Custom Class to the Attachment Link

Snippets by IsItWP

Are you looking for a way to add a custom class to the attachment link? This snippet will add a custom class to the attachment anchor tag.


  1. All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin.
  2. Change the class in line 3 to whatever you would like. Currently it is isitwp.
function add_class_attachment_link($html){
    $postid = get_the_ID();
    $html = str_replace('<a','<a class="isitwp"',$html);
    return $html;

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: CSS Hero Review and 21 best WordPress real estate themes.

Comments  Leave a Reply

  1. Wow..4 years ago and still useful.. Thanks a lot!

  2. Thanks a lot! 🙂

  3. Cheers to you for this snippet, it helped me solve a really annoying issue!

  4. Thank you for this snippet!

  5. Thank you for that snippet! I am looking for a way to change the class of the function “wp_get_attachment_image” in the functions.php. Do you know how to do that?

  6. life saver 😛

  7. Thanks for the snippet. I looked for it because I would like to add a custom class to links produced by wp_get_attachment_link working in /wp-includes/media.php (I want to add a specific class name to images made by the default wp gallery for lightbox). Where should I put a link like this?

    Also, how can I make sure the class doesn’t appear in every instance of wp_get_attachment_link? Can I make it conditional?

    Thanks again!

    1. Hi Matt, If you are going to want to make things conditional you will be best to make your own shortcode to display images. This snippet just finds each link and adds a class,

      1. Sorry, I meant to add class to the link itself, as required by a lightbox plugin i’m working on. /wp-includes/media.php uses wp_get_attachment_link to create its links, so this would be good. It wouldn’t actually need to be conditional either. Just make the gallery links have a class of thumb. Only it doesn’t seem to work when placed in the theme functions file, possibly because media.php which created the gallery links is in /wp-includes. I tried sticking your snippet in /wp-includes/functions.php and inside of media.php, which didn’t seem to work, maybe I did it wrong. What do you think? Your snippet is probably perfect, I just need to find the right slot!

  8. Hi Kevin, Mind telling me how did you think of this snippet ? I want to learn more about how you came up with this. 

    1.  HI Gerald,
      Glad to see you like the snippets, the hard part in many ways is thinking up what is worth doing. Some of the snippets I write, some I find online. This snippet is because I have one almost exactly the same but for tags


  9. Fantastic! Just what I was looking for.

    1. I figured you needed this Muditha, 😉 enjoy the snippet glad to hear you like it.

  10. awesome you made my day, really

    1. Cool glad to hear it John, I post snippets everyday so lets hope I can make your day again. 🙂 enjoy the snippets.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!