Allow comments after x hours
-
Awesome question.
Let’s do this programmatically.Are there any other variables re: the post that would need to be met ?
Categories, tags, etc?
Or would it simply apply for all posts ?– get a list of posts where the comments are set to Off
– get the Published date of those posts
– do a time compare between the server and the post
– if there is a XX Hours between current time and Published Date, turn comments OnYes ?
@corrinarusso Well just when a post is published ( not made – not pending – not draft )
Post published -> Check time -> Open comments after x hours or x days on that post.
I think its enuff.
Use the ‘comments_open’ filter to control whether a particular post’s comments are open or not.
There are certain date/time functions to avoid and ones you should use. See https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/
I like to determine time differences using timestamps, so I’d use get_post_timestamp() and subtract it from the current timestamp to see how long ago it was published. You could instead use get_post_datetime() and use DateTimeImmutable::diff() to figure the time since publishing.
Bump!
Hey, Tri — please don’t bump topics. Not only is it against our forum guidelines, it rarely has the effect you desire. Forum topics are listed by original post date, bumping doesn’t change its position in the list.
I’m sorry my replies have been too esoteric for you to use. You could find a more experienced coder to interpret my comments into working code. If you don’t know of such a person, you could hire experienced help through resources like https://jobs.wordpress.net/ or https://jetpack.pro/. Or have some patience and wait and see if anyone here will be willing to provide the code you’re looking for.
Ok i saw that it didnt bump now i know.
But there is a function which closes comments after x days built in wordpress and exists in settings.
In what file can i find that function? So i can see the code and maybe learn something.
Hi Tri – agree with the bumping.
AFAIK, there is nothing out of the box to accomplish this.
But, I do find it an interesting request – I’ll put some time into a cron script this week that should accommodate.@corrinarusso yes im googling right now and trying what i can with those skills i got.
Wierd that this not exists. Close comments exists but not open.
I would do this :
– get a list of posts where the comments are set to Off
– get the Published date of those posts
– do a time compare between the server and the post
– if there is a XX Hours between current time and Published Date, turn comments OnAnd use this :
https://developer.wordpress.org/reference/functions/wp_list_pages/
and the comment_status (open|closed) parameter from here :
https://developer.wordpress.org/cli/commands/post/update/
and a simple time compare :
https://wordpress.stackexchange.com/questions/316788/how-to-get-time-difference-between-publish-date-and-nowWhat do you mean by this?
– do a time compare between the server and the post
If a post is published 14:00 its published 14:00 not 15:00 or what am i missing?
You want to Publish a Post, then you want to turn comments to Open after 4 hours.
You need a process cron on the server to cycle through and check this.
So if it were, I would write a cron that runs say every 15 minutes.The cron gets the Published time of the post :
https://developer.wordpress.org/reference/functions/get_post_time/
The cron gets the current_time(now) on the server :
https://stackoverflow.com/questions/470617/how-do-i-get-the-current-date-and-time-in-phpThen you need to do a compare.
Or …….. you could just get the Published Post time :
https://developer.wordpress.org/reference/functions/get_post_time/
then you may only care about the time that has past, and you could just use + or similiar, like this :
$later = date( ‘Y-m-d H:i:s’, strtotime( $now ) + 11400 ); // eq 4 hoursThere are many approaches you could take to accomplish this task.
The topic ‘Allow comments after x hours’ is closed to new replies.
(@4ever16)
5 years, 3 months ago
Is there a way to allow comments in a post only after x hours or x days?