4 Custom Code Snippets For WordPress

written by: Luke
Last Updated: November 6, 2021

When building a website and the customer needs a bit of custom functionality added to it, most designers look for a plugin to assist in this process. However, sometimes it is easier and more convenient to simply add a snippet of code instead of adding the dreaded just one more plugin problem. For using this you have 2 different options, you can either add these codes to your functions.php file or you can use a plugin the two best ones are Code Snippets and Advanced Scripts. Now let's jump right in!

1. Automatically Empty Trash

This snippet will automatically empty your WordPress trashcan after a specified amount of time. If you need to change it from weekly then simply change the "7" to something else like "1" to make it daily. This is useful for WooCommerce products, old blog posts and other similar scenarios in which constantly emptying the trash is too burdensome.

// automatically empty trash weekly 
define( 'EMPTY_TRASH_DAYS', 7 );
PHP

2. Get Home URL

This is useful if you are building a website on a sub-directory setup and you want the user to be able to click on the logo and take them home. An example of this scenario is if I was building a website on https://sigma1.com/new-website if I simply set the logo link wrapper to "/" it will take them to https://sigma1.com and not the homepage of https://sigma1.com/new-website. To prevent hard-coding these situations you would need to use this PHP function:

function sigma1_get_home_url() {
    return  get_home_url();   
}
PHP

Or you can use it in practice like this:

<?php echo get_home_url(); ?>
PHP

Credit: WordPress Developer Handbook

3. Lock Your WordPress Site

Sometimes when working on a client's website we want to lock the entire website to prevent people from looking at it while under development. There are tons of plugins that perform this function, but we are trying to avoid using plugins! Here is the code:

// Redirect users who arent logged in...
function members_only() {
    global $pagenow;
    // Check to see if user in not logged in and not on the login page
    if( !is_user_logged_in() && $pagenow != 'wp-login.php' )
          auth_redirect();
}
add_action( 'wp', 'members_only' );
PHP

4. Change The Logo On Your WordPress Login Page

function my_login_logo_one() { 
?>

<style>
body.login div#login h1 a {
 background-image: url(http://localhost/wordpress/one.jpeg);  //Add your own logo image in this url 
padding-bottom: 30px; 
} 
</style>

<?php
} add_action( 'login_enqueue_scripts', 'my_login_logo_one' );
PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

our blog.

Related Blogs You May Enjoy

Sigma 1's Local Las Vegas Web Design Services Help Brands Stand Out

The power of a well-designed website cannot be underestimated. With our affordable web design services in Las Vegas, you can improve your company's online presence and attract…
Read More

The Ultimate Guide to Understanding Google's New Algorithm

In June 2021, Google released a new update to their search engine algorithm that brings in new ranking factors to determine how websites are "graded & scored"…
Read More

Copyright Infringement Scam

Recently we received a fill out through our contact form that was quite concerning to us so we decided to post on our website about it in…
Read More