Wordpress Different User, Different CSS How ???

Meenakshi Sandle

Active member
Banned User
Apr 9, 2020
162
38
28
31
Chandigarh
www.trendingmantra.com
Hi,

I added some custom css (wordpress font increase or color change) in WordPress backend, I want that CSS only apply on the particular user, not to all WordPress users?

How can this possible? Please Help me.

Meenakshi
 

venkat

Member
May 18, 2020
48
18
8
wrap the css code or style sheet inside php echo. trigger php code based on wordpress user role

global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "author" ) ){
// do something with echo
}

I never tried it. if this didn't work, I don't see any work around.
 

Meenakshi Sandle

Active member
Banned User
Apr 9, 2020
162
38
28
31
Chandigarh
www.trendingmantra.com
wrap the css code or style sheet inside php echo. trigger php code based on wordpress user role

global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "author" ) ){
// do something with echo
}

I never tried it. if this didn't work, I don't see any work around.
Thnx but where to paste this code.. I m not such tech person. Plz explain it more abit plz.
 

venkat

Member
May 18, 2020
48
18
8
Thnx but where to paste this code.. I m not such tech person. Plz explain it more abit plz.
Then i want to know below details
1. from where you are adding custom css - page builder or directly from theme?
2. what theme or page builder you are using.
3. exactly what css code you are trying to insert and what content it is going to affect.

only after knowing these details I can think and let you know solution in detail.
 

venkat

Member
May 18, 2020
48
18
8
Thnx but where to paste this code.. I m not such tech person. Plz explain it more abit plz.
1. You can use below plugin to create shortcode for your php code
2. download and add below library for generating dynamic php css

3. create your php css code as per examples given in github link from step 2

4. place your created code in php code section of the plugin installed in step 1 and generate short code

5. place your short code in the pages where ever you want the dynamic css needs to reflect.

6. don't forget to wrap code created in step 3 using below code
global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "author" ) ){
// dynamic php css goes here
}
 

Meenakshi Sandle

Active member
Banned User
Apr 9, 2020
162
38
28
31
Chandigarh
www.trendingmantra.com
1. You can use below plugin to create shortcode for your php code
2. download and add below library for generating dynamic php css

3. create your php css code as per examples given in github link from step 2

4. place your created code in php code section of the plugin installed in step 1 and generate short code

5. place your short code in the pages where ever you want the dynamic css needs to reflect.

6. don't forget to wrap code created in step 3 using below code
global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "author" ) ){
// dynamic php css goes here
}
Thank you for your reply... I mean..

there are 3 users, i need to make css for all the users EXCEPT 1.. how can i code this ?? Thank you
 

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
24,108
27,374
120
You have to be a little more specific. Like: what's the difference between those 3 users? 1 is admin and others are subscribers? All are subscribers? The 1 different is about user type or username?

What you're asking is like: I have 3 identical cars. The one I drive I want to be different. But I won't tell you which one I drive.
 

Meenakshi Sandle

Active member
Banned User
Apr 9, 2020
162
38
28
31
Chandigarh
www.trendingmantra.com
You have to be a little more specific. Like: what's the difference between those 3 users? 1 is admin and others are subscribers? All are subscribers? The 1 different is about user type or username?

What you're asking is like: I have 3 identical cars. The one I drive I want to be different. But I won't tell you which one I drive.
" Its just a simple dynamic website, not much complex....

- There are 3 admin accounts, My Account (1) & other 2 admin accounts also...
- So total 3 ADMIN accounts....

- I had already hidden some content in wordpress Backend panel via Manual CSS.
- Now I need to see all content for myself.

- But I need to Hide that backend stuff for OTHER ADMIN USERS." I want the to say, I want to see all my backend content whereas to other admin users it should be hidden.

I really hope you got my point, what I m trying here to say.

Thank you
 

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
24,108
27,374
120
PHP:
function hide_for_others() {
$current_user = wp_get_current_user();
if (!$current_user->user_login == 'your username') { ?>
<style type="text/css">
#whatever_css_id_you_want_to_hide {
your style here
}
.whatever_css_class_you_want_to_hide {
    your style here
}
</style>
<?php }
}
add_action( 'admin_init', 'hide_for_others');

Modify as you need it and place this code in your functions.php or in child theme's function.php

Edit: do not forget to clear/purge cache if you're using caching plugins and first test it on incognito window if you don't want to clear cache and cookies of your usual browser.
 
Last edited by a moderator:

Meenakshi Sandle

Active member
Banned User
Apr 9, 2020
162
38
28
31
Chandigarh
www.trendingmantra.com
PHP:
function hide_for_others() {
$current_user = wp_get_current_user();
if (!$current_user->user_login == 'your username') { ?>
<style type="text/css">
#whatever_css_id_you_want_to_hide {
your style here
}
.whatever_css_class_you_want_to_hide {
    your style here
}
</style>
<?php }
}
add_action( 'admin_init', 'hide_for_others');

Modify as you need it and place this code in your functions.php or in child theme's function.php

Edit: do not forget to clear/purge cache if you're using caching plugins and first test it on incognito window if you don't want to clear cache and cookies of your usual browser.




Why this showing here ??

Error.png
 

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
24,108
27,374
120
place this code in your functions.php

What did I say?

Anyway small change to the code:

PHP:
function hide_for_others() {
$current_user = wp_get_current_user();
if ($current_user->user_login != 'your username') { ?>
<style type="text/css">
#whatever_css_id_you_want_to_hide {
your style here
}
.whatever_css_class_you_want_to_hide {
    your style here
}
</style>
<?php }
}
add_action( 'admin_init', 'hide_for_others');

Please notice that logic of if changed as you need for others not for you.
 
  • Like
Reactions: tradesman

Meenakshi Sandle

Active member
Banned User
Apr 9, 2020
162
38
28
31
Chandigarh
www.trendingmantra.com
What did I say?

Anyway small change to the code:

PHP:
function hide_for_others() {
$current_user = wp_get_current_user();
if ($current_user->user_login != 'your username') { ?>
<style type="text/css">
#whatever_css_id_you_want_to_hide {
your style here
}
.whatever_css_class_you_want_to_hide {
    your style here
}
</style>
<?php }
}
add_action( 'admin_init', 'hide_for_others');

Please notice that logic of if changed as you need for others not for you.

If I place this code in (functions.php) then it will be deleted after theme updates... how to solve this problem ?? I just need not to show this code to anyone..
 

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
24,108
27,374
120
Make a child theme of your theme and place this code in your child theme functions.php.

Why you should use Wordpress Child Themes?
Since a child theme inherits the characteristics of a master or a parent theme, you can customize its code without breaking the original’s functionality. This way, if a theme gets an update, all of the changes you made won’t be overwritten.

How to create a Wordpress child theme?
Please google yourself.
 

Meenakshi Sandle

Active member
Banned User
Apr 9, 2020
162
38
28
31
Chandigarh
www.trendingmantra.com
Make a child theme of your theme and place this code in your child theme functions.php.

Why you should use Wordpress Child Themes?


How to create a Wordpress child theme?
I know CHILD THEME !!! But the thing is there are just few code in child theme. Anyone can find this manual code... hmmm.. I need no one can see this code. I place this code under bundle of coding.. HUGGGGE Code.
 

teawebsite

Active member
Nov 28, 2019
195
104
43
@Meenakshi Sandle my approach for this is refer to user id that is unique not admin role. WP has core function for this : wp_get_current_user()->ID == 1 (e.g. you want to catch user that has id of 1)
Let say you are user 1, to hide something in backend to others you have to declare
wp_get_current_user()->ID != 1 and use WP hook as 'admin_menu' :

Code:
 function remove_menus(){

if ( wp_get_current_user()->ID != 1 ) {
remove_menu_page( 'index.php' );
remove_menu_page( 'tools.php' );
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'edit.php?post_type=page' );
remove_menu_page( 'wpcf7' );
remove_menu_page( 'woocommerce' );
remove_menu_page('edit.php');
remove_menu_page( 'plugins.php' );
remove_menu_page( 'themes.php' );
remove_menu_page( 'options-general.php' );
remove_menu_page( 'vc-welcome' );
remove_menu_page( 'porfolio.php' );
}
}

add_action( 'admin_menu', 'remove_menus' );

To hide something in admin bar use WP hook 'admin_bar_menu':

Code:
// If not Administrator partila hide toolbar

  add_action( 'admin_bar_menu', 'remove_wp_logo', 999 );
  function remove_wp_logo( $wp_admin_bar ) {

      if ( wp_get_current_user()->ID != 1 ) {
     $wp_admin_bar->remove_node( 'wp-logo','edit');
     // $wp_admin_bar->remove_node( 'edit');
     $wp_admin_bar->remove_node( 'comments');
     $wp_admin_bar->remove_node( 'new-post' );
     $wp_admin_bar->remove_node( 'new-link' );
     $wp_admin_bar->remove_node( 'new-media' );
     $wp_admin_bar->remove_node( 'new-page' );
     $wp_admin_bar->remove_node( 'new-shop_order' );
    $wp_admin_bar->remove_node( 'new-shop_coupon' );

     }
}
Hope it helps.
 

Forum statistics

Threads
69,485
Messages
909,975
Members
239,840
Latest member
eyuiqwyeiuqweqw

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu