• Resolved mdbigg

    (@mdbigg)


    I have a calendar that uses multiple locations and I’d like to be able to display it publicly in a way that only displays events in particular locations.

    I’ve hacked around and added the following to class-r34ics.php

    into shortcode_defaults:
    ‘location_mask’ => false,

    Where events are processed:

    // Assemble events
    	foreach ((array)$ics_events as $event) {
    		// Check location mask
    		if (empty($location_mask) || stristr(@$event->location, $location_mask) != false) {
    			// All-day events
    			if (strlen($event->dtstart) == 8 || (strpos($event->dtstart, 'T000000Z') !== false && strpos($event->dtend, 'T000000Z') !== false)) {

    It all seems to work fine.

    Is this a feature you can add to the plugin?

    • This topic was modified 4 years, 8 months ago by mdbigg.

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author room34

    (@room34)

    This is a very interesting idea. I can see how it would be very useful. I’ll have to think about how I might be able to generalize this for other users. But at the very least I could add another hook at this point in the logic, so you could have this code in your theme or a separate plugin and not need to modify ICS Calendar itself.

    Thread Starter mdbigg

    (@mdbigg)

    That would be great. I’m not going to be supporting the site for long, so I don’t want to leave code that’s a hacked version of the plugin itself!

    I can see a generalised mask might be useful.

    Thanks!

    Plugin Author room34

    (@room34)

    OK, I’m not sure when/if I’ll be able to incorporate this kind of functionality directly in the plugin, but I went ahead and added the filter.

    In version 5.8.1, out now, there is a filter called r34ics_display_calendar_exclude_event that occurs right at the beginning of the foreach loop, where you had added your conditional.

    I’ve added some documentation to the plugin’s admin page explaining how this works, under the Developer tab. Let me know if you have any additional questions.

    Thread Starter mdbigg

    (@mdbigg)

    Thanks for this.

    I’ve put the following into my theme’s functions.php and not much seems to happen. Am I doing something wrong?

    function calendar_exclude_event_callback( $event, $args ) {
        // make your code do something with the arguments and return something
       	if (stristr(@$event->location, 'Hall') == false) {
    		return false;
    	} else {
    		return true;
    	}
    }
    
    // now hook the callback function to the 'example_filter'
    add_filter( 'r34ics_display_calendar_exclude_event', 'calendar_exclude_event_callback', 10, 2 );

    Thanks

    Plugin Author room34

    (@room34)

    You may need to add reload="true" to your shortcode temporarily to force the transient data to clear. Once that’s happened you can remove that from the shortcode again.

    Plugin Author room34

    (@room34)

    Oh, wait, one more thing… you have to have three parameters. The first should be the boolean value $exclude, then $event and $args as the second and third. (The first parameter in a filter is always the pass-through return value.)

    Thread Starter mdbigg

    (@mdbigg)

    Great – that works! My code looks like this:

    function calendar_exclude_event_callback($exclude, $event, $args) {
        // make your code do something with the arguments and return something
    	$result = stristr(@$event->location, 'Church Hall');
       	if ($result == false) {
    		return true;
    	} else {
    		return false;
    	}
    }
    
    // now hook the callback function to the 'example_filter'
    add_filter( 'r34ics_display_calendar_exclude_event', 'calendar_exclude_event_callback', 10, 3 );

    It seems that if the function returns ‘true’ then the event is included, and when ‘false’ the event is excluded.

    I worked on the assumption that a filter hook called ‘exclude_event’ should exclude the event when the function returns ‘true’. Is that just me?!

    Thanks for your help.

    Plugin Author room34

    (@room34)

    Yeah, it’s supposed to exclude the event if the function returns true.

    In your scenario, as it’s configured here, it should be showing only “Church Hall” events. (That is, it should be excluding any events that don’t contain “Church Hall”.) Is it not doing that?

    Thread Starter mdbigg

    (@mdbigg)

    Duh. I’m being stupid. It’s working exactly as expected. Thanks!

    Plugin Author room34

    (@room34)

    ?? No worries. Let me know if you run into any problems or have any other suggestions!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Location Mask’ is closed to new replies.