Require tags for bbPress topics

I want to require my forum members to enter at least one word into the “tags”-line, when they are creating a new topic.

As regards plugins and solutions, what I found so far is the WyPiekacz-plugin which works well for posts: You can define rules for the data entered. It did not work for the creation of forum posts.

Read More

What I also found is the answer to “require one tag for each post” here in WordPress Answers which contains the code for a small plugin.

While exploring the php-files of bbPress, I added the last two lines to the following snippet within bb_post.php:

$topic = trim( $_POST['topic'] );
$tags  = trim( $_POST['tags']  );

if ('' == $topic)
    bb_die(__('Please enter a topic title'));

if ('' == $tags)
    bb_die(__('Please enter tags'));

In my understanding, that should do the trick – but still it doesn’t. Or am I using the wrong file? My installation uses bbPress within BuddyPress.

Related posts

Leave a Reply

1 comment

  1. In the back end, it’s a matter of using the hook save_post, as topic is a Custom Post Type.

    If no Tags are filled, saving is cancelled and a link is presented so the user can go back to the editing screen.
    But, there’s one caveat: the title is not preserved, if it is a new topic it comes back blank and if it is an existing topic any changes are lost. The content and topic attributes are preserved.
    I’m not quite sure there is a solution for this…

    missing tags

    add_action( 'save_post', 'save_post_wpse_82956', 10, 2 );
    
    /**
     * Require Tags for new bbPress Topic
     */
    function save_post_wpse_82956( $post_id, $post_object ) 
    {
        // Auto save or Ajax
        if ( 
            ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            || ( defined('DOING_AJAX') && DOING_AJAX ) )
            return;
    
        // Correct post_type
        if ( 'topic' != $post_object->post_type )
            return;
    
        // First instance of the post (right after clicking "New Topic")
        if( 'auto-draft' == $post_object->post_status )
            return;
    
        if ( empty( $_POST['tax_input']['topic-tag'] ) ) 
            wp_die( '<a href="javascript:history.go(-1)">' . __( '&laquo; &laquo; Please, add tags to the topic' ) . '</a>' );
    
    }
    

    And in the front end, a couple of bbPress action hooks and a simple check are enough (when posting multiple replies, a Javascript workaround is necessary, check comments):

    add_action( 'bbp_new_topic_pre_extras', 'check_front_end_tags_wpse_82956' );
    add_action( 'bbp_new_reply_pre_extras', 'check_front_end_tags_wpse_82956' );
    
    function check_front_end_tags_wpse_82956()
    {
        if( empty( $_POST['bbp_topic_tags'] ) )
        {
            // WORKAROUND FOR LINKS WITH HASH TAG, e.g., topic/lorem-ipsum#post-91
            // Necessary when posting multiple replies that generate hashed links
            // OTHERWISE A SIMPLE href="javascript:history.go(-1) WOULD WORK
            ?><script>
            function goBack() {
                if (window.history && history.pushState) {
                    history.replaceState("", "", "?no-tags=true);
                    history.go(-1);
                }
            }
            </script><?php
    
            wp_die( '<a href="javascript:goBack()">' . __( '&laquo; &laquo; Please, add tags to the topic' ) . '</a>' );
        }
    }