Skip to content

Instantly share code, notes, and snippets.

@farleyta
Created February 8, 2014 10:15
Show Gist options
  • Save farleyta/8881462 to your computer and use it in GitHub Desktop.
Save farleyta/8881462 to your computer and use it in GitHub Desktop.
A bunch of functions to allow a custom Editor-like role for managing a specific Custom Post Type in Wordpress.
<?php
// Register Region CPT
add_action( 'init', 'register_cpt_region' );
function register_cpt_region() {
$labels = array(
'name' => _x( 'Regions', 'region' ),
'singular_name' => _x( 'Region', 'region' ),
'add_new' => _x( 'Add New', 'region' ),
'add_new_item' => _x( 'Add New Region', 'region' ),
'edit_item' => _x( 'Edit Region', 'region' ),
'new_item' => _x( 'New Region', 'region' ),
'view_item' => _x( 'View Region', 'region' ),
'search_items' => _x( 'Search Regions', 'region' ),
'not_found' => _x( 'No regions found', 'region' ),
'not_found_in_trash' => _x( 'No regions found in Trash', 'region' ),
'parent_item_colon' => _x( 'Parent Region:', 'region' ),
'menu_name' => _x( 'Regions', 'region' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'region',
'map_meta_cap' => true // as per http://codex.wordpress.org/Function_Reference/register_post_type
);
register_post_type( 'region', $args );
}
/************************
*
* Regional Admin user role stuff
*
***********************/
// Create the new user role for Regional Admin
function gpi_create_region_admin_role() {
$region_admin_caps = array(
'read' => true,
'read_region' => true,
'edit_regions' => true,
'edit_published_regions' => true,
'read_post' => true,
'delete_posts' => true,
'delete_published_posts' => true,
'edit_posts' => true,
'edit_published_posts' => true,
'publish_posts' => true,
'moderate_comments' => true,
'upload_files' => true,
'level_1' => true //http://stackoverflow.com/questions/6330008/wordpress-custom-users-not-appearing-in-author-box
);
// Create a New Role for Regional Admin
add_role( 'region_admin', 'Regional Admin', $region_admin_caps );
}
add_action( 'admin_init', 'gpi_create_region_admin_role' );
// Give Administrators and Editors All Region Capabilities
function add_region_caps_to_admin() {
$caps = array(
'read',
'read_region',
'read_private_regions',
'edit_regions',
'edit_private_regions',
'edit_published_regions',
'edit_others_regions',
'publish_regions',
'delete_regions',
'delete_private_regions',
'delete_published_regions',
'delete_others_regions',
);
$roles = array(
get_role( 'administrator' ),
get_role( 'editor' )
);
foreach ($roles as $role) {
foreach ($caps as $cap) {
$role->add_cap( $cap );
}
}
}
add_action( 'admin_init', 'add_region_caps_to_admin' );
// Add ability to edit/delete image attachments (without needing ability to edit Posts)
function gpi_fix_media_permissions() {
global $wp_post_types;
$wp_post_types['attachment']->cap->edit_post = 'upload_files';
$wp_post_types['attachment']->cap->delete_posts = 'upload_files';
$wp_post_types['attachment']->cap->edit_posts = 'upload_files';
}
add_action( 'admin_init', 'gpi_fix_media_permissions' );
// Small utility function for returning the Region page object for a user
function get_region_id_by_user( $user_id ) {
$user_regions = array();
/* Custom Query for Finding Posts by this User */
$users_posts_args = array (
'author' => $user_id,
'post_type' => 'region',
'posts_per_page' => '-1'
);
$users_posts_query = new WP_Query( $users_posts_args );
while ($users_posts_query->have_posts()) : $users_posts_query->the_post();
global $post;
$user_regions[] = array(
'user_region_ID' => get_the_ID(),
'user_region_title' => get_the_title(),
'user_region_slug' => $post->post_name
);
endwhile;
wp_reset_query();
wp_reset_postdata();
return $user_regions;
}
// Small utility function for getting current User's role
function gpi_get_user_roles() {
global $current_user;
$user_roles = $current_user->roles;
return $user_roles;
}
// Small Utility function to see if current user is a specific role
function gpi_is_current_user($user_role) {
$current_user_role_array = gpi_get_user_roles();
if ( in_array($user_role, $current_user_role_array) ) {
return true;
}
else return false;
}
// Small Utility function to grab the Region(s) for the current author
function gpi_get_associated_region( $current_author ) {
// Get the Region associated with the Author
$associate_region_obj = get_region_id_by_user($current_author);
// Create an empty array to store the values as strings
$associated_regions = array();
foreach ($associate_region_obj as $region) {
// Convert the region ID to string and add to the new array
$associated_regions[] = strval($region['user_region_ID']);
}
return $associated_regions;
}
// Customize the Admin bar for Regional Admin
function gpi_admin_bar_render_user() {
global $wp_admin_bar;
$current_user = wp_get_current_user();
// A way of checking if current user is a Region Admin (and they aren't Admin)
if ( gpi_is_current_user('region_admin') ) {
$wp_admin_bar->remove_menu('new-content');
$wp_admin_bar->remove_menu('comments');
}
}
add_action( 'wp_before_admin_bar_render', 'gpi_admin_bar_render_user' );
// Remove some items from Admin menu (left column) for Regional Admin only
function gpi_admin_menu_render() {
// A way of checking if current user is a Youth Group (and they aren't Admin)
if ( gpi_is_current_user('region_admin') ) {
// Submenu for region
remove_submenu_page( 'edit.php?post_type=region', 'post-new.php?post_type=region' );
// Main menu for any CPTs that aren't Posts (because the capability
// is set as 'post' for many of these CPTs, they show up for the
// Regional Admin role because of thier ability to edit_post)
// First, get all post types with the post capability
$post_types = get_post_types( array('capability_type' => 'post') );
// Then, loop through the list and hide all menus (except Posts themselves)
foreach ($post_types as $post_type) {
remove_menu_page( 'edit.php?post_type=' . $post_type );
}
//In addition, hide the Contact menu option
remove_menu_page( 'wpcf7' );
}
}
add_action( 'admin_init', 'gpi_admin_menu_render' );
// Hide some menu items from Regional Admin users, not from admins – with CSS
function gpi_hide_region_admin_menu_items() {
if ( gpi_is_current_user('region_admin') ) {
echo '<style type="text/css">#adminmenu li.menu-icon-dashboard,#adminmenu li.menu-icon-media{display:none;}</style>';
}
}
add_action('admin_head', 'gpi_hide_region_admin_menu_items');
// Change the default redirect location after login
function gpi_custom_login_redirect($redirect_to, $request, $user){
//is there a user to check?
if( !empty($user->roles) && is_array($user->roles)){
//check for admins
if(in_array("administrator", $user->roles)){
return home_url("/wp-admin/");
} elseif(in_array("region_admin", $user->roles)) {
return home_url("/wp-admin/edit.php?post_type=region");
} else {
return home_url();
}
}
}
add_filter("login_redirect", "gpi_custom_login_redirect", 10, 3);
// And hide that tiny "Add New" button from next to the Post Type name on edit.php of admin (posts table)
function gpi_remove_add_new_link() {
// Only on Region post type, and not for admins
if( 'region' == get_post_type() && ! current_user_can('create_users') )
echo '<style type="text/css">a.add-new-h2{display:none;}</style>';
// Also hide the Post Duplicator
echo '<style type="text/css">.wp-list-table .duplicate_post{display:none;}</style>';
}
add_action('admin_head', 'gpi_remove_add_new_link');
// Auto-assigns the current author's associate region id to any post they publish
function gpi_auto_assign_region_to_post( $post_id ) {
global $current_user;
// Only for Region Admin user roles and only for Posts
if ( gpi_is_current_user('region_admin') && 'post' == get_post_type($post_id) ) {
// Grab any Regions that are associate with this post author
$regions_by_author = gpi_get_associated_region( $current_user->ID );
// Store the array as a metavalue on publish, WP automatically serializes it
if(!wp_is_post_revision($post_id)) {
// Use the ACF plugin, if it exists
if ( is_plugin_active( 'advanced-custom-fields/acf.php' ) ) {
// field_52eafab4be98f is the ID for 'related_region'
update_field('field_52eafab4be98f', $regions_by_author, $post_id);
} // Otherwise, just store as post meta, which still works but may duplicate DB entries
else {
add_post_meta($post_id, 'related_region', $regions_by_author, true);
}
}
}
}
add_action( 'publish_post', 'gpi_auto_assign_region_to_post' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment