I’d really love to be able to conditionally apply a CSS class to link elements of images embedded into posts, but I can’t seem to figure this one out.
Basically what I’d like to achieve is to replace the default embedded image link that links the image to the full size version of itself to opening it in a colorbox.
I know I can achieve this with a bit of Javascript-trickery, but I’d love to figure out how to do this in the server side and then just attach the colorbox functionality in a straightforward manner.
So, is there a filter I should add or what’s the route to take here. Ideally I’d love to be able to write the code so that if a user embeds an image, the link opens the full size version in a colorbox but if the user specifies a custom link, it’ll work like a regular link. This shouldn’t be that hard.
Thanks a lot for any help in advance!
You can run a filter on
image_send_to_editor
, the filter runs inside theget_image_send_to_editor
function which is resposible for sending the link HTML that surrounds images sent to the editor.The filter can be found in core, in
wp-admin/includes/media
and is linked below for quick reference.core.trac.wordpress.org/browser/tags/3.1/wp-admin/includes/media.php
Thanks a LOT to user t31os for the tip on where to find my solution!
Also thanks to user orionrush for pointing out a dumb mistake I had made! 🙂
Here’s how I got what I wanted:
(Posting this answer because this question shows up on Google and I couldn’t find what I wanted through Google.)
Brief Explanation and Example
After retrieving the post from the data base, this will put all of the specified classes into the anchor tag whenever there is an anchor tag with only an image tag inside of it.
It works with many image tags in one post, as well as a variety of other weird possibilities. For instance, something like this
will become
Code (for functions.php)
Other Notes
(?![^>]*class)
is a negative lookahead so that the first regex replace rule only affects<a href...><img></a>
, not<a class... href...><img></a>
. (Read more on lookarounds.)[^>]*
is better than.*
.[^>]*
means zero or more characters that are not a>
. Without[^>]*
, I think there could be problems if there are multiple>
characters on one line or in other weird situations.'<a1 class="' . $classes . '"><img3></a>'
refers to the stuff inside corresponding parenthetical block in the corresponding pattern. In other words,1
means “put the stuff that matches what’s inside the first set of parentheses”.add_filter('the_content', 'add_classes_to_linked_images', 100, 1);
, the first parameter is the filter for getting the content of a post from the database, the second parameter is the name of the function we want to use, the third parameter is the priority of the filter (higher numbers get executed later), and the fourth parameter is the number of arguments for the filter.Someone correct me if im wrong here, but the above gives me warnings on $astart – I believe these lines:
should be:
but I may not be reading it correctly. . .
also we could dynamically add the uploads folder rather then hardcode it:
could probably substitute wp_basename but not sure of the advantages…