• Resolved kklo

    (@kklo)


    Hi,

    I have two types of users. User1 and User2

    I want this CSS line

    #st-content-wrapper .widgets .widget-box {
    display: none !important;
    }

    appear for for user1 and do not appear for user2 and when user2 logs in, the CSS line does not appear.

    I looked in the documentation for a way to put this CSS line in functions.php, I couldn’t find it.

    Can someone help me?

    Thanks

    • This topic was modified 3 years, 7 months ago by Yui.
    • This topic was modified 3 years, 7 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 11 replies - 1 through 11 (of 11 total)
  • You could use the body_class filter

    see https://developer.www.ads-software.com/reference/hooks/body_class/

    To add a class based on user1 type or user 2 type

    then

    body.user1 #st-content-wrapper .widgets .widget-box {
    display: none !important;
    }

    e.g.

    add_filter( 'body_class', function( $classes ) {
        if ( logic to find if user is user1 type) {
           $classes = array_merge( $classes, array( 'user1' ) );
        }
        return  $classes;
    } );

    This does not sound like a good idea from a scale and customization perspective, and at the bare minimum you should really use something Role based, just fyi.
    You would get better support if you described what you are trying to acheive.

    To answer your question use $current_user = wp_get_current_user();
    In a very un-elegant way, add this to the header.php file in your child theme:

    <?php 
    	wp_get_current_user();
    	if ( $current_user->user_login =='user1' ) {
    		echo '
    			<style type="text/css">
    			#st-content-wrapper .widgets .widget-box {
    				display: none !important;
    			}
    			</style>	
    		';
    	}
    ?>

    You may want to investigate this to see if it suits:
    https://en-ca.www.ads-software.com/plugins/conditional-widgets/

    In addition with @corrinarusso to run custom CSS/Script based on user_id (https://prnt.sc/1qdg6s9) through the wp_head hook. You can use the below example.

    Example:

    /** Run custom inline CSS based on user ID **/
    add_action( 'wp_head', 'run_my_custom_css');
    function run_my_custom_css() {
    global $current_user;
    $user_id = get_current_user_id();
    if( is_user_logged_in() && $user_id == '30'){
    	?>
    	<style>
    		#st-content-wrapper .widgets .widget-box {
    		display: none !important;
    		}
    	</style>
    	<?php
        }
    }

    Hook reference- https://developer.www.ads-software.com/reference/hooks/wp_head/

    • This reply was modified 3 years, 7 months ago by MM Aurangajeb.
    Thread Starter kklo

    (@kklo)

    Hi, @corrinarusso, @alanfuller

    With this CSS, I want to hide a theme box where it identifies information about the Partnership (user), if this user doesn’t pay a monthly fee.

    But if he pays this monthly fee, this user’s information (box) appears.

    I found it easier to identify the type of user;

    user1 – hide box
    user2 – view box

    • This reply was modified 3 years, 7 months ago by kklo.
    Thread Starter kklo

    (@kklo)

    an observation, it’s not one or two users, it’s user roles, like clients

    Hiding with CSS is not actually stopping the access. All the use has to do is inspect the page and change the CSS with browser tools and they would have access to the content they have not paid for.

    A better strategy is to detect the user type server side and display the widget conditionally through server side code.

    Is the widget something you coded?

    Thread Starter kklo

    (@kklo)

    Hi, @alanfuller

    I didn’t change the widget.

    Even though the user group, Customers who didn’t pay to access what was hidden with CSS, won’t be able to access the details of the information, it’s more for aesthetics.

    I thought in the form of CSS it would be easier.

    an observation, it’s not one or two users, it’s user roles, like clients

    Are you using default WordPress roles, e.g., Author, Editor, Contributor, and so on, or have you created custom roles via a plugin?

    You can check for specific roles in the roles property of the user object.

    
    $user = wp_get_current_user();
    if ( in_array( 'author', (array) $user->roles ) ) {
        // Add your custom CSS for Authors here.
    }

    I thought in the form of CSS it would be easier.

    I agree with the suggestion about displaying the widget conditionally. If you need to detect roles and add custom CSS accordingly it might not be any easier.

    Or were you imagining to be able to hook into an existing CSS class, for example is-author or something? If so, I don’t think that’s available by default unfortunately.

    If you’re comfortable with developing WordPress sites, there are a lot of hooks related to sidebars and widgets. For example the widget_text hook, which allows you to filter the contents of text widgets.

    Knowing exactly which widget you want to show and where, would help to narrow down the options.

    • This reply was modified 3 years, 7 months ago by ramonopoly.
    Thread Starter kklo

    (@kklo)

    Hi,

    In the topic related to tourism, when inserting a tour, there is a field with information about the tourism agency, by default. It may or may not be filled.

    If filled in, this data will appear on the tour page.

    There are two ways the site makes money:

    1 – charging the travel agency commission, in this case the travel agency’s data is hidden.

    2 – charging a monthly fee from the travel agency, in this case the data is visible on the tour page.

    To differentiate between paying and commissionable travel agencies, I imagined using roles and conditioning to be hidden or not with CSS.

    Thread Starter kklo

    (@kklo)

    Hi, @ramonopoly

    it worked perfectly, thanks

    Thread Starter kklo

    (@kklo)

    hi, @ramonopoly

    it worked perfectly, thanks

    But I need it the other way around. When not the author hides with CSS.

    Sorry, but I’m just curious about php, could you help me?

    Thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘functions.php’ is closed to new replies.