問題描述
我有 multi-author 網站,我不太舒服,允許所有成員在他們釋出的帖子中輸入 SEO 細節。我希望這隻能看到網站的管理員。有任何想法嗎?
最佳解決方案
它沒有在 Yoast SEO 外掛站點的 API 檔案中說出 ID 是什麼,我沒有安裝的 Yoast 的副本,但根據 yoas-plugin-dir/admin/class-metabox.php 第 144 行,註冊的 meta_box 是;
add_meta_box( 'wpseo_meta', ...etc ); ...
哪個掛在同一個檔案的第 32 行的 add_meta_boxes 鉤上,
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
當然,你可以從編輯螢幕上的元框本身獲取 ID … 無論如何。
您可以執行以下操作,
add_action('add_meta_boxes', 'yoast_is_toast', 99);
function yoast_is_toast(){
//capability of 'manage_plugins' equals admin, therefore if NOT administrator
//hide the meta box from all other roles on the following 'post_type'
//such as post, page, custom_post_type, etc
if (!current_user_can('activate_plugins')) {
remove_meta_box('wpseo_meta', 'post_type', 'normal');
}
}
… 其中 post 型別是您要應用此限制的帖子型別,如 post 或自定義帖子型別一個或多個!
應該做的伎倆
更新:manage_plugins 應該是 activate_plugins – ammended 。
次佳解決方案
您可以使用 remove_meta_box 功能刪除它。
if ( ! current_user_can( 'edit_pages' ) ) {
add_action( 'add_meta_boxes', 'my_remove_wp_seo_meta_box', 100000 );
}
function my_remove_wp_seo_meta_box() {
remove_meta_box( 'wpseo_meta', 'post', 'normal' );
}
請注意 add_action 中的 100000,這樣可以確定 WP SEO metabox 已掛鉤後完成。
第三種解決方案
嘗試將其貼上到外掛或主題 functions.php 檔案中,按照 WordPress SEO plugin(API Docs),由 Yoast 。
if(function_exists('wpseo_use_page_analysis') && !current_user_can('administrator')){
add_filter('wpseo_use_page_analysis', '__return_false');
}
為了避免任何錯誤… 這將檢查以確保該功能存在,然後嘗試隱藏的東西,允許您停用外掛,而不會丟擲錯誤。
您可能需要掛接到 init 才能正常工作,可以這樣做:
function wpse_init(){
if(function_exists('wpseo_use_page_analysis') && !current_user_can('administrator')){
add_filter('wpseo_use_page_analysis', '__return_false');
}
}
add_action('init', 'wpse_init');
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。