Start a new topic

Next upcoming event date for Countdown widget

Currently the countdown widget seems to be limited to a hardcoded date ("End time"). I would like to use the date for the next upcoming event (and also its title in a text widget like Cusotm Heading). The only option is now to manually change the date in the page which makes it not really useful.


1 person likes this idea

This feature would be great. Not every agency has time to update it every time. Hope to hear soon!

I set up a cronjob for this:


<?php

require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

 

$post_id = xx; // ID of the post/page where the countdown is integrated

 

$args = array(

    'post_type'=> 'event',

    'order' => 'ASC',

 'orderby' => 'date',

    );

 

$the_query = new WP_Query( $args );

 

$time = substr($the_query->posts[0]->post_date, 0, -3);

if ($time!="")

{

 $content = get_post($post_id)->post_content;

 $new_content = preg_replace('/end_time="[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}"/', 'end_time="'.$time.'"', $content);

 $events = array(

    'ID' => $post_id,

    'post_content' => $new_content,

   );

 wp_update_post( $events );

 

 echo "Update ok!";

}


Hi Webfish,
Could you please let me know how your got that running in Wordpress? I have no idea how to set up a cronjob with PHP included...
Thanks!!

 

You have to modify the functions.php of your theme. You should create a child theme for the modification. Because if you modify a theme directly and it is updated, then your modifications will be lost.


Here is a guide how to create a child theme: https://codex.wordpress.org/Child_Themes


Afterwards you put the following code in your new functions.php:


 

<?php

add_action('set_countdown_hourly', 'do_this_hourly');


function cron_countdown() {
	if ( !wp_next_scheduled( 'set_countdown_hourly' ) ) {
		wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'set_countdown_hourly');
	}
}

add_action('wp', 'cron_countdown');

function do_this_hourly() {
	

	$args = array(
    'post_type'=> 'event',  
    'order'    => 'ASC',
	'orderby' => 'date',
    );    
	
	$post_id = xx; // ID of the post/page where the countdown is integrated

	$the_query = new WP_Query( $args );

	$time = substr($the_query->posts[0]->post_date, 0, -3);
	echo $time.' ';
	if ($time!="")
	{
		$content = get_post($post_id)->post_content;
		$new_content = preg_replace('/end_time="[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}"/', 'end_time="'.$time.'"', $content);

		$events = array(
			  'ID'           => $post_id,      
			  'post_content' => $new_content,
		  );
		wp_update_post( $events );

		
	}
}

 Do not forget to change the id of the post/page where the countdown is integrated.


I hope this helps!

Login or Signup to post a comment