Skip to main content Jump to list of all articles

WordPress Functions PHP Super Useful Snippets

Adding Code snippets to the WordPress functions.php file enables you to perform actions and use filters in a way that will improve functionality of your WordPress site which can make your theme more organised and cleaner. Here is a bunch of useful snippets to further enhance your WordPress theme.

WordPress Website Image
Image Provided by Gil C /

Responsive Images

Having to edit, replace or upload every single image in your WordPress installation to make it responsive would be extremely daunting. This little snippet you can add a class to all your past and future images.

In this example I will use the img-responsive class from bootstrap but if you have your own replace img-responsive on line 4.

function add_image_responsive_class($content) {
   global $post;
   $pattern ="/<img(.*?)class="(.*?)"(.*?)>/i";
   $replacement = '<img$1class="$2 img-responsive"$3>';
   $content = preg_replace($pattern, $replacement, $content);
   return $content;
add_filter('the_content', 'add_image_responsive_class');

Enqueue Scripts and Styles for Internet Explorer using Conditional Comments

Thanks to WordPress version 4.2  you can now target older versions of IE using Conditional Comments without having to hardcode them in your WordPress header theme file.

The following example HTML5shiv JavaScript will load in Internet Explorer 9 and under.

wp_enqueue_script( 'html5shiv', '//' );
wp_script_add_data( 'html5shiv', 'conditional', 'lt IE 9' );

This example an IE only CSS file will load in Internet Explorer 9 and under.

wp_enqueue_style( 'my_ie', get_template_directory_uri() . '/css/ie.css');
wp_style_add_data( 'my_ie', 'conditional', 'lt IE 9' );

Redirect Author URL to About Page

If you are the only blog author then having an author page displaying all of your latest posts seems pretty pointless. Why not redirect your users to your about page instead.

add_filter( 'author_link', 'my_author_link' );

function my_author_link() {
    return home_url( 'about' );

Removing the version number from Scripts and Styles

Speed up your WordPress site by removing the script and styles version number which, as a result, will also improve your overall score when using PageSpeed etc.

function wow_remove_script_version( $src ){
    return remove_query_arg( 'ver', $src );
add_filter( 'script_loader_src', 'wow_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', 'wow_remove_script_version', 15, 1 );

De-Registering Styles

Interested in reducing the amount of HTTP Requests? If every plugin loads a front-end CSS file this could result in slowing your blog down further, particularly if you are not using a caching plugin.

If the plugin you use is pretty stable then you could disable the stylesheet and add the content directly to your theme stylesheet.  In this example I have disabled the contact form 7 CSS file.

add_action( 'wp_print_styles', 'my_deregister_styles', 100 );

function my_deregister_styles() {
    wp_deregister_style( 'contact-form-7' );

Search Posts Only

If you prefer not to have pages appearing in your default WordPress search.  This little snippet will filter only the posts and include them in the search results.

function SearchFilter($query) {
    if ($query->is_search) {
            $query->set('post_type', 'post');
            return $query;


Attach Permalink to Post Thumbnail

This little snippet adds a permalink to all of your post thumbnail images.

add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

function my_post_image_html( $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;


Do you have any favourite code snippets that you use in your WordPress blog or would like to add anymore to this list. Please feel free to send them and I’ll continue to update this post often.

Comments are closed.

Discover more from WorldOWeb

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

Continue reading