admin管理员组文章数量:1297112
I have created a user custom taxonomy called institutions and when an administrator/NationalCouncil role is creating a user account, he or she should be able to allocate that user to one institution. However, I want to set limits on the institution terms i.e. if I have the term "Kyambogo University" I should be able to set a limit for the number of users who can be assigned to this term. When a limit is reached, an error should be displayed. How best can I achieve this? Here is the code below which I used to create a user custom taxonomy.
<? php
// Register Custom Taxonomy
function custom_taxonomy() {
$labels = array(
'name' => _x( 'Institutions', 'Institutions Name', 'user_taxonomy' ),
'singular_name' => _x( 'Institution', 'Institution Name', 'user_taxonomy' ),
'menu_name' => __( 'Institutions', 'user_taxonomy' ),
'all_items' => __( 'All Institutions', 'user_taxonomy' ),
'parent_item' => __( 'Parent Institution', 'user_taxonomy' ),
'parent_item_colon' => __( 'Parent Institution:', 'user_taxonomy' ),
'new_item_name' => __( 'New Institution Name', 'user_taxonomy' ),
'add_new_item' => __( 'Add Institution', 'user_taxonomy' ),
'edit_item' => __( 'Edit Institution', 'user_taxonomy' ),
'update_item' => __( 'Update Institution', 'user_taxonomy' ),
'view_item' => __( 'View Institution', 'user_taxonomy' ),
'separate_items_with_commas' => __( 'Separate Institution with commas', 'user_taxonomy' ),
'add_or_remove_items' => __( 'Add or remove Institutions', 'user_taxonomy' ),
'choose_from_most_used' => __( 'Choose from the most used', 'user_taxonomy' ),
'popular_items' => __( 'Popular Institutions', 'user_taxonomy' ),
'search_items' => __( 'Search Institutions', 'user_taxonomy' ),
'not_found' => __( 'Not Found', 'user_taxonomy' ),
'no_terms' => __( 'No Institutions', 'user_taxonomy' ),
'items_list' => __( 'Institutions list', 'user_taxonomy' ),
'items_list_navigation' => __( 'Institutions list navigation', 'user_taxonomy' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'Institutions', 'user', $args );
}
add_action( 'init', 'custom_taxonomy', 0 );
/**
* Admin page for the 'Institutions' taxonomy
*/
function cb_add_Institutions_taxonomy_admin_page() {
$tax = get_taxonomy( 'Institutions' );
add_users_page(
esc_attr( $tax->labels->menu_name ),
esc_attr( $tax->labels->menu_name ),
$tax->cap->manage_terms,
'edit-tags.php?taxonomy=' . $tax->name
);
}
add_action( 'admin_menu', 'cb_add_Institutions_taxonomy_admin_page' );
/**
* Unsets the 'posts' column and adds a 'users' column on the manage Institutions admin page.
*/
function cb_manage_Institutions_user_column( $columns ) {
unset( $columns['posts'] );
$columns['users'] = __( 'Users' );
return $columns;
}
add_filter( 'manage_edit-Institutions_columns', 'cb_manage_Institutions_user_column' );
/**
* @param string $display WP just passes an empty string here.
* @param string $column The name of the custom column.
* @param int $term_id The ID of the term being displayed in the table.
*/
function cb_manage_Institutions_column( $display, $column, $term_id ) {
if ( 'users' === $column ) {
$term = get_term( $term_id, 'Institutions' );
echo $term->count;
}
}
add_filter( 'manage_Institutions_custom_column', 'cb_manage_Institutions_column', 10, 3 );
/**
* @param object $user The user object currently being edited.
*/
function cb_edit_user_Institution_section( $user ) {
global $pagenow;
$tax = get_taxonomy( 'Institutions' );
/* Make sure the user can assign terms of the Institutions taxonomy before proceeding. */
if ( !current_user_can( $tax->cap->assign_terms ) )
return;
/* Get the terms of the 'Institutions' taxonomy. */
$terms = get_terms( 'Institutions', array( 'hide_empty' => false ) ); ?>
<h3><?php _e( 'Institutions' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="Institutions"><?php _e( 'Allocated Institutions' ); ?></label></th>
<td><?php
/* If there are any Institutions terms, loop through them and display checkboxes. */
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) {
?>
<label for="Institutions-<?php echo esc_attr( $term->slug ); ?>">
<input type="checkbox" name="Institutions[]" id="Institutions-<?php echo esc_attr( $term->slug ); ?>" value="<?php echo $term->slug; ?>" <?php if ( $pagenow !== 'user-new.php' ) checked( true, is_object_in_term( $user->ID, 'Institutions', $term->slug ) ); ?>>
<?php echo $term->name; ?>
</label><br/>
<?php
}
}
/* If there are no Institutions terms, display a message. */
else {
_e( 'There are no Institutions available.' );
}
?></td>
</tr>
</table>
<?php }
add_action( 'show_user_profile', 'cb_edit_user_Institution_section' );
add_action( 'edit_user_profile', 'cb_edit_user_Institution_section' );
add_action( 'user_new_form', 'cb_edit_user_Institution_section' );
/**
* @param int $user_id The ID of the user to save the terms for.
*/
function cb_save_user_Institution_terms( $user_id ) {
$tax = get_taxonomy( 'Institutions' );
/* Make sure the current user can edit the user and assign terms before proceeding. */
if ( !current_user_can( 'edit_user', $user_id ) && current_user_can( $tax->cap->assign_terms ) )
return false;
$term = $_POST['Institutions'];
/* Sets the terms (we're just using a single term) for the user. */
wp_set_object_terms( $user_id, $term, 'Institutions', false);
clean_object_term_cache( $user_id, 'Institutions' );
}
add_action( 'personal_options_update', 'cb_save_user_Institution_terms' );
add_action( 'edit_user_profile_update', 'cb_save_user_Institution_terms' );
add_action( 'user_register', 'cb_save_user_Institution_terms' );
/**
* @param string $username The username of the user before registration is complete.
*/
function cb_disable_Institutions_username( $username ) {
if ( 'Institutions' === $username )
$username = '';
return $username;
}
add_filter( 'sanitize_user', 'cb_disable_Institutions_username' );
/**
* Update parent file name to fix the selected menu issue
*/
function cb_change_parent_file($parent_file)
{
global $submenu_file;
if (
isset($_GET['taxonomy']) &&
$_GET['taxonomy'] == 'Institutions' &&
$submenu_file == 'edit-tags.php?taxonomy=Institutions'
)
$parent_file = 'users.php';
return $parent_file;
}
add_filter('parent_file', 'cb_change_parent_file');
?>
That is a sample screenshot of the user custom taxonomy and that is where I wish to add limits which an admin can set a limit of how many users can be attached to the custom taxonomy term.
本文标签: How to limit the number of users who can be added to a user custom taxonomy
版权声明:本文标题:How to limit the number of users who can be added to a user custom taxonomy? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741647029a2390245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论