15 Useful WordPress Snippets.

A compilation of WordPress snippets from around the web.

Aidan Coleman
All Kinds Of Stories
7 min readJul 24, 2018

--

Wordpress snippets to save you time

When you need great functionality on WordPress without the glitz, glam and extra jazz of third-party plug-ins, snippets are here to save the day.

WordPress is a content management system (CMS) originally made for blogging; however, it has developed into a CMS used for more robust websites. That being said, using a lot of plugins can bloat your website. Using snippets for WordPress can get you the same functionality without the bulk. This is a list of snippets that we have compiled for your use.

Before we get there, take note: Please — please! — always make a backup of your files before making any modifications.

1. Default robots.txt for WordPress

A robots.txt file located in the root folder of your WordPress installation. It offers an advantage in search engine optimization by allowing your site to communicate with search engine indexing bots. It lets them know what they should and shouldn’t index in your website files. A robots.txt file won’t hold your site back from being indexed, but it helps clean up pages that aren’t necessary to be filed in search engines.

How to Implement:

  1. Create a file named robots.txt

2. Copy and paste the snippet above to the file

3. Switch the placeholder information with your unique address and save the file

4. Drop the file in your root directory for your website

2. Fix Cookie Error in WordPress

WordPress is the Frankenstein monster of CMS: Numerous developers come together to offer a wide array of functions for your website. However, this means that you’re putting together a website with plugins made by individuals who are putting together scripts that may not be compatible with each other.

Some of you may be familiar with the following message:
Error: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.

If this isn’t an error brought on by your web browser, you can use the cookieError.php snippet to say that if it finds the following cookie path, then it’s equal to that path. This overrides the error triggered and is great if you are constantly logging into your website.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file

3. Save the file

3. Hack into WordPress through functions.php

Sometimes you just don’t have the information you need to get into a WordPress website with the front interface. You can use the functions.php to make a WordPress user similar as to how you would make it in the SQL database. It’s especially fast if you already have server access.

How to Implement: To implement this snippet, you need to have access to the functions.php file

1. Copy and paste the snippet to the functions.php

2. Use the credentials to log into /wp-admin to gain access

Recommended Steps After Access

1. Make a user using the normal method of creating an admin with the SQL database
2. Delete the user in functions.php once you have access to the normal SQL database user

4. Insert a Custom Login Logo for WordPress Dashboard

When you log into WordPress, it automatically shows their logo in the admin login page. If you have a lot of people logging into your site, sometimes it’s nice to have your own branding stick out. Using a bulky plugin just to add an image into your website is a waste of valuable resources. Instead, you can use a snippet to achieve the same result.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file

3. Add your background image URL to the snippet

4. Save the file

5. Remove Query Strings from Static Resources in WordPress

If you use website speed checkers, you may see this error: Remove Query String from Static Resources. It wants you to clean up your query strings so your website can be optimized. This should help your website speed and could boost your ranking on Google.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file

3. Save the file

6. Simple .htaccess for WordPress

This handy .htaccess file can be used instead of the one that your host uploads for you (in most cases). They usually have their own server cache, but if you want to keep it as simple as possible, this one will do the job.

How to Implement:

  1. Backup your current .htaccess file

2. Edit a copy of the file and delete the current information

3. Copy and paste the snippet above to the file

4. Save the file

7. Hide Subscription Form from Returning Subscribers

This is a snippet involving a cookie so that you can hide your forms if someone has already subscribed. It’s great because it helps keep your current subscribers happy. Afterall, no one enjoys repetitive information being sent to them. Having a healthy newsletter network is essential in staying competitive in this market.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file

3. Save the file

8. How to Set the First Image of Your Post as Featured

Going back and forth in the dashboard to set your featured images is a tedious process that can be done using a WordPress snippet. This snippet is a quick and easy fix for bloggers. Anything that saves time that could be used on content is useful.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file

3. Save the file

9. Redirect WordPress Users to Different Pages on Login

Redirecting a WordPress user after login can be beneficial in so many ways, especially for marketing purposes. Once they log in, you can direct them to a specific area on your website before allowing them into their usual user portal.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file

3. Save the file

10. Add Additional Image Sizes to your WordPress Options

By default, WordPress allows four image size selections in their UI Thumbnail size (150px square); Medium size (maximum 300px width and height); Large size (maximum 1024px width and height); and Full size (full/original image size you uploaded).

Using the following WordPress snippet, you can add additional sizes to the list. This is helpful because WordPress isn’t adept at handling image constraining, and content can call for deviance from these four sizes.

Your custom image sizes won’t show up in your media settings or your post edit screen, but it will be usable when calling a post thumbnail via PHP.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file 3. Save the file

4. Call snippet using PHP.

11. Add a WordPress Tag Cloud anywhere on Your Site

This is a cool WordPress snippet if you are a blogger because you can get a lot of topics in front of your audience. In this case, it’s more so that you can bypass the use of widget areas. This is more for the creative sphere of development.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file where you want the tag cloud to be displayed

3. Save the file

12. Increase Memory Limit in php.ini

Having your memory allocation set up can save you a lot of effort in the future. Setting it to 300M will ensure that you don’t have to touch it again. If you’re running into issues with the memory limit, then you will have to get more space on your individual server. After using this snippet to clean up your plugin space, that won’t be necessary.

How to Implement:

  1. Go to your php.ini file

2. Find the “WP_MEMORY_LIMIT”

3. Change the number to 300M

4. Save the file

13. Add a Copyright that changes based on the Date

This is such a small touch that most people wouldn’t think about making it dynamic. However, having a footer that automatically displays the year is one way to add a professional edge to your design. It also keeps your website looking up to date without having to change your footer text every year.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file

3. Save the file

4. Place this into your widget area of your footer:

14. Add FontAwesome Icons to your Site

Icons can be a fun way to add an app-like experience to your current website design. Fontawesome is an established library that is versatile and responds well with CSS. Adding Fontawesome to your WordPress website is easy and you can call on the library as needed.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file

3. Save the file

4. Call upon the name of the icons in your code to be able to use the icons

15. Add SVG Compatibility to WordPress

WordPress blocks you from being able to upload SVGs because they can be a security issue. Since the everyday WordPress user doesn’t know how to spot malicious snippets, the developers decided to disable the upload for the greater good of all WordPress users.

If you’re a developer, you will probably want to make some cool SVG animations in your WordPress site, so using this snippet will allow you to upload them without the assistance of a plugin.

How to Implement:

  1. Go to your functions.php file

2. Copy and paste the snippet above to the file

3. Save the file

Takeaway

What makes WordPress a powerful tool is its flexibility with adding functions to its normal framework. There are a ton of snippets available that are extremely practical and this list only begins to dip into what is available. If you go with snippets for functionality before plugins you can ensure that your website has the minimal code to perform quickly.

Need help with implementing WordPress snippets or want to build your website further? We can help.

AKOS works with businesses and nonprofits build their digital presence and systems for maximum impact. Learn more and get in touch with us at akosweb.com/contact

Originally published at akosweb.com on July 24, 2018.

--

--

Aidan Coleman
All Kinds Of Stories

Things I Love: Front-End Web Development and Design, Search Engine Optimization, Cyber Security, Supporting Local Businesses, Petting Cats.