admin管理员组文章数量:1425660
I have 6 users with an editor role, each editor should have access to 1 category (different for every 6 users).
Besides this, I have another 20 users which are moderated by 1 editor, so 1 editor x 20 users
Could everyone guide through a solution for that?
I used this function in order to achieve this but are some things that aren't working properly, for example:
- I can select the category for each editor
but, each editor could see the posts from the other ones. Also in the frontend, all categories appear.
// Adaugare optiune selectare categorie moderatori //
function restrict_user_form_enqueue_scripts($hook) {
if ( ! in_array($hook, array('profile.php', 'user-edit.php' )))
return;
wp_enqueue_script('jquery');
wp_enqueue_script( 'jquery.multiple.select', get_template_directory_uri() . '/js/jquery.multiple.select.js' );
wp_register_style( 'jquery.multiple.select_css', get_template_directory_uri() . '/css/multiple-select.css', false, '1.0.0' );
wp_enqueue_style( 'jquery.multiple.select_css' );
}
add_filter('pre_option_default_category', 'jam_change_default_category');
function jam_change_default_category($ID) {
// Avoid error or heavy load !
if ( ! is_user_logged_in() )
return $ID;
$user_id = get_current_user_id();
$restrict_cat = get_user_meta( $user_id, '_access', true);
if ( is_array($restrict_cat) ) {
return reset($restrict_cat);
} else {
return $ID;
}
}
/**
* Exclude categories which arent selected for this user.
*/
add_filter( 'get_terms_args', 'restrict_user_get_terms_args', 10, 2 );
function restrict_user_get_terms_args( $args, $taxonomies ) {
// Dont worry if we're not in the admin screen
if (! is_admin() || $taxonomies[0] !== 'category')
return $args;
// Admin users are exempt.
$currentUser = wp_get_current_user();
if (in_array('administrator', $currentUser->roles))
return $args;
$include = get_user_meta( $currentUser->ID, '_access', true);
$args['include'] = $include;
return $args;
//var_dump($include);
}
// Display and save data in admin dashboard
function restrict_user_form( $user ) {
// A little security
if ( ! current_user_can('add_users'))
return false;
$args = array(
'show_option_all' => '',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 0,
'child_of' => 0,
'exclude' => '',
'echo' => 0,
'hierarchical' => 1,
'name' => 'allow',
'id' => '',
'class' => 'postform',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
'walker' => ''
);
$dropdown = wp_dropdown_categories($args);
// We are going to modify the dropdown a little bit.
$dom = new DOMDocument();
//$dom->loadHTML($dropdown, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$dom->loadHTML( mb_convert_encoding($dropdown, 'HTML-ENTITIES', 'UTF-8') );
$xpath = new DOMXpath($dom);
$selectPath = $xpath->query("//select[@id='allow']");
if ($selectPath != false) {
// Change the name to an array.
$selectPath->item(0)->setAttribute('name', 'allow[]');
// Allow multi select.
$selectPath->item(0)->setAttribute('multiple', 'yes');
$selected = get_user_meta( $user->ID, '_access', true);
// Flag as selected the categories we've previously chosen
// Do not throught error in user's screen ! // @JamViet
if ( $selected )
foreach ($selected as $term_id) {
// fixed delete category make error !
if (!empty($term_id) && get_the_category_by_ID($term_id) ){
$option = $xpath->query("//select[@id='allow']//option[@value='$term_id']");
$option->item(0)->setAttribute('selected', 'selected');
}
}
}
?>
<h3><?php _e('Monitori', 'restrict-author-posting'); ?></h3>
<table class="form-table">
<tr>
<th><label for="access"><?php _e('Alege categoria', 'restrict-author-posting') ?>:</label></th>
<td>
<?php echo $dom->saveXML($dom);?>
<span class="description"><?php _e('', 'restrict-author-posting') ?></span>
</td>
</tr>
</table>
<table class="form-table">
<tr>
<th><label for="access"><?php _e('Vezi doar fisierele media din categoria aceasta', 'restrict-author-posting') ?></label></th>
<td>
<fieldset>
<legend class="screen-reader-text"><span><?php _e('Da', 'restrict-author-posting') ?></span></legend>
<label for="_restrict_media">
<input type="checkbox" <?php checked (get_user_meta($user->ID, '_restrict_media', true), 1, 1 ) ?> value="1" id="_restrict_media" name="_restrict_media">
<?php _e('Da', 'restrict-author-posting') ?></label>
</fieldset>
</td>
</tr>
</table>
<script>
<!--
jQuery('select#allow').multipleSelect();
-->
</script>
<?php
}
// Restrict Save Data
function restrict_save_data( $user_id ) {
// check security
if ( ! current_user_can( 'add_users' ) )
return false;
// admin can not restrict himself
if ( get_current_user_id() == $user_id )
return false;
// and last, save it
if ( ! empty ($_POST['_restrict_media']) ) {
update_user_meta( $user_id, '_restrict_media', $_POST['_restrict_media'] );
} else {
delete_user_meta( $user_id, '_restrict_media' );
}
if ( ! empty ($_POST['allow']) ) {
update_user_meta( $user_id, '_access', $_POST['allow'] );
} else {
delete_user_meta( $user_id, '_access' );
}
}
// Remove meta box for non admin
function remove_metaboxes() {
remove_meta_box( 'categorydiv','news','normal' );
remove_meta_box( 'categorydiv','etablissement','normal' );
}
// Save Category News
function save_category_news($post_ID, $post) {
$currentUser = wp_get_current_user();
$cat = get_user_meta( $currentUser->ID, '_access', true);
$cat = array_map('intval', $cat);
wp_set_object_terms($post_ID, $cat, 'category', true);
}
// Save Category Etablissement
function save_category_etablissement($post_ID, $post) {
$currentUser = wp_get_current_user();
$cat = get_user_meta( $currentUser->ID, '_access', true);
$cat = array_map('intval', $cat);
wp_set_object_terms($post_ID, $cat, 'category', true);
}
// Action let's go
add_action( 'admin_enqueue_scripts', 'restrict_user_form_enqueue_scripts' );
add_action( 'show_user_profile', 'restrict_user_form' );
add_action( 'edit_user_profile', 'restrict_user_form' );
add_action( 'personal_options_update', 'restrict_save_data' );
add_action( 'edit_user_profile_update', 'restrict_save_data' );
add_action( 'save_post_news', 'save_category_news', 10, 3 );
add_action( 'save_post_etablissement', 'save_category_etablissement', 10, 3 );
add_action( 'admin_menu', 'remove_metaboxes');
本文标签: permissionsHow to allow each editors to only edit certain categories
版权声明:本文标题:permissions - How to allow each editors to only edit certain categories? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745448557a2658776.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论