11 Good Quality Code Snippets For WordPress Website – Best of 2023

Code Snippets For WordPress Website

Code Snippets for Your WordPress Website are used to do certain actions such as to customize your site’s themes and WP core to make it look more stunning and functional. These actions can easily be accomplished with some dedicated small plugin. But what if you can do that in WordPress without installing these plugins? Yes, we understand that you are so eager to know.

Suppose, you dislike the admin bar at the top and want to customize it? Yes, this can be done by code snippets for your site. In addition, such code snippets are placed in one of the WordPress core or theme files (usually the function.php file of your theme).

Code Snippets – An Overview

Little things can make a huge difference to your WordPress site that makes it so great. The code snippets just provide some high level of optimization that allows you to meet your needs which is really infinite. However, it sounds nice, doesn’t it? “But I do not know anything about coding,” some might say.

It’s fine because you don’t need any advanced coding knowledge to use the most useful and quality code snippets for your WordPress site. All you need to know is how to use it and where to use it. 

Code Snippets can be used in many ways. However, most of the time this involves going to the functions.php file. But you might have to think about it because this file is specific to the theme. Consequently, if you decide to change your WordPress site theme later and this file contains non-theme specific tasks. Then, you have to start from scratch.

Note: If you want to make changes to the parent theme and then update it, all the changes you make in the functions.php file will be overwritten. Then you will have to create them again. You will have to make sure that you are using a child theme.

Code Snippets for WordPress Website

Reduce Post Revisions

When you write on WordPress blogs, it automatically saves your work in the draft of your post so that you can fetch it at any time when you need it. Simply these are called Post Revisions that are valuable for your WordPress site.

But on the other hand, if not take the effective certain measurement to control these revisions, they can overburden your database and eventually leads to slow down your blog’s loading performance. For this, you can put some constraints by setting some limits on these.

Just use the below code to reduce post revisions;

define('WP_POST_REVISIONS', 5);                

Here, 5 is the number of Post Revisions. To disable these Post Revisions altogether, just use the below code;

define('WP_POST_REVISIONS', false);

This snippet will track your posts when you use the Twitter username. As the articles posted by the admin of a WordPress site are called posts. It will automatically create a link for you. Which look likes as follows:

Code Snippets
Code Snippets

WordPress Version Number

WordPress is the most commonly used CMS in the world. Where, Content Management System (CMS) is a system which allows users to easily create, edit, organize and publishing content. CMS It also makes the best target for hackers.

However, to ensure that the WordPress version is hidden from the public. Add the following line of code to your function.php file.

The function.php file is also known as theme function file. It is used by WordPress theme to define classes, actions, functions, and filters that to be used by our templates. function.php file is automatically loaded in the admin bar and front-end pages of a WordPress site.

As theme is a group of stylesheets and templates that are used to describe the appearance and display of a WordPress site. These templates are those files which control how your WordPress site will be displayed on the Web.

These files draw information from your WordPress MySQL database and generate the HTML code which is then sent to the web browser. As Admin Bar is an easily accessible toolbar with a few shortcuts to some central WordPress.com dashboard pages.

<?php

 

// Remove the version number of WP

// Warning – this info is also available in the readme.html file in your root directory – delete this file!

remove_action(‘wp_head’, ‘wp_generator’);

?>

Disabling Automatic JPEG Compression

Most of the WordPress blogs/websites use image optimization plugins, such as WP Smush. WP Smush is one of the other popular WordPress image compression plugins. It automatically compresses the images on upload and compress and optimizes your older image files as well.

It’s a good idea to disable the built-in Image Optimizer. The built-in image optimizer reduces the quality of the image to 90, in which there is no real-world visual distinction.

add_filter( ‘jpeg_quality’, ‘smashing_jpeg_quality’ );

 

function smashing_jpeg_quality()

{

return 100;

}

However, We also suggest working along with lazy load plugins for better optimization and fast load times.

Making Search Results More Refined and Controlled

There are several scenarios when a certain page or post should not be shown in search results. To make it happen, you must edit some code in your function.php file.

Private pages can also be hidden with this technique, just make sure to back up everything before making changes.

function filter_search($query)

 

{

if ($query->is_search)

{

$query->set(‘post_type’, ‘post’);

}

return $query;

}

add_filter(‘pre_get_posts’, ‘filter_search’);

Modulating the Number of Searches that are Shown in the Search Page

No one wants to scroll long in the search page, for that you can limit the number of searches you have done and therefore can improve the overall user search experience.

function limit_posts_per_search_page()

 

{

if ( is_search() )

set_query_var(‘posts_per_archive_page’, 20);

}

add_filter(‘pre_get_posts’, ‘limit_posts_per_search_page’);

New user registers and you want to send them to a certain page

If you need a user registration on your WordPress site, you may require a new user to redirect to a certain page after registration is complete. This code snippet looks like this:

function wps_registration_redirect(){

return home_url( ‘/finished/’ );

}

add_filter( ‘registration_redirect’, ‘wps_registration_redirect’ );

Add a Custom Background (BG)

Add a Custom Background, Code Snippets is used when you wish to add a custom background to your website.

// Add support for backgrounds

add_custom_background();

Keep Clients from Updating on Their Own

One of these things is that some web designers tell about the market and their services. That clients can update their WordPress site on their own. However, there are times when this is not the best way to manage the website. Using this part of the code it is possible for you to remove the nag code.

Add_action(‘admin_menu’,’wphidenag’);

 

function wphidenag()

{

(remove_action(‘admin_notices’,’update_nag’,3 );

}

Add a Shortcode to Widgets

Sometimes, whatever you want to do, add a shortcode to the widget, but the good news is that the WordPress code snippet below will allow you to add a shortcode in the text widget.

Where shortcodes are those special functions/tags that allows the user to quickly and easily pull related bits of mentioned functionality into their content. Also, widgets allows you to add content and features to your sidebar without writing any code.

add_filter( ‘widget_text’, ‘do_shortcode’ );

Setting Minimum Content-Length

We have already discussed how spamming can be determined for the blog/website. As the word Spam is used for unwanted content submitted through comments, user registration, posts, contact forms, or any other way.

Now, we will ensure that spammers do not run away from just short sentences or tones.

Using the following snippet, you can easily add minimal comment lengths to the blog/website. The code should be added to the Functions.ff file and the user will be asked to add a more valuable comment instead of two words.

add_filter(‘preprocess_comment’, ‘minimal_comment_length’);

 

function minimal_comment_length($commentdata)

{

$minimalCommentLength = 20; //change it according to your requirement

if ( strlen(trim($commentdata[‘comment_content’])) < $minimalCommentLength)

{

wp_die(‘All comments must be at least’ . $minimalCommentLength . ‘characters long’)

}

return $commentdata;

}


Bottomline

With the help of code snippets, you can customize your Website’s themes to make them more stunning which will make a qualitative impact on your WordPress blogs without the use of a specific plugin. And yes, again we can say that some small changes can make a big difference with these Code snippets we have mentioned.

We hope that this article helped you a lot. You can also talk to our WordPress Website Maintenance team to get the quick support services. Just dial +1-888-738-0846 (Toll-Free). We will be pleased to help you.

Thanks!


Leave a Reply