Blog / Tutorials / bbPress Close All Topics

bbPress Close All Topics

Cristian Antohe
Last Updated: 22/03/16

With our bbPress forums retired, I wanted to close all topics without having to go in each one of them. While I couldn’t find something that did exactly that, I did found this really nice plugin https://github.com/thebrandonallen/bbp-auto-close-topics that automatically close bbPress topics after an admin-specified time period.

So I stripped it down of any UI since we don’t need that, removed the time specific code and we’re left with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
 * Plugin Name: BBP Close All Topics
 * Plugin URI:  https://github.com/thebrandonallen/bbp-auto-close-topics
 * Description: bbPress plugin to close all topics. Inspired by https://github.com/thebrandonallen/bbp-auto-close-topics/blob/master/bbp-auto-close-topics.php
 * Author:      Cristian Antohe
 * Author URI:  http://www.cozmoslabs.com
 * Version:     0.1
 * License:     GPLv2 or later (license.txt)
 */
 
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
 
/**
 * Hook into 'the_posts' and check the topic status on single topic pages. If
 * the topic is not closed, set it to closed.
 *
 * @since 0.1.0
 *
 * @access public
 *
 * @param array $posts Array of post objects.
 * @param object $query WP_Query object.
 * @uses WP_Query::is_singular() To determine if we're on a single topic page.
 * @uses bbp_get_topic_post_type() To get the topic post type.
 * @uses bbp_get_closed_status_id() To get the topic status id.
 * @uses tba_bbp_auto_close_topics() To get auto-close option.
 * @uses tba_bbp_auto_close_age() To get the auto-close age.
 * @uses bbp_get_topic_id() To get the topic id.
 *
 * @return array $posts
 */
function cl_bbp_close_topics_the_posts( $posts, $query ) {
	if ( empty( $posts ) || ! $query->is_singular() ) {
		return $posts;
	}
	// Are we checking a topic?
	if ( $posts[0]->post_type !== bbp_get_topic_post_type() ) {
		return $posts;
	}
	// Are we already closed?
	if ( $posts[0]->post_status === bbp_get_closed_status_id() ) {
		return $posts;
	}
 
	$posts[0]->post_status = bbp_get_closed_status_id();
	return $posts;
}
 
/**
 * Hook into 'the_posts' and check the topic status on single topic pages. If
 * the topic is not closed, set it to closed.
 *
 * @since 0.1.0
 *
 * @access public
 *
 * @param string $status Status of supplied topic id.
 * @param int $topic_id The topic id.
 * @uses bbp_get_closed_status_id() To get the topic status id.
 * @uses bbp_get_topic_id() To get the topic id.
 * @uses bbp_is_topic() To verify we're checking a topic.
 * @uses tba_bbp_auto_close_topics() To get auto-close option.
 * @uses tba_bbp_auto_close_age() To get the auto-close age.
 *
 * @return string $status
 */
function cl_bbp_close_topics_topic_status( $status, $topic_id ) {
	// Bail if topic is already closed
	if ( $status === bbp_get_closed_status_id() ) {
		return $status;
	}
	// Validate topic id
	$topic_id = bbp_get_topic_id( $topic_id );
	// Are we checking a topic?
	if ( ! bbp_is_topic( $topic_id ) ) {
		return $status;
	}
 
	$status = bbp_get_closed_status_id();
 
	return $status;
}
 
/**
 * Only load our filters after bbPress has loaded.
 *
 * @since 0.1.0
 *
 * @access public
 *
 * @uses add_filter() To load our filters.
 */
function cl_bbp_auto_close_topics_loader() {
	add_filter( 'the_posts', 'cl_bbp_close_topics_the_posts', 10, 2 );
	add_filter( 'bbp_get_topic_status', 'cl_bbp_close_topics_topic_status', 10, 2 );
}
add_action( 'bbp_includes', 'cl_bbp_auto_close_topics_loader' );

You can install it as a plugin by creating a file or simply add the code to your functions.php file and you’re good to close all topics in bbPress.

From the blog

Related Articles

roundup wordpress ecosystem january2

Roundup of WordPress ecosystem #1 – January 2017

Author: Patricia Borlovan
Last Updated: February 9th, 2017

After writing the article "Overview of the WordPress Community in 2016" and getting feedback for the article on various platforms, I decided to continue writing them, but I changed its name into "Roundup of WordPress ecosystem". This is the first article from a monthly series that will showcase what happened around the whole ecosystem in […]

Continue Reading
feb_mar_roundup_wp_ecosystem

Roundup of WordPress ecosystem #2 – February & March 2017

Author: Patricia Borlovan
Last Updated: April 11th, 2017

There were two interesting months within the WordPress ecosystem with events that are here to impact the course and development of the platform. We saw WordPress getting important security updates and a brand new design for the plugin repository and also witnessed to a significant acquisition. With this being said, let's dive into the summary […]

Continue Reading

How to Password Protect Content, Posts, and Categories in WordPress

Author: Rishi Lodha
Last Updated: July 9th, 2024

There are many use cases for password-protected content in WordPress. For example, you might be a content creator who wants to monetize premium content in the form of subscriptions or memberships. As with everything related to WordPress, password-protecting posts doesn’t have to be difficult. If you’re wondering how to password-protect WordPress content, posts, and even […]

Continue Reading

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.