Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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.

share|improve this question
    
Have you tried anything yet? Found any helpful research? –  s_ha_dum Jan 24 '13 at 15:37
    
I added to the question what I found so far. –  The_Fritz Jan 24 '13 at 15:55
add comment

1 Answer

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>' );
    }
}
share|improve this answer
    
Thank your for your detailed answer with a lot of explanations! This afternoon, I will try it out in the local installation on my laptop. –  The_Fritz Jan 30 '13 at 9:36
    
Tried it. It works! Just to mention it for other newbies: The part for the front end goes to functions.php in the active theme. –  The_Fritz Jan 30 '13 at 10:24
    
I forgot to mention that I tested all the code inside a custom plugin, no functions.php, check: wordpress.stackexchange.com/q/73031/12615 –  brasofilo Jan 30 '13 at 16:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.