Wordpress How To's, Snippets, Hooks & Lifesavers

Welcome to Easy Wordpress Code. I collect code snippets during my work and post them here for your and my usage. If you need a custom code that don't appear hear, feel free to post your request in our forums.

WordPress: Add Rel Prev Next Meta Tags For Custom Query Loop

When we use custom query loop in wordpress, we lose SEO befenits such as the rel prev/next meta tags that’s in the <head> and improves how Google reads our website. This code will read the custom query pagination and will display those tags in the head section of the pages. Paste this into your theme’s […]

Contact Form 7: Add Country By IP Shortcode To Email

Copy this code to your theme’s functions.php and use [country_by_ip] shortcode/tag ANYWHERE to print the visitor’s country by his IP address with CF7. It uses ipinfo.io’s free API, detects the visitor’s IP address and adds CF7 shortcode/tag to use in the email body for example. Enjoy!

3rd Party Integration: Add Geolocation To Service Posts

Copy this code to your theme’s functions.php and each call your ‘3rd party integrations’ plugin will make after a visitor submission (For CRMs API, for example), it will include the Country, City & State of the visitor by his IP address.  

Contact Form 7: Overwrite/Manipulate Posted Data Before Sending Email

Copy this code to your theme’s functions.php to manipulate the posted data by the visitor, update, add or remove fields and such. There’s a lot that you can do with it!

How To Add Checkbox To Checkout in WooCommerce

This is how you add a checkbox to checkout in Woocommerce. The Checkbox will appear in the checkout is not required. Add this code to your theme’s functions.php file: You may also like: Add Checkbox To Woocommerce (Required Checkbox)

WPML: Fix Duplicate Hreflang Tags

Paste this into your theme’s functions.php file to prevent from Hreflang tags to appear on the same current language and fix duplicates.

WooCommerce: Hide Tabs On Single Product Page

Add this to your theme’s function.php to hide all product tabs on a single product page:

WooCommerce: Show Contact Form When Product Is Out Of Stock

Paste those into your theme’s functions.php and edit the contact form shortcode inside do_shortcode function.

Add NoFollow (rel=”nofollow”) To WordPress Tags Cloud

This is how you add rel=”nofollow” to tag links in the tag cloud using wp_tag_cloud(). Add it to your theme’s functions.php.

Remove HREF attribute from Empty Anchor Link on WordPress Menu

Add the following code to your theme’s functions.php file to remove the HREF attribute from all the links that link to “#”, Basically can be very useful if you’re using submenus and you want the parent item unlinked, for example.

Load Font-Awsesome Non-render blocking

Solved: Eliminate Render Blocking Fonts, Javascript & CSS in Google Lighthouse Speed Test

Here’s the solution for eliminate render blocking fonts, javascript and css and load non-render blocking resources warning in Google Lighthouse Speed Test. Place this code as higher as possible in your theme’s header.php, after the “<head>” tag. In this example we load 2 font families: Heebo and Assistant. Use this if you need to load […]

WooCommerce: Add a Required Checkbox Field On Checkout

This is how you add a checkbox to checkout in Woocommerce. The Checkbox will appear in the checkout and will not let the customer to proceed unless the checkbox is checked. See also: How to add checkbox to checkout in woocommerce (not required) Add this code to your theme’s functions.php file:

Integrate HasOffers (By Tune) With WooCommerce

In this post I will include my code to track, store and pass HasOffers (affiliate id + transaction id + amount of order) data from the the first page, through the order details and back to HasOffers when the purchase is successfull. I will divide the process to a few steps: Track the Transaction ID […]

WooCommerce: Don’t Add Product To Cart If Already Exists

Put it in your theme’s functions.php. Whenever your visiot will try to add a product twice mistakenly, it will keep only 1 of this product in his cart. However, he will be still able to change the quantity of the single product he has in cart.

Prevent Contact Form 7 (CF7) From Sending Email

Put it in your theme’s functions.php and update the form id (999999) to your CF7’s form ID.

Disable Auto Check of “Ship to different address” Checkbox on Checkout

Put it into your theme’s functions.php:

WooCommerce: Hide Coupon On Checkout If Already Exists

This code will disable coupon on cart if it’s already applied.

Search WordPress Posts By Custom Term Name (Category/Tag/Any Taxonomy)

Copy this code to your theme’s functions.php and it will return posts in search result also by their category/tag name.

Add Post Featured Thumbnail To Post List on WordPress

Adding a featured image for each post is important for your website’s beauty & very much for the SEO part. To make sure all posts have a featured image, use this code to add a thumbnail of the image to the post list in your wp-admin:

Disable A HREF Link With CSS ONLY!

Disabling a link can be sometimes a dirty task to do. Sometimes we need to do that only temporarily and we have a cute solution for that. Add this css attribute to disable a link, but note – this link will still be crawlable by crawl engines. For example see the link ahead of this […]

Add Facebook/Analytics/GTM Pixels To Your Website With No Plugins!

There are many plugins that will let you add Facebook, Google Analytics, Google Tag Manager or any other data collector pixel to your website with ease. But instlling a plugin for every small task will cause your website become heavy very quickly and that’s something you want to prevent. With this code you can add […]

Integrate With MailChimp API With This Simple PHP Code

Integrating your website with MailChimp is easily done with this code. You can connect it with any form that’s processed with PHP.

Change Category/Taxonomy Archive Page Title

Customizing the archive/category page can be very useful if you want to take your website one step further by fine tuning every phrase just like you want. This code is pretty easy and can be developed in many more ways. In this example we customize category page title and another title for taxonomy page under […]

Add Your Custom Logo to WordPress Admin Login Screen

Changing the WordPress logo in the login screen is pretty cool when you build a website to another client. Placing his logo on the login screen will give him a better feeling, like ‘wow, they REALLY thought about us’. Seeing the WordPress logo isn’t always cool, and there’s nothing to lose with this snippet. Place […]

Disable WordPress Admin Black Bar

Disabling Admin Bar can be done in 2 methods: Method #1: Update specific user profile And then uncheck the “Show Toolbar when viewing site” option & Save (that solution will apply only on your user account). Method #2: Disable for all users By pasting this code into your theme’s functions.php:

Remove ?Ver Querystring Parameter From Static Resources with WordPress

The following code example will remove all “?ver=” querystring parameters from static resources such as JS or CSS files, and may cause you issues with caching those. Just place it in your theme’s functions.php and it’ll do:

Change Any Button, Input & Text With 1 Function (Also For WooCommerce)

How many times did you encounter a “Add to Cart” button or “View Cart” button on your WooCommerce website and you wanted to change it to something else, and didn’t know how? In this code snippet we can change any string we want (as long as it’s not a string with variables, it may be […]

WordPress Error Log 3 Code Lines Alternative with PHP

Debugging WordPress error can be sometimes a tough mission. For that, we can use that simple code to create error log file in the website’s root. Paste this into your wp-config.php: Once an error is occurred, it will create the error log file named error_log.txt with a full report of that error. Warnings will also […]

Create A New WordPress Page Template

Creating a New Page Template requires us to do 4 things in order to make things work, but first we’ll create a new .php file in the theme directory (/wp-content/YOUR-THEME/), such as page-homepage.php. Specify a page template name. Include the header Start the WordPress loop on the desired page & print the content Include the […]

How To Add/Edit/Remove Custom Posts Columns In Posts Screen

This snippet is one of the most useful I’ve ever used. It allows you to fully customize the shown columns in the posts screen, whether it’s a regular ‘post’ type or a custom one. Adding a new column Step 1/2 – add the column to the column list & set its label Line 1:┬ámanage_post_posts_columns = […]

How To Get The Real Visitor’s IP With PHP

The following function will return the visitor’s IP address: Usage:

Change WordPress Website URL (from wp-config.php)

Sometimes you need to change your website’s url to a new url, it could be for many reasons but we won’t dwell on that right now. The thing is, that sometimes you can find yourself ‘stuck’ outside of your /wp-admin/ area and that’s, how to say, sucks. One way to do that is to add […]