Skip to:
Content
Pages
Categories
Search
Top
Bottom

Auto close topic after some time / days


  • jsantana
    Participant

    @jsantana

    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 12 replies - 1 through 12 (of 12 total)

  • Robin W
    Moderator

    @robin-w

    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

    @jsantana

    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

    @robin-w

    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

    @jsantana

    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

    @robin-w

    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

    @jsantana

    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

    @robin-w

    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

    @jsantana

    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

    @robin-w

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


    jsantana
    Participant

    @jsantana

    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

    @robin-w

    yes just use ‘hours’ instead of ‘days’

    eg

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

    AlexanderCnb
    Participant

    @alexandercnb

    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 );
    		}
    }
    ?>
Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.
Skip to toolbar