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

Making Elements Sticky With CSS - No Plugin Necessary

We have had a few people ask us how we managed to get our sidebar to "stick" as your scroll down the page. So we decided to…
Read More

A Webmasters Guide to Integrating SEO With Web Design

Bringing SEO and SERP optimization into line with a quality interface is the Holy Grail of web design. This means that your website shouldn’t only look fantastic…
Read More

Nifty CSS Hover Animations

For this blog post, we will show you how to add even more CSS hover animations for links and/or menu items. If you want to see the…
Read More