纯代码让wordpress作者发布文章时只能看到自己关联的文章和图片
<?php
/**
* Plugin Name: Author Content Restriction
* Description: 限制作者只能查看和管理自己的文章和媒体
* Version: 1.0
* Author: Your Name
*/
// 限制作者只能看到自己的文章
function restrict_authors_to_their_posts( $query ) {
if ( ! current_user_can( 'edit_others_posts' ) && $query->is_admin ) {
global $pagenow;
if ( 'edit.php' === $pagenow || 'upload.php' === $pagenow ) {
$query->set( 'author', get_current_user_id() );
}
}
return $query;
}
add_filter( 'pre_get_posts', 'restrict_authors_to_their_posts' );
// 限制作者只能编辑自己的文章
function restrict_authors_edit_access( $capabilities, $cap, $user_id, $args ) {
if ( in_array( $cap, array( 'edit_post', 'delete_post' ) ) ) {
$post_id = isset( $args[0] ) ? $args[0] : 0;
if ( $post_id ) {
$post = get_post( $post_id );
if ( $post && $post->post_author != $user_id && ! current_user_can( 'edit_others_posts' ) ) {
$capabilities[] = 'do_not_allow';
}
}
}
return $capabilities;
}
add_filter( 'user_has_cap', 'restrict_authors_edit_access', 10, 4 );
// 限制作者媒体库访问
function restrict_media_library() {
if ( ! current_user_can( 'edit_others_posts' ) ) {
global $pagenow;
if ( 'upload.php' === $pagenow || 'media-new.php' === $pagenow ) {
$user_id = get_current_user_id();
$GLOBALS['wp_query']->query_vars['author'] = $user_id;
}
if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
add_filter( 'ajax_query_attachments_args', 'restrict_ajax_attachments' );
}
}
}
add_action( 'load-upload.php', 'restrict_media_library' );
add_action( 'load-media-new.php', 'restrict_media_library' );
add_action( 'load-post.php', 'restrict_media_library' );
add_action( 'load-post-new.php', 'restrict_media_library' );
function restrict_ajax_attachments( $query ) {
if ( ! current_user_can( 'edit_others_posts' ) ) {
$query['author'] = get_current_user_id();
}
return $query;
}
// 移除仪表盘的一些统计信息
function remove_dashboard_widgets() {
if ( ! current_user_can( 'edit_others_posts' ) ) {
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
remove_meta_box( 'dashboard_secondary', 'dashboard', 'side' );
}
}
add_action( 'wp_dashboard_setup', 'remove_dashboard_widgets' );
// 自定义仪表盘显示
function custom_dashboard_widgets() {
if ( ! current_user_can( 'edit_others_posts' ) ) {
global $wp_meta_boxes;
wp_add_dashboard_widget(
'custom_quick_press',
'快速发布文章',
'wp_dashboard_quick_press'
);
wp_add_dashboard_widget(
'custom_recent_drafts',
'最近的草稿',
'wp_dashboard_recent_drafts'
);
}
}
add_action( 'wp_dashboard_setup', 'custom_dashboard_widgets' );
// 隐藏后台不必要的菜单
function remove_admin_menus() {
if ( ! current_user_can( 'edit_others_posts' ) ) {
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'tools.php' );
remove_menu_page( 'options-general.php' );
remove_menu_page( 'plugins.php' );
remove_menu_page( 'themes.php' );
remove_menu_page( 'users.php' );
remove_menu_page( 'upload.php' ); // 如果想完全隐藏媒体菜单
}
}
add_action( 'admin_menu', 'remove_admin_menus' );
// 限制作者查看分类和标签
function restrict_taxonomy_access() {
if ( ! current_user_can( 'edit_others_posts' ) ) {
global $pagenow;
if ( in_array( $pagenow, array( 'edit-tags.php', 'term.php' ) ) ) {
wp_die( '您没有权限访问此页面。', '权限不足', array( 'response' => 403 ) );
}
}
}
add_action( 'admin_init', 'restrict_taxonomy_access' );
将上述代码保存为 PHP 文件(例如author-content-restriction.php),然后上传到 WordPress 网站的wp-content/plugins/目录下。之后在 WordPress 后台的插件管理页面激活该插件即可。
这个插件实现了以下功能:
- 限制作者只能查看和编辑自己发布的文章
- 限制作者只能访问自己上传的媒体文件
- 简化了作者的管理后台界面,移除了不必要的菜单和仪表盘小部件
- 阻止作者访问分类和标签管理页面
请注意,使用前建议先在测试环境中验证,确保符合您的需求。如果有特定的自定义需求,可以根据实际情况调整代码。
提示:仅供参考