Skip to content

SALE - Get Flat 20% OFF on Theme Bundle of 120+ Premium Themes Just at $89

Get Bundle!!

FLASH SALE - Get 25% Discount on All Themes, use code "FLASH25"

  • Home
  • About Us
  • Themes
  • Support
  • Contact
  • Blogs
Log in

Country/region

  • Afghanistan USD $
  • Åland Islands USD $
  • Albania USD $
  • Algeria USD $
  • Andorra USD $
  • Angola USD $
  • Anguilla USD $
  • Antigua & Barbuda USD $
  • Argentina USD $
  • Armenia USD $
  • Aruba USD $
  • Ascension Island USD $
  • Australia USD $
  • Austria USD $
  • Azerbaijan USD $
  • Bahamas USD $
  • Bahrain USD $
  • Bangladesh USD $
  • Barbados USD $
  • Belarus USD $
  • Belgium USD $
  • Belize USD $
  • Benin USD $
  • Bermuda USD $
  • Bhutan USD $
  • Bolivia USD $
  • Bosnia & Herzegovina USD $
  • Botswana USD $
  • Brazil USD $
  • British Indian Ocean Territory USD $
  • British Virgin Islands USD $
  • Brunei USD $
  • Bulgaria USD $
  • Burkina Faso USD $
  • Burundi USD $
  • Cambodia USD $
  • Cameroon USD $
  • Canada USD $
  • Cape Verde USD $
  • Caribbean Netherlands USD $
  • Cayman Islands USD $
  • Central African Republic USD $
  • Chad USD $
  • Chile USD $
  • China USD $
  • Christmas Island USD $
  • Cocos (Keeling) Islands USD $
  • Colombia USD $
  • Comoros USD $
  • Congo - Brazzaville USD $
  • Congo - Kinshasa USD $
  • Cook Islands USD $
  • Costa Rica USD $
  • Côte d’Ivoire USD $
  • Croatia USD $
  • Curaçao USD $
  • Cyprus USD $
  • Czechia USD $
  • Denmark USD $
  • Djibouti USD $
  • Dominica USD $
  • Dominican Republic USD $
  • Ecuador USD $
  • Egypt USD $
  • El Salvador USD $
  • Equatorial Guinea USD $
  • Eritrea USD $
  • Estonia USD $
  • Eswatini USD $
  • Ethiopia USD $
  • Falkland Islands USD $
  • Faroe Islands USD $
  • Fiji USD $
  • Finland USD $
  • France USD $
  • French Guiana USD $
  • French Polynesia USD $
  • French Southern Territories USD $
  • Gabon USD $
  • Gambia USD $
  • Georgia USD $
  • Germany USD $
  • Ghana USD $
  • Gibraltar USD $
  • Greece USD $
  • Greenland USD $
  • Grenada USD $
  • Guadeloupe USD $
  • Guatemala USD $
  • Guernsey USD $
  • Guinea USD $
  • Guinea-Bissau USD $
  • Guyana USD $
  • Haiti USD $
  • Honduras USD $
  • Hong Kong SAR USD $
  • Hungary USD $
  • Iceland USD $
  • India USD $
  • Indonesia USD $
  • Iraq USD $
  • Ireland USD $
  • Isle of Man USD $
  • Israel USD $
  • Italy USD $
  • Jamaica USD $
  • Japan USD $
  • Jersey USD $
  • Jordan USD $
  • Kazakhstan USD $
  • Kenya USD $
  • Kiribati USD $
  • Kosovo USD $
  • Kuwait USD $
  • Kyrgyzstan USD $
  • Laos USD $
  • Latvia USD $
  • Lebanon USD $
  • Lesotho USD $
  • Liberia USD $
  • Libya USD $
  • Liechtenstein USD $
  • Lithuania USD $
  • Luxembourg USD $
  • Macao SAR USD $
  • Madagascar USD $
  • Malawi USD $
  • Malaysia USD $
  • Maldives USD $
  • Mali USD $
  • Malta USD $
  • Martinique USD $
  • Mauritania USD $
  • Mauritius USD $
  • Mayotte USD $
  • Mexico USD $
  • Moldova USD $
  • Monaco USD $
  • Mongolia USD $
  • Montenegro USD $
  • Montserrat USD $
  • Morocco USD $
  • Mozambique USD $
  • Myanmar (Burma) USD $
  • Namibia USD $
  • Nauru USD $
  • Nepal USD $
  • Netherlands USD $
  • New Caledonia USD $
  • New Zealand USD $
  • Nicaragua USD $
  • Niger USD $
  • Nigeria USD $
  • Niue USD $
  • Norfolk Island USD $
  • North Macedonia USD $
  • Norway USD $
  • Oman USD $
  • Pakistan USD $
  • Palestinian Territories USD $
  • Panama USD $
  • Papua New Guinea USD $
  • Paraguay USD $
  • Peru USD $
  • Philippines USD $
  • Pitcairn Islands USD $
  • Poland USD $
  • Portugal USD $
  • Qatar USD $
  • Réunion USD $
  • Romania USD $
  • Russia USD $
  • Rwanda USD $
  • Samoa USD $
  • San Marino USD $
  • São Tomé & Príncipe USD $
  • Saudi Arabia USD $
  • Senegal USD $
  • Serbia USD $
  • Seychelles USD $
  • Sierra Leone USD $
  • Singapore USD $
  • Sint Maarten USD $
  • Slovakia USD $
  • Slovenia USD $
  • Solomon Islands USD $
  • Somalia USD $
  • South Africa USD $
  • South Georgia & South Sandwich Islands USD $
  • South Korea USD $
  • South Sudan USD $
  • Spain USD $
  • Sri Lanka USD $
  • St. Barthélemy USD $
  • St. Helena USD $
  • St. Kitts & Nevis USD $
  • St. Lucia USD $
  • St. Martin USD $
  • St. Pierre & Miquelon USD $
  • St. Vincent & Grenadines USD $
  • Sudan USD $
  • Suriname USD $
  • Svalbard & Jan Mayen USD $
  • Sweden USD $
  • Switzerland USD $
  • Taiwan USD $
  • Tajikistan USD $
  • Tanzania USD $
  • Thailand USD $
  • Timor-Leste USD $
  • Togo USD $
  • Tokelau USD $
  • Tonga USD $
  • Trinidad & Tobago USD $
  • Tristan da Cunha USD $
  • Tunisia USD $
  • Türkiye USD $
  • Turkmenistan USD $
  • Turks & Caicos Islands USD $
  • Tuvalu USD $
  • U.S. Outlying Islands USD $
  • Uganda USD $
  • Ukraine USD $
  • United Arab Emirates USD $
  • United Kingdom USD $
  • United States USD $
  • Uruguay USD $
  • Uzbekistan USD $
  • Vanuatu USD $
  • Vatican City USD $
  • Venezuela USD $
  • Vietnam USD $
  • Wallis & Futuna USD $
  • Western Sahara USD $
  • Yemen USD $
  • Zambia USD $
  • Zimbabwe USD $
  • Facebook
  • Pinterest
  • Instagram
THEMESPRIDE
  • Home
  • About Us
  • Themes
  • Support
  • Contact
  • Blogs
Log in Cart My Account

Elevate Your WordPress Experience With These Essential Code Snippets

April 11, 2024
Elevate Your WordPress Experience With These Essential Code Snippets
Share

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

Back to blog

Search

Blog Categories

  • Blog

WordPress Theme Bundle

Bestseller Themes

Automobile
Premium NGO Organization
Car booking
Fitness Supplement
Supermarket
Author Writer
  • Facebook
  • Instagram
  • Pinterest

WordPress Theme Bundle

Buy Now

LINKS

  • Best WordPress Themes
  • Support
  • Contact
  • Terms and Conditions
  • Privacy Policy
  • Return
  • Blogs
  • Free WordPress Themes
  • Tour Operator WordPress Theme
  • Auto Dealer WordPress Theme

Topics

  • Automobile
  • Business
  • Construction
  • Ecommerce
  • Education
Payment methods
  • PayPal
Copyright © 2026, All Right Reserved THEMESPRIDE
  • Choosing a selection results in a full page refresh.
  • Opens in a new window.