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'm writing a plugin, I want to prevent posts have multiple categories (i.e same product having category "Samsung" & "Apple"). Any idea on how to use some actions/filters to achieve this using my plug-in?

share|improve this question
add comment

2 Answers

There are jquery plugins that change category checkboxes to radio buttons Making category selection radio buttons. This however is not a pure safe solution. What you can do is using

add_action('publish_post', 'your_function');

Do a var_dump in you function to see what is actually going on. Then you will also see if there are more than on category checked.

function your_function($content){
  var_dump($content);

  //Check if more than one category is checked, return false and don't publish
}

You can also look at: Running a function in Wordpress when publishing a custom post type

share|improve this answer
add comment

Try the following code. What this would do is convert the category taxonomy checkboxes to radio buttons. In this way only one category can be selected.

add_filter('wp_terms_checklist_args', 'wpse_64691_one_category_only', '', 2);
function wpse_64691_one_category_only( $args, $post_id){
    $args['walker'] = new WPSE_64691_Category_Radio;
    return $args;
}

class WPSE_64691_Category_Radio extends Walker {
    var $tree_type = 'category';
    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this

    function start_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = str_repeat("\t", $depth);
            $output .= "$indent<ul class='children'>\n";
    }

    function end_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = str_repeat("\t", $depth);
            $output .= "$indent</ul>\n";
    }

    function start_el( &$output, $category, $depth, $args, $id = 0 ) {
            extract($args);
            if ( empty($taxonomy) )
                    $taxonomy = 'category';

            if ( $taxonomy == 'category' )
                    $name = 'post_category';
            else
                    $name = 'tax_input['.$taxonomy.']';

            $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
            if ( $taxonomy == 'category' )
                $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="radio" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
            else
                $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
    }

    function end_el( &$output, $category, $depth = 0, $args = array() ) {
            $output .= "</li>\n";
    }
}
share|improve this answer
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.