Useful WordPress code snippets

If you wish to add any additional code to a WordPress site, where you place it is important. If it’s theme specific code you should add it to `functions.php`, in the theme folder. For CSS you should add this to the theme customizer. If the latter is not available to you, your theme may have a `custom.css` file for this.  If it’s not a theme specific change then I create my own, single-file plugin.

A lot of my code snippets over the years have gone on to become full-blown plugins in their own right but there is still some code that remains in my site plugin. So I thought I’d share them.

Remove WordPress Version

This first one removed the version of WordPress that you’re using from your meta data in the header. A useful security caution, as it prevents you from potentially giving away the fact that you’re using an insecure version of WordPress.

<?php
function site_version_removal() { return ''; }
add_filter( 'the_generator', 'site_version_removal' );
?>
view raw functions.php hosted with ❤ by GitHub

Allow Shortcodes in the Feed

As the name suggests this allow shortcodes to be actioned within your RSS feed. Use with caution!

<?php
add_filter( 'the_content_rss', 'do_shortcode' );
?>
view raw functions.php hosted with ❤ by GitHub

Add Your Own Avatar

Is there a fun image you’d like to use as the default user image if they don’t have a Gravatar? This is what you need – just replace `[your image]` with the location of the image on your `uploads` folder.

<?php
function new_avatar( $avatar_defaults ) {
$new_avatar = content_url() . '/uploads/[your image]';
$avatar_defaults[ $new_avatar ] = 'No Avatar';
return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'new_avatar' );
?>
view raw functions.php hosted with ❤ by GitHub

Show Post if Search Returns One Result

Few things in life (okay, I exaggerate) are more annoying than searching for something, getting one result and you still have to click it to view it – this rectifies that. Thanks to my fellow Automattician Kailey for the original code.

<?php
function single_result() {
if ( is_search() ) {
global $wp_query;
if ( $wp_query -> post_count == 1 && $wp_query -> max_num_pages == 1 ) {
wp_redirect( get_permalink( $wp_query -> posts[ 0 ] -> ID ) );
exit;
}
}
}
add_action( 'template_redirect', 'single_result' );
?>
view raw functions.php hosted with ❤ by GitHub

Highlight Old Posts

There are lots of plugins that will add a message to your posts when they’re greater than a specific age (say, 2 years) but that isn’t what I wanted – after switching themes and generally rebuilding this site a lot of images were broken, etc. I went through the posts, fixing them, until a specific age – I therefore wanted to highlight any post before a fixed date (i.e. where I’d got to). This, simple piece of code, does this. Obviously, change the message and date to taste.

<?php
function old_post_message( $content ) {
if ( get_the_time( 'U' ) < strtotime( '1st January 2013' ) ) {
$content = '<div class="alert alert-info alert-dismissible"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Due to updates, over time, that have been made to the site and the age of this article, this post may not display correctly. In particular images may be missing or product reviews display incorrectly.</div>' . $content;
}
return $content;
}
add_filter( 'the_content', 'old_post_message' );
?>
view raw functions.php hosted with ❤ by GitHub

Add a Default Image to Jetpack Related Posts

Due to the aforementioned changes to my site a number of posts no longer have featured images and may not include an image either. This code allows you to specify an image that will appear as a default in such situations. Replace [image url] with the full URL to the default image.

<?php
function jrp_custom_image( $media, $post_id, $args ) {
if ( $media ) {
return $media;
} else {
$permalink = get_permalink( $post_id );
$url = apply_filters( 'jetpack_photon_url', '[image url]' );
return array( array(
'type' => 'image',
'from' => 'custom_fallback',
'src' => esc_url( $url ),
'href' => $permalink,
) );
}
}
add_filter( 'jetpack_images_get_images', 'jrp_custom_image', 10, 3 );
?>
view raw functions.php hosted with ❤ by GitHub

Do not add any context below each Related Post

More on related posts – this simple filter will remove the context below each (normally it will say why it’s shown, for example it’s in the same category). I remove this because I don’t use categories but it still displays this as the context reason.

<?php
add_filter( 'jetpack_relatedposts_filter_post_context', '__return_empty_string' );
?>
view raw functions.php hosted with ❤ by GitHub

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from David Artiss

Subscribe now to keep reading and get access to the full archive.

Continue reading