Skip to:
Content
Pages
Categories
Search
Top
Bottom

Auto close topic after some time / days

  • @jsantana

    Participant

    Hello, I’m new to wordpress and bbPress and I’m trying to find an option to close topics after a some time, for example, after 2 days the topic was created.

    How may I do this? Do I have to edit the code? If so, can someone explain step by step, as I have never worked with PHP before.

    Thanks.

Viewing 25 replies - 1 through 25 (of 33 total)
  • @robin-w

    Moderator

    ok, so how tech savvy are you do you know

    1. how to create a notepad/notepad++ file and copy/paste code into it?
    2. FTP this to your wordpress site?

    Come back and let me know

    @jsantana

    Participant

    erm, yes, pretty much everything web relate, but not the language itself ‘PHP’. Also not an expert with wordpress. Just asking to get step by step as I’ve seen answers here with just code, without explaining where to insert it.

    @robin-w

    Moderator

    no problem

    copy this code into a new notepad/notepad++ page

    <?php 
    
    /*
    Plugin Name: BBPress Close Old Posts
    Description: Close BBPress 2.0+ posts that haven't been updated in X days. 
    Author: Raygun
    Version: 0.1
    Author URI: http://madebyraygun.com
    
    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    */ 
    
    register_activation_hook(__FILE__, 'bbpress_topic_scheduler');
    
    add_action('bbpress_daily_event', 'bbpress_close_old_topics');
    
    function bbpress_topic_scheduler() {
     wp_schedule_event(time(), 'daily', 'bbpress_daily_event');
    }
    
    function bbpress_close_old_topics() {
    	// Auto close old topics
    	$topics_query = array(
    		'author' => 0,
    		'show_stickies' => false,
    		'parent_forum' => 'any',
    		'post_status' => 'publish',
    		'posts_per_page' => -1
    	);
    	if ( bbp_has_topics( $topics_query ) )
    		while( bbp_topics() ) {
    			bbp_the_topic();
    			$topic_id = bbp_get_topic_id();
    			$last_active = strtotime( get_post_meta( $topic_id, '_bbp_last_active_time', true ) );
    			if ($last_active < strtotime( '-10 days') )
    				bbp_close_topic( $topic_id );
    		}
    }
    ?>
    

    Change the -10 days right near the bottom to whatever number you want

    Save this file as ‘close_old_topics.php’

    Compress/zip this file, so you have a zipped version.

    then in worpress go to

    Dashboard>plugins>add new and click ‘upload’ and then upload the zipped file you saved above, and then activate.

    Come back if you have any issues, the code is not mine and I have not tested it, but others have used.

    @jsantana

    Participant

    Hi there, thanks for the code, I haven’t tested it but the installation went smoothly and now I have it active in the plugins section.

    But this isn’t what I’m looking for specifically. What I would like is to have it close after X amount of days even if it has been updated. This code just do it but after it hasn’t been any replies, right?

    Thanks for taking time in answering, really appreciate it.

    @robin-w

    Moderator

    no problem

    Alter the lines

    $last_active = strtotime( get_post_meta( $topic_id, '_bbp_last_active_time', true ) );
    if ($last_active < strtotime( '-10 days') )
    

    to

    $topic_date = strtotime( get_post( $topic_id, 'post_date', true ) );
    if ($topic_date < strtotime( '-10 days') )
    

    should do it

    @jsantana

    Participant

    ah, thanks!

    I forgot to mention that I want that feature to happen in a specific sub-forum, let say in ‘Announcements’. Is there a way to catch the ID of that specific sub-forum to make this function happen only there and not everywhere else?

    @robin-w

    Moderator

    Hope you haven’t forgotten anything else !

    ok edit

    $last_active = strtotime( get_post_meta( $topic_id, '_bbp_last_active_time', true ) );
    			if ($last_active < strtotime( '-10 days') )
    				bbp_close_topic( $topic_id );
    
    

    to

    $topic_date = strtotime( get_post( $topic_id, 'post_date', true ) );
    $forum_id = bbp_get_topic_forum_id($topic_id);
    if ($topic_date < strtotime( '-10 days') && $forum_id == 210 )
    bbp_close_topic( $topic_id );
    

    the $forum_id == 210 needs the forum id of the forum you want to restrict.

    to find this, go into

    Dashboard>forums>all Forums and hover of edit of the forum you want – eg hover over edit ‘announcements’ and look in the bottom right of the screen. you’ll see the edit url that will include post=210 or whatever number.

    You’ll need to test that all the above works, and come back if you need further help

    @jsantana

    Participant

    Thank you so much and sorry.

    Seems like the auto close (I tested it for -1 days) worked. Now I’m testing that it close the topic only in the ‘Announcements’ sub-forum and not everywhere else. I’ll let you know tomorow since it takes a day :l

    @robin-w

    Moderator

    Hey great that the first bit works, let me know re the second, we’ll get it working if not šŸ™‚

    @jsantana

    Participant

    hi Robin, it seems to be working. I know it closing after x days, is there a way to change it to hours instead to be more precise? Since if you choose days, let say you want it to close after 1 day, then if you post the topic at 23:59 it will close after 1 minute (at least that’s what I think) right?

    Thanks!

    @robin-w

    Moderator

    yes just use ‘hours’ instead of ‘days’

    eg

    if ($last_active < strtotime( '-48 hours') )
    

    @alexandercnb

    Participant

    Should this still work? It doesn’t seem to close the topics in my case. Just wondering if it’s me doing something wrong or that this doesn’t apply anymore.
    WordPress version: 4.5.2
    bbPress version: 2.5.9
    Child theme: Canvas WooThemes

    This is the code I’ve got in the plugin now, from all the code @robin-w provided.

    <?php 
    
    /*
    Plugin Name: BBPress Close Old Posts
    Description: Close BBPress 2.0+ posts that haven't been updated in X days. 
    Author: Raygun
    Version: 0.1
    Author URI: http://madebyraygun.com
    
    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    */ 
    
    register_activation_hook(__FILE__, 'bbpress_topic_scheduler');
    
    add_action('bbpress_daily_event', 'bbpress_close_old_topics');
    
    function bbpress_topic_scheduler() {
     wp_schedule_event(time(), 'daily', 'bbpress_daily_event');
    }
    
    function bbpress_close_old_topics() {
    	// Auto close old topics
    	$topics_query = array(
    		'author' => 0,
    		'show_stickies' => false,
    		'parent_forum' => 'any',
    		'post_status' => 'publish',
    		'posts_per_page' => -1
    	);
    	if ( bbp_has_topics( $topics_query ) )
    		while( bbp_topics() ) {
    			bbp_the_topic();
    			$topic_id = bbp_get_topic_id();
    			$topic_date = strtotime( get_post( $topic_id, 'post_date', true ) );
                    $forum_id = bbp_get_topic_forum_id($topic_id);
                    if ($topic_date < strtotime( '-1 day') && $forum_id == 1276 )
                        bbp_close_topic( $topic_id );
    		}
    }
    ?>

    @sbask

    Participant

    May I hire you to write a plug in that would allow SOME sections of bbpress to be closed after a year but others to stay open indefinitely?

    @robin-w

    Moderator

    contact me via

    Contact me

    @dimitri333

    Participant

    @robin-w Iā€™m trying to find an option to close topics (specific forum) after a some time, for example, after 1 days the topic was created. But this discussion is over 6 years old and doesn’t seem to work. I would simply like to close the various threads of a specific forum after 1 day (H 24) of creation. it can be done? TY

    @robin-w

    Moderator

    the code above should still be fine, I presume you have changed the

    && $forum_id == 1276

    to the correct forum number?

    @dimitri333

    Participant

    register_activation_hook(FILE, ‘bbpress_topic_scheduler’);

    add_action(‘bbpress_daily_event’, ‘bbpress_close_old_topics’);

    function bbpress_topic_scheduler() {
    wp_schedule_event(time(), ‘daily’, ‘bbpress_daily_event’);
    }

    function bbpress_close_old_topics() {
    // Auto close old topics
    $topics_query = array(
    ‘author’ => 0,
    ‘show_stickies’ => false,
    ‘parent_forum’ => ‘any’,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => -1
    );
    if ( bbp_has_topics( $topics_query ) )
    while( bbp_topics() ) {
    bbp_the_topic();
    $topic_id = bbp_get_topic_id();
    $topic_date = strtotime( get_post( $topic_id, ‘post_date’, true ) );
    $forum_id = bbp_get_topic_forum_id($topic_id);
    if ($topic_date < strtotime( ‘-5 hours’) && $forum_id == 9547 )
    bbp_close_topic( $topic_id );
    }
    }

    Ps: code add to functions.php

    @dimitri333

    Participant

    @robin-w what do you think?

    @robin-w

    Moderator

    that’s basically the code which can be added to a functions file, or using

    Code Snippets

    @dimitri333

    Participant

    @robin-w I installed the plugin, then I entered the code (mentioned above) in the code php section of the plugin .. in the end I saved. Now I wait 1 hour and see if it works

    @leon1912

    Participant

    Hello!
    I used the code from @robin-w and pasted it into a code snippet but it does not seem to work. That is my code:

    register_activation_hook(__FILE__, 'bbpress_topic_scheduler');
    
    add_action('bbpress_daily_event', 'bbpress_close_old_topics');
    
    function bbpress_topic_scheduler() {
     wp_schedule_event(time(), 'daily', 'bbpress_daily_event');
    }
    
    function bbpress_close_old_topics() {
    	// Auto close old topics
    	$topics_query = array(
    		'author' => 0,
    		'show_stickies' => false,
    		'parent_forum' => 'any',
    		'post_status' => 'publish',
    		'posts_per_page' => -1
    	);
    	if ( bbp_has_topics( $topics_query ) )
    		while( bbp_topics() ) {
    			bbp_the_topic();
    			$topic_id = bbp_get_topic_id();
    			$topic_date = strtotime( get_post( $topic_id, 'post_date', true ) );
                    $forum_id = bbp_get_topic_forum_id($topic_id);
                    if ($topic_date < strtotime( '-2 hours') ) // && $forum_id == 1276 )
                        bbp_close_topic( $topic_id );
    		}
    }

    The auto close time is set to 2 hours to test it and I created a comment after 2 hours because it should happen in every forum.

    Have I done anything wrong? Thanks in advance! šŸ™‚

    @robin-w

    Moderator

    a cron event only runs at the interval specified, so the check event runs once a day ie

    wp_schedule_event(time(), 'daily', 'bbpress_daily_event');

    so would only close topics older than 2 hours once a day.

    suggest changing that line to

    wp_schedule_event(time(), 'hourly', 'bbpress_daily_event');

    so it would run every hour, so topics would be closed between 2 and 3 hours depending on when cron runs in relation to their creation.

    @leon1912

    Participant

    Hey @robin-w,
    thanks for your reply.
    I changed the code right away after you sent the reply.

    But it still does not work and I’m note sure why.

    @robin-w

    Moderator

    you probably need to unschedule and reschedule it.

    add this to see what it is doing

    WP Crontrol

    @leon1912

    Participant

    Thank you again @robin-w!

    Well, the action isn’t even listed under cron events whatsoever.

    Should I add the action manually?

Viewing 25 replies - 1 through 25 (of 33 total)
  • You must be logged in to reply to this topic.