Limit User Role to Certain Posts

There are a variety of plugins out there for managing user role access. User Role Editor (URE) and Advanced Access Manager (AAM) are both good plugins I can recommend.URE is great for…editing roles. The problem with URE is that it doesn’t allow more narrowly scope restrictions…like to single posts. AAM is great at limiting individual posts and hiding admin menus. AAM however requires a LOT of setup if you have a lot of pages / a lot of roles and starts to become unpractical for highly restrictive page editing.

All that said, I ended up turning to good ol’ taxonomies.

First, setup your taxonomy for your posts. In this case I am targeting a “Departments” custom post type.

See jp_register_cpt and jb_register_taxonomy for info on these functions.

Second, setup your roles with URE. I’ll leave this to you, but I usually start with copying either the “Contributor” or the “Subscriber” default roles and going from there. Mine looks like this:

Third, Add a new admin taxonomy in the net taxonomy you just created. Make sure to give it the same slug as the user role ID, this is just for ease of use later and for reference. Example:

Forth, add this function to your PHP file.

All done! Now this user role should not be able to view or edit any posts that do not have this category.

Using TinyMCE outside of WordPress

Recently we had to setup a TinyMCE WYSIWYG outside of WordPress so we couldn’t use the wp_editor function.

TinyMCE has changed a lot since I last used it as a stand alone feature so I wanted to make a post about how to set this up so I don’t have to look around for this info again.

Step number one is setting up the script, like so

Unfortunately you do need to setup an account and get an API key in order to correctly use TinyMCE. You also need to setup the domains you want this script to work on, this is done in your TinyMCE account.

Once we have that in place we can setup our WYSIWYG!

And there we go! That is all you need to get a basic TinyMCE instance going.

NOTE: I had to make sure I had the editor.on(‘change’) function working or else my textarea wasn’t updating quite right. You might not need this for your use case.

TinyMCE also offers additional plugins which are really neat, unfortunately they require paying a monthly subscription 🙁

If you are using TinyMCE in multiple places and NEED those plugins, it would definitely be worth it, but the default functionality is probably good enough for most users.

Get Vimeo Thumbnail

Vimeo updated their API to version 3. In the process they have not only deprecated their previous, simple, API. They have also created a giant unwieldy monster that is unusable by 90% of developers in 90% of cases.

Sure, if you want to build an application around the use of vimeo for hosting your videos, this new API may be worthwhile.

But if you are like 99% of people who just want to display a thumbnail of a video hosted on Vimeo, you are **** out of luck. You need to setup library, and run through a bunch of oAuth bullshit. Dashron over on stack overflow seems to be the lead developer for this API and I am sure he had his reasons. I just can’t fathom what they could be.

ANYWAY, angry rant aside I found a workaround for this on Brian Coords website. The following code is his, but I needed to save it here for my future use since there is no way I will ever setup an entire library in a project just to get a thumbnail.

Now to use this function you simple have to pass it the embed URL of your video, like this.

This should return the URL of your thumbnail which you can stick directly into your image src.

Thanks Brian!

Basic Breadcrumbs

Recently we made a site that needed breadcrumbs but was used as an internal portal. This means it didn’t need any SEO stuff so the standard Yoast breadcrumbs we use seemed like overkill.

Decided to create a couple simple functions to display our breadcrumbs.

By sticking those two functions in your functions.php file and then calling jb_breadcrumbs() wherever you want to display these breadcrumbs you should be all set.

ACF Convert Old PHP fields to JSON

Back in the day, if you wanted to make sure you could keep your environments in sync (and you couldn’t just be moving the database around) you had to use PHP to build and update your ACF forms. It was gross.

Nowadays you can just work with the ACF fields in the admin like normal and ACF will AUTOMAGICALLY build a json form you can use to sync your different environments.

You do need to set this up a little bit, so stick this in your functions.php file to get started.

Now the tricker part is going to be converting your old PHP.

I just setup a script like the one below and ran it once. This script is taken from David Egan’s site.

All you need to do now is copy the output from this script into it’s own file (or you could just have PHP write that file for you) and save it as a .json.

IMPORTANT NOTE: At this point this json file probably wont work. I had to go through the file and double quote all of the keys and fix up some other formatting issues. This was 99% completed by just doing a single find and replace in Visual Studio Code. Regex: /”\s\w+:”/ This should find MOST of your keys and correctly format them so ACF can import them. This wont work PERFECTLY so you will need to scan your file to make sure everything is formatted right, but it will get you 99% of the way there.

Good Luck!

 

Show Post Date Instead of Freshness

BBPress shows the “Freshness” of a post by default. This is fine and will work for most people, but some clients want to show the actual date of a forum post.

Here is the code for that.

Thanks to user Robin who posted this function to the BBPress forums here: https://bbpress.org/forums/topic/freshness-date-format/

I modified it slightly since it wasn’t working quite right in my testing.