Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom taxonomy term picker for a widget WordPress

  • REFUNDED

For a custom plugin / widget I need a way to build a picker for a specific taxonomy, like the ones in the WordPress menus.

What I need is a function/method that gets passed a taxonomy and it returns a picker (<strong>radios</strong>, <em>not checkboxes</em>)

e.g.

echo generate_picker("post_tag");

to return something like the attached screenshot -- and whatever term is selected it updates a hidden input field with its id.

Let me know if this doesn't make sense.

Thanks,
MR

Answers (2)

2012-08-07

Jatin Soni answers:

Here is the function

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


If you want allow admin can select multiple category than use below code

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;
}
}

$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 '';
}


mackrider comments:

Not sure I follow

As I stated in my question --> I want something as easy as

echo generate_picker("post_tag");


Jatin Soni comments:

Where you want to put this? This code you need to place in function.php and nothing require you to do. It will automatically convert all taxonomy checkbox with radio button.


mackrider comments:

I am adding a widget that allows a user to select a term from selected taxonomies (there will be additional functionality, but this is the one piece I was unsure how to build).

What I need is a taxonomy picker that looks like that screenshot for the widgets.

This is going to go in the widget area in wp_admin.

2012-08-08

Arnav Joy answers:

define this function in functions.php


<?php
function generate_picker($tax='category'){
$terms = get_terms($tax);
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
$str .= '<input type="radio" name="picker" value="'.$term->term_id.'" >'.$term->name.'<br>';
}
}

return $str;
}
?>


and use it as you wanted :-

<?php echo generate_picker("post_tag");
?>


now you can use its value after form submission <?php echo $_REQUEST['picker'];?> this will give you selected term's id


mackrider comments:

Looks good. I just need it in the three tabs: most used / all (paginated) / search - use the image as a reference. :)