Skip to content

Instantly share code, notes, and snippets.

@remcotolsma
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remcotolsma/07418d345649537fbc41 to your computer and use it in GitHub Desktop.
Save remcotolsma/07418d345649537fbc41 to your computer and use it in GitHub Desktop.
WordPress admin page post capabilities.
<?php
$update = filter_has_var( INPUT_POST, 'pronamic_save_permissions' );
?>
<div class="wrap">
<h2><?php echo get_admin_page_title(); ?></h2>
<form action="" method="post">
<h3><?php _e( 'Post Types', 'pronamic' ); ?></h3>
<?php
$post_types = array(
'book',
'post',
'page',
);
$roles = get_editable_roles();
$capabilities = array(
// 'edit_post' => __( 'Edit Post', 'pronamic' ),
// 'read_post' => __( 'Read Post', 'pronamic' ),
// 'delete_post' => __( 'Delete Post', 'pronamic' ),
'edit_posts' => __( 'Edit Posts', 'pronamic' ),
'edit_others_posts' => __( 'Edit Other Posts', 'pronamic' ),
'publish_posts' => __( 'Publish Posts', 'pronamic' ),
'read_private_posts' => __( 'Read Private Post', 'pronamic' ),
'delete_posts' => __( 'Delete Posts', 'pronamic' ),
'delete_private_posts' => __( 'Delete Private Posts', 'pronamic' ),
'delete_published_posts' => __( 'Delete Published Posts', 'pronamic' ),
'delete_others_posts' => __( 'Delete Other Posts', 'pronamic' ),
'edit_private_posts' => __( 'Edit Private Posts', 'pronamic' ),
'edit_published_posts' => __( 'Edit Published Posts', 'pronamic' ),
);
?>
<table class="widefat" cellspacing="0">
<?php foreach ( $post_types as $post_type ) : ?>
<?php $post_type = get_post_type_object( $post_type ); ?>
<thead>
<tr>
<th scope="row">
<?php echo $post_type->label; ?>
</th>
<?php foreach ( $roles as $role_name => $role_info ) : ?>
<th scope="col">
<?php echo translate_user_role( $role_info['name'] ); ?>
</th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ( $capabilities as $name => $label ) : ?>
<?php if ( isset( $post_type->cap->$name ) ) : ?>
<tr>
<th scope="row">
<?php echo $label ?>
</th>
<?php foreach ( $roles as $role_name => $role_info ) : ?>
<td>
<?php
$role = get_role( $role_name );
$cap = $post_type->cap->$name;
if ( $update ) {
$role->add_cap( $cap, isset( $_POST[ $role_name ][ $cap ] ) );
}
?>
<input type="checkbox" name="<?php echo esc_attr( sprintf( '%s[%s]', $role_name, $cap ) ); ?>" value="true" <?php checked( $role->has_cap( $cap ) ); ?>/>
</td>
<?php endforeach; ?>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
<?php endforeach; ?>
</table>
<?php submit_button( __( 'Save Changes', 'pronamic' ), 'primary', 'pronamic_save_permissions' ); ?>
</form>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment