Skip to content

Instantly share code, notes, and snippets.

@bryanwillis
Last active September 16, 2024 01:46
Show Gist options
  • Select an option

  • Save bryanwillis/b2e558b395f2018f2eba to your computer and use it in GitHub Desktop.

Select an option

Save bryanwillis/b2e558b395f2018f2eba to your computer and use it in GitHub Desktop.

Revisions

  1. bryanwillis revised this gist Jan 9, 2016. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions other-caps
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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'
  2. bryanwillis revised this gist Jan 9, 2016. 16 changed files with 360 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions admin-publish-posts.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <?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 );
    23 changes: 23 additions & 0 deletions block-file-changes.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <?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 );
    20 changes: 20 additions & 0 deletions capabilities.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <?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(); }
    38 changes: 38 additions & 0 deletions current-user-level.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    <?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;
    }
    // */
    15 changes: 15 additions & 0 deletions edit-comments-30-minutes.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    <?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 );
    8 changes: 8 additions & 0 deletions edit-pages-edit-theme-options.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <?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;
    } );
    8 changes: 8 additions & 0 deletions editors-approve-posts.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <?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 );
    34 changes: 34 additions & 0 deletions login.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    <?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();
    }
    }
    // */
    51 changes: 51 additions & 0 deletions new-user.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    <?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');
    // */
    8 changes: 8 additions & 0 deletions no-delete-users.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <?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, );
    18 changes: 18 additions & 0 deletions no-profile-admin.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?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;
    }
    }
    20 changes: 20 additions & 0 deletions protected-user.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <?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;
    }
    9 changes: 9 additions & 0 deletions register-post-type-cap.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <?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,
    ...
    ) );
    11 changes: 11 additions & 0 deletions secondary-admin.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <?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 );
    7 changes: 7 additions & 0 deletions user-can.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <?php

    global $current_user;
    get_currentuserinfo();
    if ( user_can( $current_user, "role_apple" ) ){
    // do something
    }
    82 changes: 82 additions & 0 deletions user-profile.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    <?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' );
  3. bryanwillis revised this gist Jan 9, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions user-has-cap.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    /**
    * Add Gravity Forms capabilities
    */
  4. bryanwillis created this gist Jan 9, 2016.
    14 changes: 14 additions & 0 deletions user-has-cap.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    /**
    * 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;
    });