Never heard of WordPress code snippets? They are little pieces of code designed to add functionality to a WordPress website. Typically, developers place snippets in the functions.php or wp-config.php files of the active theme (another reason to use a child theme). Check out these popular snippets and see if you need their functionality for your website.

Thousands of code snippets available

There are literally thousands of snippets available. You install them by manually editing your functions.php or wp-config.php files. You can also use a plugin to add snippets. Pay attention when you are copying and pasting snippets from websites because sometimes web servers change the single and double quotes for standardized printing. If a snippet does not work or breaks your website, then check for incorrect quotation marks. Always test snippets first.

WordPress comes with pre-set image sizes. What if your website design requires different image sizes to give your website a distinct look? Try this snippet:

if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'new-size', 300, 100, true ); // New image size
add_filter('image_size_names_choose', 'my_image_sizes');
function my_image_sizes($sizes) {
$addsizes = array(
"new-size" => __( "New Size")
$newsizes = array_merge($sizes, $addsizes);
return $newsizes;

Speed up WordPress

Is your website loading slowly? Is Google PageSpeed Insights telling you to defer the loading of JavaScript scripts? Use this snippet to force JavaScript scripts to the footer area of your website:

remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);

add_action('wp_footer', 'wp_print_scripts', 5);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
add_action('wp_footer', 'wp_print_head_scripts', 5);

Control user registrations

Do you want visitors to have the ability to register (i.e. as a customer) but you don’t want them to have access to the backend dashboard? Then use this snippet:

add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) ) {
wp_redirect( home_url() ); // Redirect to the Home page

Disable links in comments

Do you want to disable the ability for users to add links to comments (i.e. link away from your website)? Use this snippet:

remove_filter('comment_text', 'make_clickable', 9);

The default excerpt length for WordPress posts is 55 characters. Do you want a different excerpt length to match your website design? Use this snippet:

function new_excerpt_length($length) {
return 100; // New excerpt length
add_filter('excerpt_length', 'new_excerpt_length');

WordPress saves post revisions in the database for easy roll back to an earlier version. These revisions add up over time causing your database to bloat. Do you want to limit the number of post revisions WordPress saves to the database? Use this snippet:

define('WP_POST_REVISIONS', 5); // Number of post revisions saved

These little snippets add functionality to your WordPress website. They are compact and precise. When looking for a certain functionality for your website, check to see if there is a snippet available using your favourite search engine.

Need help with WordPress code snippets?

Let’s chat

Share This