Skip to content

Instantly share code, notes, and snippets.

@anunay
Last active August 29, 2015 14:03
Show Gist options
  • Save anunay/169642eb69b0ba0fe622 to your computer and use it in GitHub Desktop.
Save anunay/169642eb69b0ba0fe622 to your computer and use it in GitHub Desktop.
Wordpres: Admin user association
<?php
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
$current_admin_user = wp_get_current_user();
if ( isset( $_POST['first_name'] ) ){
$user = new WP_User( $user_id );
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
if($user_role!="administrator"){
update_user_meta($user_id, 'asso_admin', $current_admin_user->ID);
}else{
update_user_meta($user_id, 'asso_admin', $user_id);
}
}
}
add_action('pre_user_query','adminuser_pre_user_query');
function adminuser_pre_user_query($user_search) {
if(is_admin()){
$user = wp_get_current_user();
if($user->ID!=1){
global $wpdb;
$user_search->query_from .= "JOIN {$wpdb->usermeta} um ON um.user_id = {$wpdb->users}.ID AND um.meta_key = 'asso_admin'";
$user_search->query_where = 'WHERE um.meta_value=' . $user->ID;
}
}
}
@govindak
Copy link

        add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
        function myplugin_registration_save( $user_id ) {   
                $current_admin_user = wp_get_current_user();
            if ( isset( $_POST['first_name'] ) ){
                $user = new WP_User( $user_id );
                $user_roles = $user->roles;
                $user_role = array_shift($user_roles);
                if($user_role!="administrator"){
                    update_user_meta($user_id, 'asso_admin', $current_admin_user->ID);  
                }else{
                    update_user_meta($user_id, 'asso_admin', $user_id); 
                }        
            }
        }


        add_action('pre_user_query','adminuser_pre_user_query');
        function adminuser_pre_user_query($user_search) {
            if(is_admin()){
              $user = wp_get_current_user();
              if($user->ID!=1){
                global $wpdb;
                $user_search->query_from .= "JOIN {$wpdb->usermeta} um ON um.user_id = {$wpdb->users}.ID AND um.meta_key = 'asso_admin'";
                $user_search->query_where = 'WHERE um.meta_value=' . $user->ID;         
              }
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment