Join our new community forum for support & discussion

Join Now

Home Support Themes ListingHive Add Listing Button Visible by Role

Add Listing Button Visible by Role

  • Author
    Posts
  • sofia

    Hi. I’m wondering if I could hide the add listing button based on my user role? I’ve checked that after registering, the user will be a ‘Subscriber’ by default so I would like to make the ‘Add Listing’ button not visible only for ‘Subscriber’. Hope to hear from you soon. Thank you.

    ihor developer

    Sure, if you use some plugin or a snippet that adds a CSS class based on the user role then this CSS snippet should hide the button:

    .role-subscriber .hp-menu__item--listing-submit {display:none}

    For example this one https://gist.github.com/jancbeck/3178689

    approve24

    Hello Ihor!

    Don’t work by your example!

    I use a standard role subscriber- don’t work
    Use admin – dont work
    http://joxi.ru/gmvxOddtdwVdGA

    I am a lammer, can’t code well, please help

    ihor developer

    Please make sure that you added the code snippet correctly, for example you can add it via this plugin https://wordpress.org/plugins/code-snippets/

    approve24

    Sorry for stupid question

    http://joxi.ru/KAx1XKKtvp9EPr – is this right?

    approve24

    maybe you have some more detailed instruction? For dummies?

    approve24

    http://joxi.ru/ZrJw411tbv7kjm I do like this and http://joxi.ru/p27J1GGHn9VxOm this

    always something error

    can you give me printscreen of correctly snippet?

    robertv

    Hi approve24,
    It’s a CSS snippet, so i think you can use the example CSS in the code snippet.
    Please try this:

    add_action( 'wp_head', function () { ?>
    <style>
    
    	/* write your CSS code here */
    .role-subscriber .hp-menu__item--listing-submit {display:none}
    
    </style>
    <?php } );

    Hopefully this helps you.

    Regards,

    ihor developer

    Please try adding this snippet in Appearance/Customize/Additional CSS section.

    Thanks @robertv 🙂

    robertv

    Your welcome ihor !

    sofia

    Hi, ihor. I’ve tried the code from robertv and it didn’t work for me. I added the snippet in the Code Snippet plugin. I also have Content Control plugin activated to force login to all users. Is it not working because of the Content Control plugin?

    sofia

    Hey! I think I got it! Thank you @ihor and @robertv! @approve24, you can try this:

    In your theme’s functions.php, add this code:

    if ( is_user_logged_in() ) {
        add_filter('body_class','add_role_to_body');
        add_filter('admin_body_class','add_role_to_body');
    }
    function add_role_to_body($classes) {
        $current_user = new WP_User(get_current_user_id());
        $user_role = array_shift($current_user->roles);
        if (is_admin()) {
            $classes .= 'role-'. $user_role;
        } else {
            $classes[] = 'role-'. $user_role;
        }
        return $classes;
    }

    Then, in the Code Snippets plugin, add the code that @robertv suggested. For this part, you can directly try it in functions.php though, but I wanted to keep it simple and organised so I used the Code Snippets plugin instead.

    ihor developer

    Thanks for posting the solution! Yes, the PHP snippet from the linked topic should be added via the Code Snippets plugin, and the CSS snippet can be added in Appearance/Customize section.

    VolumeR

    Hi,

    I’ve added in Appearance/Customize/Additional CSS section (I also tried to add via Code Snippets):

    .role-subscriber .hp-menu__item–listing-submit {display:none}

    I’ve added PHP snippet via the Code Snippets plugin like this (I also tried to put it directly in wordpress/wp-content/themes/listinghive/functions.php):

    if ( is_user_logged_in() ) {
        add_filter('body_class','add_role_to_body');
        add_filter('admin_body_class','add_role_to_body');
    }
    function add_role_to_body($classes) {
        $current_user = new WP_User(get_current_user_id());
        $user_role = array_shift($current_user->roles);
        if (is_admin()) {
            $classes .= 'role-'. $user_role;
        } else {
            $classes[] = 'role-'. $user_role;
        }
        return $classes;
    }

    But the Add Listing button is still there!

    What’s wrong?

    ihor developer

    If the code snippets are added correctly please check your website as a subscriber, the code snippets you posted hide the Add Listing button for subscribers only. You can try registering as a new user to check this.

    VolumeR

    I’ve registered as a new user, I’ve verified that the new user has the subscriber role, but the Add button is still there

    ihor developer

    I noticed that the CSS snippet you posted is incorrect, please try this one:

    .role-subscriber .hp-menu__item--listing-submit {display:none!important}

    There was a single dash instead of a double one.

    VolumeR

    Works perfectly now! Thank you!

Viewing 18 posts - 1 through 18 (of 18 total)

New Reply

This forum has been archived and is no longer accepting new posts or replies. Please join our new community forum for support & discussion.