[Events +] Conditional logic for recurring events with countdown

I have a client who’s church broadcasts a live video every Sunday. On the church’s website, there’s a live video player that’s being displayed while the event is live and when it’s finished, a countdown timer is shown counting down to the next event the following week.

Right now I’m doing this manually with the Date/Time picker and a dropdown from Advanced Custom Fields Pro to make that logic work.

I want to know this scenario is possible to automate this process with Events+.

So say I have a recurring weekly event happening every Sunday from 9 AM to 1 PM with Events+. Could I do something like the following?

[If the weekly event is now]

Show the embedded live player here

[Otherwise, if the event has passed]

Show the countdown here

[endif]

Is this possible?

  • Nahid
    • Tech Support

    Hey Glenton !
    Hope you are having a great day!

    Thank you very much for your query. Unfortunately, something like this would not be possible with Events+ out of the box. However, this can be accomplished by creating a custom template for single events, adding conditional to determine the event status and displaying content accordingly.

    If that sounds doable for you, please go through the following steps:

    1. Create custom Single Events template: In order to create a custom single events template, please log into your server using FTP/cPanel's File Manager, navigate to {WordPress Installation Folder}/wp-content/plugins/events-and-bookings/default-templates/default and copy over the single-incsub_event.php file to your theme (more preferably, child theme) folder.

    2. Add required conditional and content accordingly: In the custom template, add the following PHP conditional and content code wherever you want the countdown/live video to show up:

    <?php
    $start_dates = $event->get_start_dates();
    foreach ($start_dates as $key => $start) {
        $start = $event->get_start_timestamp($key);
        $end = $event->get_end_timestamp($key);
    
        if (eab_current_time() > $start && eab_current_time() < $end) {
            // Event Currently running
            echo 'Event has already stated, display live video.';
        } elseif (eab_current_time() > $end) {
            // Event has ended
            echo 'Event has ended, display nothing.';
        } else {
            // Event in future
            echo 'Event is in the future, display countdown.';
        }
    }
    ?>

    In order to display the countdown/live video, please notice the comments and text that I've put in the above code snippet. Shortcodes can be outputted in the code by using the do_shortcode() function.

    Hope this helps. Let us know if you need any further assistance regarding this. Thanks!

    Kind regards,
    Nahid

      • Nahid
        • Tech Support

        Hey Glenton !
        Hope you are doing well today!

        In that case, the best approach would be using a custom shortcode in order to display content depending on the conditional logic. Please try adding the following snippet of code in your theme's (more preferably, child theme's) functions.php file:

        function event_cond( $atts ) {
        	$a = shortcode_atts( array(
        		'id' => '',
        	), $atts );
        
        	ob_start();
        
        	$post_id = $a['id'];
        	$event = new Eab_EventModel($post_id);
        	$start_dates = $event->get_start_dates();
        	foreach ($start_dates as $key => $start) {
                $start = $event->get_start_timestamp($key);
                $end = $event->get_end_timestamp($key);
        
                if (eab_current_time() > $start && eab_current_time() < $end) {
                    // Event Currently running
                    echo 'Event has already stated, display live video.';
                } elseif (eab_current_time() > $end) {
                    // Event has ended
                    echo 'Event has ended, display nothing.';
                } else {
                    // Event in future
                    echo 'Event is in the future, display countdown.';
                }
            }
        
            $output = ob_get_clean();
            return $output;
        }
        add_shortcode( 'eventcond', 'event_cond' );

        Alternatively, you can also add the code in a mu-plugin. That'll require you to paste the mentioned code in a PHP file that ends with the extension .php, e.g. "anyname.php". The mentioned code should have a "<?php" definition at the start of the file. This PHP file should be placed in {your root WordPress folder}/wp-content/mu-plugins (you'll need to create the folder named "mu-plugins" if it is not there already).

        Once the code has been added, you can now use the custom [eventcond id="{Your Event ID}"] in any page/post in order to display the content depending on the conditional logic set on the code.

        Hope this helps. Let us know if this works for you. Thanks!

        Kind regards,
        Nahid

        • Glenton
          • Site Builder, Child of Zeus

          Looking at the code above, the second if statement that has with the $end variable. After an event has passed, wouldn't it always match $end? How would it ever make it to the last conditional to display the countdown?

          • Dimitris
            • Support Star

            Hey there Glenton

            You could change the conditionals to be like this then:

            if (eab_current_time() > $start && eab_current_time() < $end) {
              // Event Currently running
              echo 'Event has already stated, display live video.';
            } elseif (eab_current_time() > $end) {
              // Event in future
              echo 'Event is in the future, display countdown.';
            }

            Warm regards,
            Dimitris

    • Dimitris
      • Support Star

      I'm sorry for this, I missed the logic.
      This should do the trick, as it first checks if event is in future, then if it's ended.

      if (eab_current_time() > $start && eab_current_time() < $end) {
        // Event Currently running
        echo 'Event has already stated, display live video.';
      } elseif (eab_current_time() < $start) {
        // Event in future
        echo 'Event is in the future, display countdown.';
      } elseif (eab_current_time() > $end) {
        // Event has ended
        echo 'Event has ended, display nothing.';
      }

      Thank you,
      Dimitris

  • Glenton
    • Site Builder, Child of Zeus

    It works when I use the post id from a no recurring event but a post id from a recurring event doesn't hit any of the logic at all.

    Here's the actual code I have in a single page which I'm using as a custom archive page named page-media.php

    Staging site: https://staging.clcww.org/media

    <?php
    	$post_id = 1187;
    	$event = new Eab_EventModel($post_id);
    	$start_dates = $event->get_start_dates();
    	foreach ($start_dates as $key => $start) {
            $start = $event->get_start_timestamp($key);
            $end = $event->get_end_timestamp($key);
    
    if (eab_current_time() > $start && eab_current_time() < $end) { ?>
    
    		<div class="featured-player animated fadeIn">
    			<div class="grid-container">
    			<h1><i class="live"></i> Live</h1>
    			<div class="custom-player">
    			<?php get_template_part('template-parts/featured', 'player'); ?>
    			</div>
    			<?php get_template_part('template-parts/featured', 'info'); ?>
    			</div>
    		</div>	
    
    <?php } elseif (eab_current_time() < $start) { ?>
    	<div class="countdown-hero">
    		<div class="countdown-inner">
    			<div class="grid-container">
    				<div class="grix-x">
    					<div class="cell">
    						<h1 class="text-center">Time until next live event</h1>
    						<div class="the-countdown">
    							<?php echo do_shortcode('[next_event_countdown id="'.$post_id.'" format="dHMS" size="70" add="-120" expired="Too late!" title="yes"]'); ?>
    						</div>
    					</div>
    				</div>
    			</div>
    		</div>
    	</div>
    
    	<?php } elseif (eab_current_time() > $end) { ?>
    
    		<div class="featured-player animated fadeIn">
    			<div class="grid-container">
    			<h1><i class="live"></i> Event has ended</h1>
    			</div>
    		</div>	
    
    	<?php } ?>
    <?php } ?>
  • Dimitris
    • Support Star

    Hello Glenton,

    hope you're doing good! :slight_smile:

    This is happening because each recurring event actually auto-produces separate single events, with different post_id.

    If you visit a recurring event via calendar view, or a list of upcoming events, then above snippet should work well (also confirmed in a testing site of mine), as you are seeing these auto-generated events, not the "parent" one.

    If you're trying to see this "parent" event post, like in first screenshot from Events -> Events -> All (/wp-admin/edit.php?post_type=incsub_event), then you should have above issue.

    Hope that helps!
    Thank you,
    Dimitris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.