Skip to content

Instantly share code, notes, and snippets.

@bryanwillis
Last active April 8, 2016 08:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bryanwillis/b2e558b395f2018f2eba to your computer and use it in GitHub Desktop.
Save bryanwillis/b2e558b395f2018f2eba to your computer and use it in GitHub Desktop.
Gravity Forms User Capabilities / Roles - Using user_has_cap (which is a simpler filter than map_meta_cap), allows Editors to view and manage form entries. #user_has_cap #role #currrent_user_can
<?php
// Only administrators can delete published posts:
add_filter( 'map_meta_cap',
function( $required_caps, $cap ) {
if ( 'delete_post' == $cap )
$required_caps[] = 'manage_options';
return $required_caps;
}, 10, 2 );
<?php
// Don't allow file changes via the UI:
add_filter( 'map_meta_cap',
function( $required_caps, $cap ) {
if ( in_array( $cap, array(
'edit_themes',
'edit_plugins',
'update_themes',
'update_plugins',
'install_themes',
'install_plugins',
'update_core'
) ) )
$required_caps[] = 'do_not_allow';
return $required_caps;
}, 10, 2 );
/**
* add to wp-config without defined part or functions with defined part
* to avoid already defined
*/
defined('DISALLOW_FILE_EDIT') || define( 'DISALLOW_FILE_EDIT' true );
defined('DISALLOW_FILE_EDIT') || define( 'DISALLOW_FILE_MODS' true );
<?php
/*-----------------------------------------------------------------------------------*/
/* Capability */
/*-----------------------------------------------------------------------------------*/
/*
function add_capability() {
// gets the author role
$role = get_role( 'admin' );
// This only works, because it accesses the class instance.
$role->add_cap( 'edit_users' );
}
add_action( 'admin_init', 'add_capability');
// */
/*-----------------------------------------------------------------------------------*/
/* Has Capability*/
/*-----------------------------------------------------------------------------------*/
// if ($user->has_cap('manage_options')) { return admin_url(); }
<?php
/*-----------------------------------------------------------------------------------*/
/* Block Access to Certain User Levels on Admin */
/*-----------------------------------------------------------------------------------*/
//*
function restrict_access_admin_panel(){
global $current_user;
get_currentuserinfo();
if ($current_user->user_level < 4) {
wp_redirect( get_bloginfo('url') );
exit;
}
}
add_action('admin_init', 'restrict_access_admin_panel', 1);
// */
/*-----------------------------------------------------------------------------------*/
/* Not Used */
/*-----------------------------------------------------------------------------------*/
//*
add_filter('map_meta_cap', 'prevent_user_edit', 10, 4 );
function prevent_user_edit( $required_caps, $cap, $user_id, $args ){
$protected_user = 2; // ID of user not editable
if ( $user_id === $protected_user ) // Don't block caps if current user = protected user
return $required_caps;
$blocked_caps = array(
'delete_user',
'edit_user'
);
if ( in_array( $cap, $blocked_caps ) && $args[0] === $protected_user )
$required_caps[] = 'do_not_allow';
return $required_caps;
}
// */
<?php
// Users can edit coments, for 30 minutes:
add_filter( 'map_meta_cap',
function( $caps, $cap, $user_id, $args ) {
if ( $cap !== 'edit_comment' )
return $caps;
$comment_id = $args[1];
$c = get_comment( $comment_id );
$user_id = $c->user_id;
$time = strtotime( $c->comment_date_gmt );
$window = strtotime( '-30 minutes' );
if ( $user_id && $time > $window )
return array(); // No cap required!
return $caps;
}, 10, 3 );
<?php
// If you can edit pages, you can edit widgets
add_filter( 'user_has_cap',
function( $caps ) {
if ( ! empty( $caps['edit_pages'] ) )
$caps['edit_theme_options'] = true;
return $caps;
} );
<?php
// Require editors to approve posts:
add_filter( 'map_meta_cap',
function( $required_caps, $cap ) {
if ( $cap == 'publish_post' || $cap == 'publish_posts' )
$required_caps[] = 'edit_others_posts';
return $required_caps;
}, 10, 2 );
<?php
//*
add_action('wp_authenticate','login_with_email_address');
add_filter( 'gettext', 'change_username_wps_text' );
function login_with_email_address($username) {
$user = get_user_by('email',$username);
if(!empty($user->user_login))
$username = $user->user_login;
return $username;
}
function change_username_wps_text($text){
//if ( 'wp-login.php' != basename( $_SERVER['SCRIPT_NAME'] ) )
//return;
if(in_array($GLOBALS['pagenow'], array('wp-login.php'))){
if ($text == 'Username'){$text = 'Username / Email';}
}
return $text;
}
/*-----------------------------------------------------------------------------------*/
/* Redirect back to page after login */
/*-----------------------------------------------------------------------------------*/
//*
if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) {
add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
wp_safe_redirect($location);
exit();
}
}
// */
<?php
/*-----------------------------------------------------------------------------------*/
/* Insert New User */
/*-----------------------------------------------------------------------------------*/
//*
function wpse_22754_insert_new_user() {
$user_data = array(
'ID' => '',
'user_pass' => '@DD128YyDpOBysi$t(CePy&g',
'user_login' => 'TimBuhay',
'user_nicename' => 'Tim Buhay',
'user_email' => 'tim@disputebills.com',
'display_name' => 'Tim Buhay',
'nickname' => 'Tim',
'first_name' => 'Tim',
'last_name' => 'Buhay',
'user_url' => '',
'user_registered' => '2015-09-03 08:55:55',
'role' => 'administrator '
);
$user_id = wp_insert_user( $user_data );
}
add_action( 'admin_init', 'wpse_22754_insert_new_user' );
// */
/*-----------------------------------------------------------------------------------*/
/* Allow the user to be updated once they are created */
/*-----------------------------------------------------------------------------------*/
//*
function wpse_22754_empty_email_error( $arg ) {
if ( !empty( $arg->errors['empty_email'] ) ) unset( $arg->errors['empty_email'] );
}
add_action( 'user_profile_update_errors', 'wpse_22754_empty_email_error' );
// */
/*-----------------------------------------------------------------------------------*/
/* Add Role */
/*-----------------------------------------------------------------------------------*/
//*
function wps_change_role_name() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$wp_roles->roles['administrator']['name'] = 'Devops';
$wp_roles->role_names['administrator'] = 'Devops';
}
add_action('init', 'wps_change_role_name');
// */
<?php
// Don't let anyone delete users:
add_filter( 'map_meta_cap',
function( $required_caps, $cap ) {
if ( 'delete_user' == $cap || 'delete_users' == $cap )
$required_caps[] = 'do_not_allow';
return $required_caps;
}, 10, 2, );
<?php
/*-----------------------------------------------------------------------------------*/
/* Will Break Site if Not Activated in functions.php */
/*-----------------------------------------------------------------------------------*/
add_action('', '');
function no_proflie_admin_pages_redirect() {
if(!current_user_can('manage_options')){
return;
}
global $pagenow;
$admin_redirects = array(
'profile.php'
);
if(in_array($pagenow, $admin_redirects)){
wp_redirect( admin_url('/') ); exit;
}
}
these can be added to list of blocked capabilites for a user
'install_plugins'
'activate_plugins'
'update_plugins'
'delete_plugins'
'list_users'
'add_users'
'create_users'
'edit_users'
'delete_users'
'remove_users'
'unfiltered_upload'
'install_themes'
'update_themes'
'delete_themes'
'switch_themes'
'edit_theme_options'
'manage_options'
'import'
'update_core'
'edit_dashboard'
'gravityforms_view_entries'
'gravityforms_edit_entries'
'gravityforms_delete_entries'
'gravityforms_export_entries'
'gravityforms_view_entry_notes'
'gravityforms_edit_entry_notes'
'gravityforms_feed'
'manage_administrators'
<?php
/**
* Prevent Editing of a specified user
*
* This example shows how you can protect the original admin from being edited or deleted by anyone else
*/
add_filter('map_meta_cap', 'prevent_user_edit', 10, 4 );
function prevent_user_edit( $required_caps, $cap, $user_id, $args ){
$protected_user = 1; // ID of user not editable
if ( $user_id === $protected_user ) // Don't block caps if current user = protected user
return $required_caps;
$blocked_caps = array(
'delete_user',
'edit_user'
);
if ( in_array( $cap, $blocked_caps ) && $args[0] === $protected_user )
$required_caps[] = 'do_not_allow';
return $required_caps;
}
<?php
// Where you are assigning *_books capabilities to users:
register_post_type( 'book', array(
...
'capability_type' => 'book',
// Map read_post, edit_post, etc.
'map_meta_cap' => true,
...
) );
<?php
// Give secondary "administrators" less control:
add_filter( 'user_has_cap',
function( $caps, $cap, $args ) {
$user_id = $args[1];
$user = new WP_User( $user_id );
$email = $user->user_email;
if ( $email != get_option('admin_email') )
$caps['manage_options'] = false;
return $caps;
}, 10, 3 );
<?php
global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "role_apple" ) ){
// do something
}
<?php
/**
* Add Gravity Forms capabilities
*/
add_filter('user_has_cap',
function( $caps ){
if (! empty( $caps['edit_pages'] ) ) { // user has edit capabilities
$caps['gravityforms_delete_entries'] = true;
$caps['gravityforms_edit_entries'] = true;
$caps['gravityforms_edit_entry_notes'] = true;
$caps['gravityforms_view_entries'] = true;
$caps['gravityforms_view_entry_notes'] = true;
}
return $caps;
});
<?php
/*-----------------------------------------------------------------------------------*/
/* Add Contact Methods */
/*-----------------------------------------------------------------------------------*/
//*
function phone_contactmethods( $contactmethods ) {
$contactmethods['phone'] = 'Phone';
return $contactmethods;
}
add_filter('user_contactmethods','phone_contactmethods',10,1);
/*-----------------------------------------------------------------------------------*/
/* Add custom meta fields */
/*-----------------------------------------------------------------------------------*/
//*
add_filter( 'user_contactmethods', 'rv_custom_profile_fields', 9999 );
function rv_custom_profile_fields( $contactmethods ) {
unset( $contactmethods['twitter'] );
unset( $contactmethods['googleplus'] );
unset( $contactmethods['facebook'] );
$contactmethods['twitter_custom'] = 'Twitter Profile URL';
$contactmethods['facebook_custom'] = 'Facebook Profile URL';
$contactmethods['linkedin_custom'] = 'LinkedIn Profile URL';
$contactmethods['gplus_custom'] = 'Google+ Profile URL';
return $contactmethods;
}
// */
/*-----------------------------------------------------------------------------------*/
/* Hide Admin User from users.php */
/*-----------------------------------------------------------------------------------*/
//*
function hide_admin_user_bw() {
?>
<style type="text/css">
.users-php tr#user-2 {
display: none!important;
}
.users-php li.administrator {
display: none!important;
}
</style>
<?php
}
add_action('admin_head-users.php', 'hide_admin_user_bw');
// */
/*-----------------------------------------------------------------------------------*/
/* Remove Personal Options */
/*-----------------------------------------------------------------------------------*/
// removes the `profile.php` admin color scheme options
remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
if ( ! function_exists( 'cor_remove_personal_options' ) ) {
/**
* Removes the leftover 'Visual Editor', 'Keyboard Shortcuts' and 'Toolbar' options.
*/
function cor_remove_personal_options( $subject ) {
$subject = preg_replace( '#<h3>Personal Options</h3>.+?/table>#s', '', $subject, 1 );
return $subject;
}
function cor_profile_subject_start() {
ob_start( 'cor_remove_personal_options' );
}
function cor_profile_subject_end() {
ob_end_flush();
}
}
add_action( 'admin_head-profile.php', 'cor_profile_subject_start' );
add_action( 'admin_footer-profile.php', 'cor_profile_subject_end' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment