Helpful Code Snips

Introduction

Sometimes we need to use small snips of code to accomplish certain functionalities because it is either the only option or it’s a cleaner approach than other methods. This is often done by adding code to our active themes functions.php file, which is essentially a theme-specific plugin file. Code is only executed here if the theme is active or a child theme that relies on it is active.

Many of these code snips rely on placement in your active themes functions.php file or another file directly referenced by it via a require() or require_once() PHP function, some are commands to be run in Linux, Unix or Mac OSX. Most of these code snips can also be placed in your custom plugins. If you are not sure what you’re doing, placing these code snips at the end of your active themes functions.php is your best bet for success, placing the code anywhere else in the file could lead to errors or incorrect/unwanted results. Make sure to remove any closing PHP tags ?> on the last line of your functions.php, they’re unneeded.

IMPORTANT: Before making any code changes, backup all files and databases.


Description

Accidents happen, passwords are lost or forgotten or sometimes we’re given a site to work on with insufficient access or the client is unaware of how to give more access. The code below will allow us to create a new admin user with only access to FTP or ssh.

How to Implement
  1. Login to the DocumentRoot of the website
  2. Navigate to /wp-content/themes/your_active_theme/functions.php and download this file locally
  3. Make a backup of this file in another local directory
  4. Open functions.php in a text editor and scroll to the bottom, make sure to remove any closing PHP bracket ?>
  5. Add the code below to the bottom of functions.php
  6. Upload and overwrite/replace the functions.php with the one we just edited
  7. Reload the main website, this will run the code the functions.php
  8. Login with the new credentials: https://example.com/wp-login.php
  9. After successful, replace the functions.php with the backup we make in Step 3 above

Example Code:

Description

The WordPress post/page visual editor (TinyMCE) strips out various HTML tags and attributes that are seen as unhelpful, bad practice or security risks such as: iframe, some table style properties, and other HTML attributes. The code and steps below will disable this filter, allowing you to use these.

How to Implement
  1. Login to the DocumentRoot of the website
  2. Navigate to /wp-content/themes/your_active_theme/functions.php and download this file locally
  3. Make a backup of this file in another local directory
  4. Open functions.php in a text editor and scroll to the bottom, make sure to remove any closing PHP bracket ?>
  5. Add the code below to the bottom of functions.php
  6. Upload and overwrite/replace the functions.php with the one we just edited
  7. Reload the main website, this will run the code the functions.php
  8. Login, go to the page or post edit screen and test by pasting code with a previously stripped HTML tag and switch between the text and visual editor tabs

Example Code:

Description

If you have the need to make sure certain plugins are always kept up to date, or to make sure others are not updated automatically, you can define them in this code snip below. In our example we’re using Akismet and BuddyPress as the plugins we’d like to keep up to date as soon as there’s a new release.

How to Implement
  1. Login to the DocumentRoot of the website
  2. Navigate to /wp-content/themes/your_active_theme/functions.php and download this file locally
  3. Make a backup of this file in another local directory
  4. Open functions.php in a text editor and scroll to the bottom, make sure to remove any closing PHP bracket ?>
  5. Add the code below to the bottom of functions.php
  6. Upload and overwrite/replace the functions.php with the one we just edited
  7. Reload the main website, this will run the code the functions.php
  8. It’s best to test this when you know for a fact there is an update to a specific plugin you want kept up to date, otherwise you’ll be waiting until there is.

Example Code:

Description

Often, people seeking to do your site harm with infect it with base64-encoded PHP which obfuscates the true purpose and intent of the code.

How to Implement
  1. Copy the code into a file in the DocumentRoot of your WordPress install on your server and call it base64-hunter.sh
  2. Once that’s done: chmod +x base64-hunter.sh
  3. Next, ./base64-hunter.sh then hit enter
  4. You should be left with two files: base64-detections.txt & eval-detections.txt
  5. With this information you can isolate and clean infected files
  6. You can also simply copy each command and run them manually if you prefer
  7. Use man find & man grep to learn more about what’s happening with these commands

Example Code:

Description

You may have the need to hide the page editor from unskilled editors on occasion only for certain pages, use the code below to do so.

  1. Login to the DocumentRoot of the website
  2. Navigate to /wp-content/themes/your_active_theme/functions.php and download this file locally
  3. Make a backup of this file in another local directory
  4. Open functions.php in a text editor and scroll to the bottom, make sure to remove any closing PHP bracket ?>
  5. Add the code below to the bottom of functions.php
  6. Upload and overwrite/replace the functions.php with the one we just edited
  7. Reload the main website, this will run the code the functions.php
  8. Login and visit the post/page editor to verify the changes
    1. Example Code:

Description

This script will backup your servers /etc, /var/log, /var/www, /var/lib/mysql as well as sync your time and timezone settings, provide detailed lists of installed software and backup pruning. This script was written for Red Hat Linux, Centos or Fedora in mind but can easily be adapted to your Linux distro by changing the relevant file and directory paths. Feel free to edit it and make it your own.

How to Implement
  1. vi /root/serverbackup.sh paste and edit script below
  2. esc :wq exit vi
  3. chmod +x /root/serverbackup.sh mark as executable
  4. vi /etc/cron.daily/serverbackup paste the next line into this file (step 5)
  5. bash /root/serverbackup.sh > /dev/null 2>&1 this code will run once everyday
  6. chmod +x /etc/cron.daily/serverbackup
  7. esc :wq
Testing
  1. /root/./serverbackup.sh


Focus

leica-square
 
microtec-square
 
zeiss-square
nikon-square
 
meiji-square
 
olympus-square

 

Morph

leica-square
 
microtec-square
 
zeiss-square
nikon-square
 
meiji-square
 
olympus-square

 

Tilt

leica-square
 
microtec-square
 
zeiss-square
nikon-square
 
meiji-square
 
olympus-square

 

Shrink

leica-square
 
microtec-square
 
zeiss-square
nikon-square
 
meiji-square
 
olympus-square

 

Grow

leica-square
 
microtec-square
 
zeiss-square
nikon-square
 
meiji-square
 
olympus-square

 
 

Add nice sortable column with last modified dates/times for posts and pages in the wp-admin post/page editor main screen.
  1. Login to the DocumentRoot of the website
  2. Navigate to /wp-content/themes/your_active_theme/functions.php and download this file locally
  3. Make a backup of this file in another local directory
  4. Open functions.php in a text editor and scroll to the bottom, make sure to remove any closing PHP bracket ?>
  5. Add the code below to the bottom of functions.php
  6. Upload and overwrite/replace the functions.php with the one we just edited
  7. Reload the main website, this will run the code the functions.php
  8. Login and visit the post/page editor to verify the changes

Description

WordPress automatically converts some ASCII characters into Emoji’s which may interfere with your intended use, use the code below to remove this.

  1. Login to the DocumentRoot of the website
  2. Navigate to /wp-content/themes/your_active_theme/functions.php and download this file locally
  3. Make a backup of this file in another local directory
  4. Open functions.php in a text editor and scroll to the bottom, make sure to remove any closing PHP bracket ?>
  5. Add the code below to the bottom of functions.php
  6. Upload and overwrite/replace the functions.php with the one we just edited
  7. Reload the main website, this will run the code the functions.php
  8. Login and visit the post/page editor to verify the changes


Sometimes you want to create an offline copy of a site that you can take and view even without internet access. Using wget you can make such copy easily:

wget --mirror --convert-links --adjust-extension --page-requisites --no-parent http://example.org

Explanation of the various flags:

  • –mirror – Makes (among other things) the download recursive.
  • –convert-links – convert all the links (also to stuff like CSS stylesheets) to relative, so it will be suitable for offline viewing.
  • –adjust-extension – Adds suitable extensions to filenames (html or css) depending on their content-type.
  • –page-requisites – Download things like CSS style-sheets and images required to properly display the page offline.
  • –no-parent – When recursing do not ascend to the parent directory. It useful for restricting the download to only a portion of the site.

Alternatively, the command above may be shortened:
wget -mkEpnp http://example.org
Note: that the last p is part of np (–no-parent) and hence you see p twice in the flags.

Also see: http://www.httrack.com/ website mirroring tool.

Description

WordPress blocks certain mimetypes as “harmful” or security risks by default, use the working example code below to alter this behaviour, edit as needed.

  1. Login to the DocumentRoot of the website
  2. Navigate to /wp-content/themes/your_active_theme/functions.php and download this file locally
  3. Make a backup of this file in another local directory
  4. Open functions.php in a text editor and scroll to the bottom, make sure to remove any closing PHP bracket ?>
  5. Add the code below to the bottom of functions.php
  6. Upload and overwrite/replace the functions.php with the one we just edited
  7. Reload the main website, this will run the code the functions.php
  8. Login and visit the post/page editor to verify the changes

Description

This is useful for something like a membership or subscription site, where you’re selling only one of several choices. For example: Brone, Silver, Gold, etc. This function prevents users from adding 2 Silver account or 1 of each, etc, so they are only allowed to buy one membership level at a time, per user.

  1. Login to the DocumentRoot of the website
  2. Navigate to /wp-content/themes/your_active_theme/functions.php and download this file locally
  3. Make a backup of this file in another local directory
  4. Open functions.php in a text editor and scroll to the bottom, make sure to remove any closing PHP bracket ?>
  5. Add the code below to the bottom of functions.php
  6. Upload and overwrite/replace the functions.php with the one we just edited
  7. Reload the main website, this will run the code the functions.php
  8. Login and visit the post/page editor to verify the changes

Last Modified: 5 Jun, 2017 at 16:52:00