You may have been aware that, during the past year, I have been plunging somewhat headlong into WordPress development and making a few connections within the community. It’s been exciting and fun, and to be honest, a little addictive. I’ve learned that turning a concept to code can prove to be a complex task. There is always a problem to solve, and the mathman in me seems to thrive on that. For some time, I’ve considered the possibility of posting a blog now and then about my coding exploits. I’m certain I’ve tallied well over a thousand google searches for code snippets, tutorials, tips, and code definitions. So, here we go. I figure this may be a good place to start. Here’s a simple solution for a simple problem. (Note to family & friends: the rest of this post is technical.)
I’ve been registering a good number of custom post types on the World Mission Prayer League site and have begun managing access to these via the Members plugin by Justin Tadlock. After manually and repeatedly adding capabilities to the roles, I determined I needed to automate the process. Thus, the following few lines of code. This should soon be has been released to the plugin repository as Members: Caps Lock. Enjoy!
/** * Plugin Name: Members: Caps Lock * Description: Adds registered CPT capabilities to Members plugin capabilities list * Author: UaMV * Author URI: https://vandercar.net * Version: 1.0 * License: GPL2+ */ // add caps to members plugin list function capslock_add_capabilities( $capabilities ) { // get CPTs as objects $post_types = get_post_types( array( '_builtin' => FALSE ), 'objects' ); foreach ( $post_types as $post_type => $post_type_object ) { // push each CPT capability to members capabilities foreach ( $post_type_object->cap as $meta_cap => $cap ) { $capabilities[] = $cap; } } // get custom taxonomies as objects $taxonomies = get_taxonomies( array( '_builtin' => FALSE ), 'objects' ); foreach ( $taxonomies as $taxonomy => $taxonomy_object ) { // push each taxonomy capability to members capabilities foreach ( $taxonomy_object->cap as $meta_cap => $cap ) { $capabilities[] = $cap; } } return $capabilities; } add_filter( 'members_get_capabilities', 'capslock_add_capabilities' );
This, along with taxonomy caps, has long been on my list to add to the plugin. Of course, my laziness with writing the code makes for new and awesome add-on plugins like this one. 🙂
By the way, you can probably get by with a single filter and the
get_post_types()
function.Ah, good call. Taxonomy caps … how did I overlook them? I did have a workaround for them, as well and will have to add. I thought of submitting a contribution to Members, but seemed to have forgotten that thought, too. I did just submit this to the repository, but feel free to add it to Members, if you see fit. I’m just having fun here. 🙂
Edited … and better.
look at that!