Try not to write clever code

Sometimes you can really do the same with less, you just have to give it some thought…

function disable_form_in_topics_and_replies( $templates, $slug, $name ) { if ( $slug === 'form' && in_array( $name, [ 'topic', 'reply' ] ) ) { return []; } return $templates; } add_filter( 'bbp_get_template_part', 'disable_form_in_topics_and_replies', 20, 3 );
Code language: PHP (php)

The purpose is to remove the topic and reply forms from bbPress conditionally ( that add_filter is actually used in an if statement). However, it turns out there is a little simpler way to do this. Look:

add_filter( 'bbp_current_user_can_publish_replies', '__return_false' ); add_filter( 'bbp_current_user_can_publish_topics', '__return_false' );
Code language: JavaScript (javascript)

That's it. Nothing else is required. Remove the rights to publish posts and replies and the code above is absolutely unnecessary.

The lesson I keep learning

Whenever you think you're doing something clever, make sure you aren't writing useless code for the sake of writing code. Write the code to achieve a result, not to increase line count in your files. Sure – both approaches above achieve the result, but if I had given it enough thought yesterday, I wouldn't have to write this blog post today. That's my exactly my point – give it some thought. Don't rush to write more code.