Javascript Sorting, Summing, and Rounding

I was running into an instance where I needed to be able to sort some arrays in either ASC or DESC order. I found some nice functions to help with that.

Example Usage:

 

Calculate Age / Date Masking

Javascript is kind of wonky about their dates. Here is a simple function to calculate age based on a given date string.

Note: This is not perfect and does not work with every possible date string, but it works pretty well with all standard US date formats.

Here is some decent code to setup some simple date masking.

 

Force Same Height on All Slick Slides

I love the slick slider, but sometimes the default functionality isn’t quite what I need. For a variety of other reasons the height of the slides can vary from slide to slide There is no built in “force height” option. Although it should be possible using “display: flex”, I try to avoid using flex on dynamic or complex elements that are hard to fail gracefully on.

I found some good code on the forums to force the slick slides to share the same height. I hope it helps you in the future too!

 

 

Highlight and Locate Search Terms

Sometimes we have clients that want to give their users a little extra help in finding the search terms they are looking for.

Here is some basic code you can through in your functions.php file to add highlighting to a text string (in my case that is search term).

Obviously, in order for this to work you will need to setup a $_GET variable, probably attached to the link on your search page. That will look something like this:

You will probably want to also setup a background color for your highlight so it is, you know, highlighted:

Lastly, you can also move the user to the first instance of this search term like so:

List Woocommerce Cart Item Attributes

90% of the time I am very thankful for plugins like Woocommerce, that handle so much of the architecture associated with an e-commerce site.

10% of the time I curse their name in the more profane manner possible. This seems to happen most often when using variable products.

For reasons that defy reason, Woocommerce does not list the variable product’s attributes when it is in your cart. There is also no Woocommerce function, afaik, to easily list a cart items selected attributes. Why on earth would someone want to know what type of product they put in their cart! Bah!

Anyway, here is some simple code to list the attributes for the variable products in your cart:

Hopefully that will save you 1% more time of cursing the name Woocommerce!

Sort Posts in Admin by Meta Value

Sometimes we need to display some meta information of a post in the admin listing. Usually it is handy to also make this information sortable so it is more useful to the client.

Here is some example code of how that is done.

You will need to update the ‘post-type’ to the post type you want to edit and the ‘my_meta_key’ to the meta key you want to sort by.

Big thanks to Rachel Carden over at wpdreamer.com.