bbPress: "Mark as Solved" for Support Topics
Lets topic authors and moderators mark a topic as solved with one click; solved topics get a "[Solved]" label in their title so others find answers quickly.
Opens WPIDE on
Code
if ( ! function_exists( 'bbp_get_topic_id' ) ) {
return;
}
add_action( 'bbp_template_before_single_topic', function () {
$topic_id = (int) bbp_get_topic_id();
if ( ! $topic_id || ! is_user_logged_in() ) {
return;
}
if ( get_current_user_id() !== (int) bbp_get_topic_author_id( $topic_id ) && ! current_user_can( 'moderate', $topic_id ) ) {
return;
}
$solved = (bool) get_post_meta( $topic_id, '_wpide_solved', true );
$url = wp_nonce_url( add_query_arg( array( 'wpide_solved' => $topic_id ), get_permalink( $topic_id ) ), 'wpide_solved_' . $topic_id );
printf( '<p class="wpide-bbp-solved"><a class="button" href="%1$s">%2$s</a></p>', esc_url( $url ), esc_html( $solved ? 'Mark as unsolved' : 'Mark as solved' ) );
} );
add_action( 'template_redirect', function () {
if ( empty( $_GET['wpide_solved'] ) || ! is_user_logged_in() ) {
return;
}
$topic_id = absint( $_GET['wpide_solved'] );
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'wpide_solved_' . $topic_id ) ) {
return;
}
if ( bbp_get_topic_post_type() !== get_post_type( $topic_id ) || ( get_current_user_id() !== (int) bbp_get_topic_author_id( $topic_id ) && ! current_user_can( 'moderate', $topic_id ) ) ) {
return;
}
if ( get_post_meta( $topic_id, '_wpide_solved', true ) ) {
delete_post_meta( $topic_id, '_wpide_solved' );
} else {
update_post_meta( $topic_id, '_wpide_solved', 1 );
}
wp_safe_redirect( get_permalink( $topic_id ) );
exit;
} );
add_filter( 'bbp_get_topic_title', function ( $title, $topic_id ) {
return get_post_meta( $topic_id, '_wpide_solved', true ) ? '[Solved] ' . $title : $title;
}, 10, 2 );
Notes
The button appears above the topic for its author and moderators. Clicking again marks the topic as unsolved.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.