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.
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');
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', '//cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv.js' ); 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' );
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' ); }
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 );
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' ); }
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; } add_filter('pre_get_posts','SearchFilter');
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.
Welcome to Hot Web Dev October 2024, featuring the latest technology and web development news.… Read More
In this tutorial, you’ll build a fun and interactive guessing game using Svelte 5, the… Read More
Welcome to Hot Web Dev September 2024, featuring the latest technology and web development news.… Read More
The JavaScript guessing game tutorial is a simple beginner's project. It features modern JavaScript syntax… Read More
If you have been following the monthly Hot Web Dev magazine you will find the… Read More
Welcome to Hot Web Dev August 2024, featuring the latest technology and web development news.… Read More