Table Shortcode

Haven’t posted in a while so thought I would post a simple short code I just setup.

This short code will produce a table and has some basic options.

Used like this:

[table classes=”table table-striped”  head=”true”]
Head 1 | Head 2
Row 1 Cell 1 | Row 1 Cell 2
Row 2 Cell 1 | Row 2 Cell 2


You may want to setup a check to make sure your shortcode is not conflicting with other shortcodes.


Admin CPT Meta Value Filter

Today I needed to filter some Custom Post Types (CPTs) by their meta value for a client.

First we need to setup the filter select element.

Next we just need to filter these post types if we are on the right admin page

And done! Now you can filter your CPTs by some specified meta value.


Order Admin Posts by Title

Some clients prefer the admin menu to be sorted alphanumerically by default instead of by menu order. This is all you need.


Build Your Own Cache Feature

Personally I love Quick Cache for setting up caching on websites. Sometimes these cache programs don’t work quite how you would like.

If you really feel like diving in and setting up your own caching feature here is some basic functionality to get you started.

All you need to do to get this started is to load all of these functions. You could go ahead and put it in your funtions.php file but I think it makes more sense to setup a theme support check and just call that in your functions.php file.

Then include a file containing the above functionality with a check for theme support at the top.

The final thing you need to do is just setup a folder to save your cache files to. I created a ‘jb_cache’ folder in the wp_uploads directory which is then called in the code like so.

Now that you have all of the structure in place your caching will be working! This caching works in the following way:

When a user visits the site the code checks to see if that page already has a cache file saved. If so, it displays that file. If no page is found then the code starts an output buffer and then lets WordPress run normally. Then, in the ‘wp_footer’ hook (priority 1000) it ends the output buffer and closes the body and html tags. This output is then saved as a cached file so any other user who visits the page can load this pre generated file.

On the admin side things work like so:

When the ‘save_post’ action is called the code checks to see if there is a cached file for that page and, if so, deletes it.

It sounds super simple, and really, it is. Of course this is the MOST BASIC way to enable caching and is really only the starting point / learning functionality. In order to turn this into production ready code you will need to spend some extensive hours testing and implementing all of the features you want out of a caching program.

Really though…it has all been done before. Unless you REALLY NEED to implement a custom caching feature, or you are just looking to learn about caching, just download one of the many fully developed caching programs. Most of them are really great and have a huge amount of customization options.

Edit Custom User Capabilities

There are many good user role editors (like user role editor and Press Permit among others). However we had a client who needed very specific control over some Custom Post types on a per user basis and they felt that the standard plugins were too confusing (a common problem). So I ended up having to setup an ACF on all users and then add and remove capabilities based on selected permissions…it will be easier to show than explain.

The first step is to create an ACF Checkbox field called User PermissionsScreen Shot 2014-10-02 at 2.21.30 PM

In the “Choices” section just enter in the permissions you want to have and a label for each. (ie member_access : Allow users to add, edit, and delete Members). Make sure to set the location for this ACF to User => is equal to => All (or, I guess, whatever you want).

Next make sure to update your custom post type with a capability:

Now go ahead and put the following function in your functions.php file

Now if you go into a specific user you should see a list of user permissions you defined.Screen Shot 2014-10-02 at 2.31.26 PM

Just check whatever  capabilities you want the user to have and hit “Update User”. Now you should have control over what CPTs each user can see AND it is even easy enough for the technically illiterate to use!


Using Sessions in WP

Sometimes I need to use SESSION variables in WordPress when no user is logged in…ok well I probably do not NEED to use SESSIONs but I do anyway…deal with it. Here is some code to make that possible.