Refactoring Sites – WordCamp Baltimore 2017

Slides and more information from my talk at WordCamp Baltimore 2017, on Refactoring Sites.

Slides

Audit Checklists

These are not meant to be exhaustive but rather a starting point to take and expand or remove from.

Client/Admin Audit Checklist:

  • Where is the site hosted?
  • Is DNS at the host, registrar, or elsewhere?
  • Are there any custom DNS settings? (mx records for email, subdomains/A records at other hosts)
  • What 3rd party tools are being used? (Email collection, analytics, a/b testing, heatmaps)
  • Are there content managers? Editorial calendars? What does the posting schedule look like?
  • Any back office tools or systems integrated with the website? (stock management, accounting, employee portals, etc)

Development Audit Checklist:

  • What updates need done? (plugins, themes, WordPress core)
  • Is active theme a custom or child theme?
  • Analyze and examine theme:
    • Does the template structure follow the proper hierarchy? (wphierarchy.com)
    • Overridden templates (any overridden templates for a plugin like WooCommerce or Events Cal Pro)
    • Functions are organized and make sense
    • Nothing alarming or stands out as a serious needs to be addressed asap issue
  • What plugins are running and can some be removed or replaced with custom functionality?
    • If there are a lot you can use get_plugins┬áto spit out a list of all plugins (codex)

Sort Posts in Multidimensional Array by ACF Repeater Field Value

If you have a collection of posts that need sorted or filtered by a repeater field, this can be a bit of a pain to query for. The normal WP_Query meta_query arguments won’t work here.

To visualize this, lets imagine you have a custom post type of Trips. Inside Trips, you have repeating fields for Dates of each trip and a checkbox if it should be Featured.

If you want to show ALL Trips that are marked as Featured and sorted by the Date of each trip, you’ll need to build an array with all your post_meta then output them via a loop.

Here’s what we need to do:

  • Create an empty array to hold our post data
  • Create a new WP_Query with any arguments we require
  • Start the loop
  • Loop through the repeater fields
  • Optional: I added an if statement to filter by Featured only
  • Add our post data to our array using key => value format
  • Sort out data by date
  • Output via foreach loop

A word on sorting in PHP

There are several built-in functions to sort, the best resource for quickly knowing which sort function to use is this chart:

Props TutsPlus (https://code.tutsplus.com/tutorials/working-with-php-arrays-in-the-right-way–cms-28606)

Some helpful links to further reading about sorting options in PHP:

The full code below shows an example of a basic custom sort, which we can pass to usort if none of the pre-defined PHP sorting functions fit the bill.

You could achieve the same results with asort in this case, replacing both usort() and subval_sort function with just asort( $post_data );

Here’s the full code:

Using .htaccess to block folders and subdomains

There are a lot of ways to protect and lock down subdomains, folders, and even specific files. The preferred, reliable, and easiest way to do this is using .htaccess and .htpasswd files.

Block All, Except IPs you set

Order Deny,Allow 
Deny from all
Allow from 111.222.333.444   # Single IP
Allow from 111.222.333.0/99  # Range from .0 to .99

Set a Username and Password

To set a login and block everything else on a folder or subdomain, you need two files.

.htaccess tells us what type of Authentication and where to look for it.

.htpasswd stores the username and password the .htaccess looks for. It can also store multiples if you want to give users their own unique login.

.htaccess file:

AuthType Basic
AuthName "Protected"
AuthUserFile /my/path/to/.htpasswd
Require valid-user

.htpasswd file:

here is an example of username: user, password: password

user:$apr1$ZuZXm5r4$UqiQ7QOz.kc47U/HTVNWa0

You’ll need to generate your password for these, I recommend the generator from htaccesstools.com

Further Reading

You can do a lot with .htaccess, if you want to dive deeper here are some links: