Automatically Link Featured Images to Posts

Wrap featured images in your theme in links to posts.

Use Snippet
php
<?php
/**
 * Wrap the thumbnail in a link to the post.
 * Only use this if your theme doesn't already wrap thumbnails in a link.
 *
 * @param string $html The thumbnail HTML to wrap in an anchor.
 * @param int    $post_id The post ID.
 * @param int    $post_image_id The image id.
 *
 * @return string
 */
function wpcode_snippet_autolink_featured_images( $html, $post_id, $post_image_id ) {
	$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
	return $html;
}
add_filter( 'post_thumbnail_html', 'wpcode_snippet_autolink_featured_images', 20, 3 );
WPCode Team
300+

Add default ALT to avatar/Gravatar Images

Add the user's name as a default alt tag to the Gravatar images loaded on...

Use Snippet
php
<?php
add_filter(
	'pre_get_avatar_data',
	function ( $atts ) {
		if ( empty( $atts['alt'] ) ) {
			if ( have_comments() ) {
				$author = get_comment_author();
			} else {
				$author = get_the_author_meta( 'display_name' );
			}
			$alt = sprintf( 'Avatar for %s', $author );
			$atts['alt'] = $alt;
		}
		return $atts;
	}
);
WPCode Team
<10

Remove Query Strings From Static Files

Use this snippet to remove query string from CSS & JS files and improve performance...

Use Snippet
php
<?php
function wpcode_snippet_remove_query_strings_split( $src ) {
	$output = preg_split( "/(&ver|?ver)/", $src );
	return $output ? $output[0] : '';
}
add_action( 'init', function () {
	if ( ! is_admin() ) {
		add_filter( 'script_loader_src', 'wpcode_snippet_remove_query_strings_split', 15 );
		add_filter( 'style_loader_src', 'wpcode_snippet_remove_query_strings_split', 15 );
	}
} );
WPCode Team
<10