Support

Home Forums Event Espresso Premium Hide Categories for non logged-in users (visitors)

Hide Categories for non logged-in users (visitors)

Posted: April 13, 2018 at 9:50 am


abycto

April 13, 2018 at 9:50 am

This topic was marked as closed however the sample code provided doesn’t appear to work.

https://eventespresso.com/topic/hide-category-for-non-logged-in-users/


Josh

  • Support Staff

April 13, 2018 at 9:59 am

Hi there,

The topics on this forum self close after a few weeks of inactivity. The code does work if it’s added to a plugin, the plugin is activated, and the correct event category ID is swapped into line 19 of the example.


abycto

April 13, 2018 at 10:17 am

Thank you Josh – we created a specific plugin for EE4 Customization and added the code snipet to it – trying to hide 1 category but the event still displays in the calendar.


Josh

  • Support Staff

April 13, 2018 at 11:54 am

You can do this instead (only works on the calendar)

function eecal_custom_category_restrict(){
    if(is_user_logged_in()){
        return;
    }
    wp_add_inline_script( 
        'espresso_calendar',
        'function my_hide_category() {
            var cat = "category-slug"; //change this to match category slug
            jQuery("#espresso_calendar ." + cat).hide();
        };
        jQuery( document ).ready(function($) {
            $( document ).ajaxComplete(my_hide_category);
        });'
    );
}
add_action( 'wp_enqueue_scripts', 'eecal_custom_category_restrict', 20 );


abycto

April 13, 2018 at 1:08 pm

Thank you Josh this code does work in the Calendar – what if I need to hide 2 categories? Should I create a parent category for the two and use the parent instead of the category slug?


Josh

  • Support Staff

April 13, 2018 at 2:26 pm

A slight modification to the code will allow you to add more categories.

You change these two lines:

var cat = "category-slug"; //change this to match category slug
 jQuery("#espresso_calendar ." + cat).hide();

to:

var cat1 = "category-slug"; //category slug
var cat2 = "other-category-slug"; //other category
jQuery("#espresso_calendar ." + cat1 + ", #espresso_calendar ." + cat2).hide();


abycto

April 15, 2018 at 10:43 am

Thank you Josh – this worked for the Calendar as you mentioned.

Could this code be reused with some modification for the Table View plugin?


Josh

  • Support Staff

April 16, 2018 at 9:19 am

You can use this one for the Table view plugin:

https://gist.github.com/joshfeck/306e3fd441c90de62c4cd0453b029a9f

The support post ‘Hide Categories for non logged-in users (visitors)’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso