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.

How would I go about requiring each post to have no more than one tag?

I would like to require the user to create a tag or choose one previous tag for each post. If you could give me any information, that would be very helpful.

share|improve this question
add comment

3 Answers 3

up vote 0 down vote accepted

Have fun.

This works for any post type (including customs) that supports the 'post_tag' taxonomy. 99.9% of the time, it will apply only to 'post' post type. But we make compatible stuff ;)

/*
Plugin Name: Single Postag
Description: Enforces use of single 'post_tag' taxonomy on select posts.
Author: EarnestoDev
Version: 1.0.0
Author URI: http://www.earnestodev.com/
*/

// Prepare to save new post_tag
function postag_wp_insert_post($post_ID, $post){
    // Don't update if field value was not posted
    if(!isset($_POST['single_postag'])) return;
    // Fix WP slashing madness (I hate this joke)
    $postag = stripslashes(trim($_POST['single_postag']));
    $postag = $postag ? array(trim($postag)) : array();
    // Set object terms, don't append
    wp_set_object_terms($post_ID, $postag, 'post_tag', false);
}
add_action('wp_insert_post', 'postag_wp_insert_post', 10, 2); // 2 args required

// The metaxbox is here
function postag_meta_box($post, $box){
    // Get the tags and only keep names to weld
    $tags = wp_get_post_tags($post->ID);
    // Loop through as references for quick value reassignment
    foreach($tags as &$tag) $tag = $tag->name;
    // If multiple tags are added outside this widget, they are combined on save.
    echo '<p><input type="text" class="widefat" name="single_postag" value="',
        esc_attr(implode(', ', $tags)), '" /></p>';
    // Pimp description so your users can understand this
    echo '<p class="description">', 'Enter a single Tag.', '</p>';
}

// And now hijack the metaxbox
function postag_add_meta_boxes($post_type, $post){
    // Discard default 'post_tag' metabox (Important)
    remove_meta_box('tagsdiv-post_tag', $post_type, 'side');
    // Only add the new metabox if post_type supports 'post_tag' taxonomy
    if(!is_object_in_taxonomy($post_type, 'post_tag')) return;
    // Create a new 'tagsdiv-postag' metabox (you can't reuse the old MB name)
    add_meta_box('tagsdiv-postag', __('Tag'), 'postag_meta_box',
        $post_type, 'side', 'core');
}
add_action('add_meta_boxes', 'postag_add_meta_boxes', 10, 2); // 2 args required

Tested code. PHP 5.3+ Closures used. Convert code yourself to PHP 5.2 compatible variant if you need.

Regards.

share|improve this answer
    
Thanks! Does this go in edit-tags.php? –  Ryan Oct 22 '11 at 14:09
    
NOOOOO! It's code for a new plugin located here: /wp-content/plugins/single-postag.php. Or, in worst case, in functions.php. (I edited it for PHP 5.2, I have a feeling you're not a techie) –  EarnestoDev Oct 22 '11 at 14:16
    
Ah, ok, thanks for the help! –  Ryan Oct 22 '11 at 14:48
    
BTW, it seems like this disables ajax, any way to keep it on there, so the previously created tag can pop up when the user starts typing? –  Ryan Oct 22 '11 at 15:11
    
I just tried but it can't be done in this usage scenario (without new full Ajax implementation). Because if the Ajax list drops down, clicking it needs to add it to Tags in that list with X near it. Ajax is designed for multi-tag functionality in its core. –  EarnestoDev Oct 22 '11 at 15:46
show 1 more comment

Override the tag MetaBox and set only a text field that will be that single Tag. On updates, delete old tags and add new. If you still need help I could produce some code...

share|improve this answer
    
It would help to see the code, there's a lot of things going on in 'edit-tags.php' (which I think is the right file to edit for this) that I don't have a solid grasp on, namely editing with ajax –  Ryan Oct 22 '11 at 13:56
    
Just added it :) –  EarnestoDev Oct 22 '11 at 13:57
add comment

I know this post is old, but here is a nice plugin that has a ton of different posting requirements options, including min/max number of tags and categories, min/max length of content and title, require featured image, max number of links in the post and more - WyPiekacz

I use this plugin on all of my multi-author sites.

share|improve this answer
    
Not an answer. And it is a duplicate ... –  toscho Apr 18 '12 at 7:38
    
Much better answer than 30 lines of code... And, many times the multiple questions require the same response. –  Travis Pflanz Apr 18 '12 at 7:46
    
An answer should be more than a link to an external site. Explain why and how the plugin solves the issue. See also meta.wordpress.stackexchange.com/q/1041/73 –  toscho Apr 18 '12 at 7:52
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.