Essential Code Snippets | Numerous sites on the web rely on WordPress since it is a robust and flexible CMS. While programmers love it for its programmable extensibility, its easy design is helpful for beginners as well. We'll examine a wealth of code snippets in this piece that might enhance your WordPress experience in several ways, from functionality to speed.
Improve Performance with Browser Caching: Essential Code Snippets
Caching in a browser saves static resources locally, reducing load times. To activate browser caching, include the following code in your.htaccess file:
apache - Essential Code Snippets
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 2 year"
ExpiresByType image/jpeg "access 2 year"
ExpiresByType image/gif "access 2 year"
ExpiresByType image/png "access 2 year"
ExpiresByType text/css "access 2 month"
ExpiresByType application/pdf "access 2 month"
ExpiresByType text/x-javascript "access 2 month"
ExpiresByType application/x-shockwave-flash "access 2 month"
ExpiresByType image/x-icon "access 2 year"
ExpiresDefault "access 4 days"
</IfModule>
## EXPIRES CACHING ##
Customizing the Excerpt Length: Essential Code Snippets
By adjusting the excerpt length, you may modify the amount of material displayed on archive pages, increasing interaction. By tailoring the snippet length to your site's layout, you may find the sweet spot between informativeness and visual appeal. WordPress excerpts are automatically capped at a certain length. The following code snippet can help you suit your purposes:
php
function custom_excerpt_length($length) {
return 20; // Adjust the number to your desired excerpt length
}
add_filter('excerpt_length', 'custom_excerpt_length');
Customize the WordPress Login Screen: Essential Code Snippets
Change the look of your WordPress site's login screen and increase its security simultaneously. Include this code snippet into the functions.php file of your theme to replace the standard WordPress logo with your own, bolstering brand recognition and improving usability. This code snippet will strengthen authentication and add your company's logo to the WordPress login screen. Include it in the functions.php file of your theme:
php
function custom_login_logo() {
echo '<style type="text/css">
h1 a { background-image: url(' . get_stylesheet_directory_uri() . '/images/custom-login-logo.png) !important; }
</style>';
}
add_action('login_head', 'custom_login_logo');
Replace 'custom-login-logo.png' with the address of your business logo.
Disable WordPress Emoji Script:
You can improve WordPress speed by turning off the emoji script if you don't plan on using emojis. If you want to improve your site's load time, you may add the following code to the functions.php file of your theme.
php
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
Limit Post Revisions:
WordPress stores all of your post changes indefinitely, which might cause your database to grow. Keeping WordPress post modifications to a minimum is essential for optimal database performance. Including the following code in your site's header may limit the amount of revisions stored, which will help keep your site lean and mean. You may restrict the number of revisions in your wp-config.php file to preserve disk space using the following code:
php
define('WP_POST_REVISIONS', 5);
Change '5' to the number of revisions you desire to keep.
Enable Shortcodes in Widgets:
Adding shortcodes to your WordPress widgets is a simple way to increase their usefulness. You may give your widgets the power of shortcodes by inserting a single line of code into the functions.php file of your theme. Widgets in WordPress do not support shortcodes by default. Use the following code to enable this feature:
php
add_filter('widget_text','do_shortcode');
Redirect Users After Login:
A powerful method of personalization is to redirect people once they have logged in. If you edit the functions.php file of your WordPress theme, you may direct visitors to different pages depending on their role or personal preferences. It can boost overall user engagement and pleasure. Add the following code to the functions.php file of your theme to redirect users to a specified page once they log in:
php
function custom_login_redirect($redirect_to, $request, $user) {
// Is there a user to check?
if (isset($user->roles) && is_array($user->roles)) {
// Is it an administrator?
if (in_array('administrator', $user->roles)) {
//Redirect them to the default place
return home_url('/admin-dashboard');
} else {
return home_url();
}
} else {
return $redirect_to;
}
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);
Change '/admin-dashboard' to the URL you want.
Add Custom Excerpts to Pages:
WordPress does not have built-in support for page excerpts. However, you may enhance this feature by adding a simple code snippet to your theme's functions.php file. With this, you can improve user engagement by using extracts tailored specifically to each page. WordPress pages can't have excerpts enabled by default. Insert this line of code into your theme's functions.php to activate them:
php
add_post_type_support('page', 'excerpt');
Disable WordPress Admin Bar:
Despite its convenience, the WordPress admin bar may clash with your site's aesthetic, depending on your theme. You can add the following code snippet into the functions.php file of your theme to turn it off, making for a more streamlined and less distracting user experience. It comes in handy when you're trying to provide the front end of your site a more organized appearance. Insert this line of code:
php
add_filter('show_admin_bar', '__return_false');
Customize the Admin Footer:
You may modify the WordPress admin footer to reflect your unique style. Modifying the default information and adding personalized messages, links, or branding may be accomplished with only a few lines of code, resulting in a more polished and convenient administrative interface. Insert this line of code into the functions.php file of your theme:
php
function custom_admin_footer() {
echo 'Thank you for using <a href="https://wordpress.org/" target="_blank">WordPress</a>!';
}
add_filter('admin_footer_text', 'custom_admin_footer');
You are welcome to make whatever changes you see fit to the text.
Change Login Error Message:
You may increase the WordPress site's security by altering the login error message. Use a code snippet to offer a more general answer than the standard error message. Potential attackers won't be able to learn the details of any failed login attempts. Use the following code:
php
function custom_login_error() {
return 'Incorrect login credentials. Please try again.';
}
add_filter('login_errors', 'custom_login_error');
Conclusion
These code examples are only a taste of what you can do to improve and personalize your WordPress site. Incorporating these snippets can improve your site's performance regardless of your expertise level as a developer. These code snippets are a great resource to have at your disposal as you go further into the realm of WordPress customization.
Checkout Our Premium WordPress Themes