Zum Inhalt springen

Archiv für "Filter"

WordPress filter for lazy loading src

Chris Coyier (@css) has shared a code snippet that will add loading="lazy" to every scr=" instance in WordPress's content.

But this will replace every instance, not only in img or iframe tags, which can lead to some unwanted replacements.

Here's my version for that filter:

// in theme’s functions.php or a plugin
function wbr_add_lazy_loading($content){
$content = preg_replace('/(< [^>]*?)(\ssrc=)(.*?\/?>)/','\1 loading="lazy" src=\3',$content);
return $content;
}
add_filter('the_content','wbr_add_lazy_loading');

(! beware that WordPress here adds an empty space after the "<", look at the test link for the correct regex)

This should match all src attributes in tags, but excludes for example "data-src" from the match and ...

Add class attribute to WordPress "the_tags" markup

I'm in the process of gradually enhancing my site's markup with microformats, in order to "indiewebify" my site further.
On thing I noticed while working on this at the Düsseldorf Indiewebcamp, is that WordPress (or the way my theme handles) tags on posts has no way to get an additional class inside the link markup. I noticed this while POSSEing to flickr -- my categories were transferred, my tags not so much, because the class="p-category" was missing. I found a way to modify the the_tags output by hooking into WP's term_links-$taxonomy filter in my theme's function.php.

Maybe there's a smarter ...