[Solved] Hide and display comment form according to daily schedule  

  RSS

maroelamedia
(@maroelamedia)
Member Customer
Joined: 2 years ago
Posts: 2
21/11/2019 2:11 pm  

Hi there

We need to constantly moderate comments on our website but it's a massive job and want to prevent visitors from leaving comments after hours each day.

We have done this before with WordPress's built-in comments by switching the comments template with the filter 'comments_template' if the current time is after 19:30 and before 05:30.

Now we plan on switching to wpDiscuz. Which filter should I use to hide the form and display a custom message in it's place after hours?

Any help would be greatly appreciated. The current code follows:

// Filters
if ( mm_is_afterhours() ) {
add_filter( 'comments_template', 'mm_comm_schedule_template', 10 );
add_filter( 'comment_reply_link', 'mm_comm_schedule_reply_remove', 10 );
}

function mm_is_afterhours() {
$tz = 'Africa/Johannesburg';
$timestamp = time();
$dt = new DateTime( "now", new DateTimeZone( $tz ) ); //first argument "must" be a string
$dt->setTimestamp( $timestamp ); //adjust the object to correct timestamp

$current_time = $dt->format( 'Gi' ); //24 hour (without leading zero) and minutes

if ( 1930 <= $current_time || $current_time < 530 ) {
// After 19:30 or before 05:30
return true;
} else {
return false;
}
}

 

This topic was modified 2 months ago by Elvina

Topic Tags
Elvina
(@elvina)
Reputable Member Moderator
Joined: 5 months ago
Posts: 435
21/11/2019 3:19 pm  

Hi @maroelamedia,

I've just asked our developers and currently I'm waiting for the response from them.
I'll update the topic asap.


Elvina
(@elvina)
Reputable Member Moderator
Joined: 5 months ago
Posts: 435
22/11/2019 1:15 pm  

Hi @maroelamedia,

// Filters
if ( mm_is_afterhours() ) {
add_filter( 'comments_template', 'mm_comm_schedule_template', 10 );
add_filter( 'comment_reply_link', 'mm_comm_schedule_reply_remove', 10 );
}

function mm_is_afterhours() {
$tz = 'Africa/Johannesburg';
$timestamp = time();
$dt = new DateTime( "now", new DateTimeZone( $tz ) ); //first argument "must" be a string
$dt->setTimestamp( $timestamp ); //adjust the object to correct timestamp

$current_time = $dt->format( 'Gi' ); //24 hour (without leading zero) and minutes

if ( 1930 <= $current_time || $current_time < 530 ) {
// After 19:30 or before 05:30
return true;
} else {
return false;
}
}

Please replace the red mark code with the following.

if (mm_is_afterhours()) {
add_filter('comments_open', '__return_false');
add_action('wpdiscuz_comment_form_closed', function () {
echo '//Here should be your custom message';
});
}

Save, delete all caches and check again.


maroelamedia
(@maroelamedia)
Member Customer
Joined: 2 years ago
Posts: 2
22/11/2019 4:55 pm  

Hi Elvina, thank you so much!


Share: