Limit tags to a pre-defined list in bbpress?

Create a plugin using the code below:

<?php
/*

Plugin Name: Restrict Topic Tags

Description: Restricts tags to a pre-defined list.

Author: _mr_raptor_

Version: 0.1

*/

$allowed_tags = array(

'test',

'test2',

);

function restrict_topic_tags_form( $args = null )

{

$defaults = array( 'topic' => 0, 'submit' => __('Add »'), 'list_id' => 'tags-list' );

$args = wp_parse_args( $args, $defaults );

extract( $args, EXTR_SKIP );



if ( !$topic = get_topic( get_topic_id( $topic ) ) ) {

return false;

}

if ( !bb_current_user_can( 'edit_tag_by_on', bb_get_current_user_info( 'id' ), $topic->topic_id ) ) {

return false;

}

global $page, $allowed_tags;

$current_tags = bb_get_topic_tags( $topic->topic_id );

foreach ($current_tags as $tag_key => $tag ) {

if( in_array( $tag->name, $allowed_tags ) )

unset( $allowed_tags[ $tag_key ] );

}


if( is_array( $allowed_tags ) && !empty( $allowed_tags ) ) { ?> <form id="tag-form" method="post" action="
	<?php bb_uri('tag-add.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN); ?>" class="add:
	<?php echo esc_attr( $list_id ); ?>:">
 <p>
  <select name="tag" id="tag">
   <option value=""> <?php _e("Select a tag")?> </option> <?php	foreach( $allowed_tags as $tag ) { ?> <option value="
				<?php echo $tag?>"> <?php echo $tag?> </option> <?php	} ?>
  </select>
  <input type="hidden" name="id" value="
			<?php echo $topic->topic_id; ?>" />
  <input type="hidden" name="page" value="
				<?php echo $page; ?>" /> <?php bb_nonce_field( 'add-tag_' . $topic->topic_id ); ?> <input type="submit" name="submit" id="tagformsub" value="
					<?php echo esc_attr( $submit ); ?>" />
 </p>
</form> <?php

} // End if



} // End function



function restrict_topic_tags( $tag ) {



global $allowed_tags;



//	if( !in_array( $tag, $allowed_tags ) )

//		return array();



return array($tag);

}


add_filter( 'bb_add_topic_tags', 'restrict_topic_tags' )

Then in your theme’s 

topic-tags.php

, change 

<?php tag_form(); ?>

 to:

<?php

if( function_exists( 'restrict_topic_tags_form' ) )

restrict_topic_tags_form();

else

tag_form();

?>

Leave a comment

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