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

How To Properly Create Image Cards in Bricks Builder

In this tutorial, we are going to show you how you properly create image cards in Bricks Builder. This concept applies far beyond the scope of just…
Read More

5 Step To Take Before Launching an E-Commerce Store

We all know how important E-Commerce websites are, especially with COVID-19 preventing people from purchasing items inside stores. For this post, we have decided to create an…
Read More

Expired Domain Scam

Today we received a contact form fill out with the following message: Notice#: 491343 Date: 2021-01-21 YOUR IMMEDIATE ATTENTION TO THIS MESSAGE IS ABSOLUTELY NECESSARY! YOUR DOMAIN…
Read More