Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Limit tags to a pre-defined list?

All my code is written against 1.0.2 SVN, so it’s not 0.9-only :) and yeah, $allowed_tags is meant to be changed and then back again, not the cleanest way of doing things, but it should work. The copy that’s working live at the moment is below (I just realised I forgot to turn on the validation code too, oops):

<?php
/*
Plugin Name: Restrict Topic Tags
Description: Restricts tags to a pre-defined list.
Author: Kawauso
Version: 0.1.1
*/

$allowed_tags = array(
'test4',
'test 3',
'test3',
'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 );
$allowed_tags = array_flip( $allowed_tags ); // CHANGE PLACES!
foreach( $current_tags as $tag ) {
if( isset( $allowed_tags[ $tag->name ] ) )
unset( $allowed_tags[ $tag->name ] );
}
$allowed_tags = array_flip( $allowed_tags ); // CHANGE PLACES!

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' );

Skip to toolbar