WordPress.org

Ready to get started?Download WordPress

Forums

Making category selection radio buttons (8 posts)

  1. emzdesign
    Member
    Posted 4 years ago #

    Hi

    I have a site that has a lot of members and I want to change the post new page so that the categories are radio buttons rather than check boxes, so that they can only choose one category for their post to go into.

    I've tried modifying some functions (wp_dropdown_categories) but nothing I do seems to effect the post new page.

    Does anyone have any ideas or is there a plugin that allows you to do this?

    Thanks in advance.

  2. emzdesign
    Member
    Posted 4 years ago #

    Can anyone help me? Thanks.

  3. gilig
    Member
    Posted 4 years ago #

    I want to do that too. My site has many members. It will be messy if they select many categories at once to boast up their postings.

  4. hcigdem
    Member
    Posted 4 years ago #

    i want to do that too... but stil i couldnt fing

  5. donzacharias
    Member
    Posted 4 years ago #

    I just actually figured out how to do this with JavaScript, I was looking at the same problem.

    In your functions.php:

    add_action( 'admin_footer', 'catlist2radio' );
    function catlist2radio(){
    	echo '<script type="text/javascript">';
    	echo 'jQuery("#categorychecklist input, #categorychecklist-pop input, .cat-checklist input")';
    	echo '.each(function(){this.type="radio"});</script>';
    }

    How/why it works: Insert a script in the footer of all admin pages. It uses jQuery (already present) to change the type of those inputs to radio which by default in the browser only allows 1 to be selected. There are 3 places where that list shows up, the new/edit post page (x2) and in quick edit mode. If there are more, we just need to find the DOM selector for it and add it to that list. That ought to take care of it!

  6. pauro
    Member
    Posted 4 years ago #

    donzacharias, You just save my life... God Bless You

  7. briantempleton
    Member
    Posted 4 years ago #

    Your a genius Don! Thanks so much!

  8. pauro
    Member
    Posted 4 years ago #

    Now I've tried Don's code and tweak it a little more like this:

    <?php
    function the_user_level () {
    	if (is_user_logged_in()) {
    		global $current_user, $wpdb;
    		$userLevel = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE user_id=$current_user->id AND meta_key='wp_user_level'");
    		return $userLevel;
    	} else {
    		return 0;
    	}
    }
    ?>
    <?php
        $accessLevel = the_user_level();
        if ($accessLevel < 8) {
    add_action( 'admin_footer', 'catlist2radio' );
    function catlist2radio(){
    	echo '<script type="text/javascript">';
    	echo 'jQuery("#categorychecklist input, #categorychecklist-pop input, .cat-checklist input")';
    	echo '.each(function(){this.type="radio"});</script>';
    }
     }else {
    	echo '';
        }
    ?>

    The idea is, I want all user with level less then 8 (editor - subscriber) have to choose category with radio button, while admin still allowed to pick more then 1 cats per post with checkboxes.

    The problem is, it works well with FF, Opera, & Safari but in IE, checkbox appeared again.

    Please help

Topic Closed

This topic has been closed to new replies.

About this Topic