Add a custom field to every public post types in WordPress with Advanced Custom Fields

Add a custom field to every public post type in WordPress by pasting this to your functions.php. Make sure you have Advanced Custom Fields Pro installed and activated. The custom fields will appear as a Option Page in WordPress admin.

 

Print the custom field on the archive page by adding this to the template file.

 

Custom logo in WooCommerce Storefront

Add this to your functions.php in your theme or plugin to add a custom logo in WooCommerce Storefront. Make sure to change the image source to what ever you want.

 

Glances – a cross-platform system monitoring tool for Ubuntu Server

Glances is a free software (licensed under LGPL) to monitor your GNU/Linux or BSD operating system from a text interface.

Installation:

screenshot-wide

Secure your Ubuntu Server

First thing to do when setting up a Ubuntu Server is to make sure it’s secure. Install fail2ban to make sure you block IP-addresses that try to break in, and log everything with logwatch.

Update the system.

 

Install fail2ban.

 

Copy the configuration file.

 

Edit the file.

 

Change the ban time from 600 (10 minutes) to 604800 (7 days) and CTRL + O to save and CTRL + X to exit.

 

Install logwatch.

 

Edit logwatch daily cron to email you the log.

 

Add this line at the bottom and replace the example email with your own. Save with CTRL + O and exit with CTRL + X.

 

Generate custom XML sitemap on post save in WordPress

It’s not pretty, but it works. The script generates a custom XML sitemap on post save in WordPress. It’s looping through every page for every post with the post type “city” (since it’s unique content based on what city you choose). Remember to also use the URL parameter tool in Google Search Console to make the crawlers index the pages correctly. Since we use Polylang, the script will create a sitemap for each language.

Don’t forget to create the folder “sitemaps” in your theme folder and make it writable.

 

Disable payment gateway for specific user role in WooCommerce

Disable a specific payment gateway in WooCommerce by adding this to your custom plugin or function.php in your theme. You can also add multiple user roles with the plugin Members and change payment gateway for different roles.

 

Force HTTPS in htaccess and wp-config.php

Force HTTPS in WordPress by adding this to your htaccess-file, before the line “# BEGIN WordPress”.

 

Also, add this to your wp-config.php. FORCE_SSL_ADMIN is there to make sure there’s no redirect loop in wp-admin.

 

Automatically start MySQL if apache kills the process or sacrifice children on Ubuntu Server

There are several ways to solve the problem of Apache running wild and killing MySQL. It the problem occurs very infrequently, there’s a easy way of restarting MySQL with the use of the crontab. The script even sends a notification email so you know what just happened.

First, SSH into your server:

Create a script file:

Paste the following:

Press CTRL + O to save, and CTRL + X to exit.

Give execute permission to your script:

Edit the Crontab:

Paste the following:

Press CTRL + O to save, and CTRL + X to exit.

To see if it works, type the following:

In case the service doesn’t start, just start it manually with the following:

 

Remove query strings from static resources in WordPress

Remove query strings from static resources in WordPress by adding this to your functions.php. You can also change the substring “?ver” to anything else you want to remove.

 

Featured image URL from Post ID in WordPress

Get featured image url from post id in WordPress, for when you’re to lazy to google it. This includes a custom image size as well.