Info
- 10 posts
- 4 voices
- Started 2 years ago by AdamKayce
- Latest reply from 3sixty
- This topic is resolved
Limit tags to a pre-defined list?
-
- Posted 2 years ago #
I'm wanting to limit the number of tags in my forum to a pre-defined list of 10 topics, and have members click/choose one (or more) when posting. Bonus points for having them in a drop-down menu... :)
I saw someone wrote in a post, 8 months ago, "I hacked [my forum] so users would tick on tags that are pre-defined..." which is exactly what I want to do. Any ideas how to do that?
-
- Posted 2 years ago #
bump? any ideas?
-
- Posted 2 years ago #
<?php /* Plugin Name: Restrict Topic Tags Description: Restricts tags to a pre-defined list. Author: Kawauso 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(); ?> -
- Posted 2 years ago #
You, sir, are awesome.
As soon as I can test this out, I will. One question: for the plugin, should I add a ?> to the end of the chunk of code you've put there?
-
- Posted 2 years ago #
Whoops, I need to use more test data next time.
Change:
foreach ($current_tags as $tag_key => $tag ) { if( in_array( $tag->name, $allowed_tags ) ) unset( $allowed_tags[ $tag_key ] ); }to
$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!The closing
?>isn't really important if there's no non-PHP data after it -
- Posted 2 years ago #
Bummer, I can't get it to work. Is the "$allowed_tags" line supposed to be in there twice?
Also, I'm using 1.0.2 - is this an 0.9-only fix, or ?
Thanks for your help.
-
- Posted 2 years ago #
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' ); -
- Posted 2 years ago #
Thanks so much for your help; this is awesome.
Now when I activate it, I get a sweet drop-down menu up in the post tag area to choose from my ten tags, and if I try to add a miscellaneous tag down below, it doesn't take it. That's awesome.
Thanks, Kawauso!
-
- Posted 2 years ago #
To answer your question about the closing
?>being missing, yes you can leave that off. wp-config.php starting coming like that. It concerned me at first but it turns out it's OK and might be a good thing to do.http://activeblogging.com/info/can-you-leave-off-the-closing-php-tag-in-your-source-code/
http://php.net/basic-syntax.instruction-separation -
- Posted 1 year ago #
@chrishajer, thanks for this great plugin. I further extended it by hacking the post-form to have a drop down containing allowed tags (albeit I just hard-coded them in a rush) and also to limit the number of tags allowed per post. I further extended it by only enabling it in certain forums.
These are all fairly Stupid Coding Tricks but if there's any interest out there, let me know what you need and I'll post it up here.
-
You must log in to post.