問題描述
我剛剛安裝了 Yoast SEO 插件,注意到一旦插件被激活,就會在管理欄中添加一個新的彈出菜單。
任何人都知道有沒有一個教程或示例如何做到這一點?
最佳解決方案
動作鈎 admin_bar_menu 做這個工作。這是核心文件中的註釋:
It includes the action “admin_bar_menu” which should be used to hook in and add new menus to the admin bar. That way you can be sure that you are adding at most optimal point, right before the admin bar is rendered. This also gives you access to the $post global, among others.
用法:
add_action('admin_bar_menu', 'add_items');
function add_items($admin_bar)
{
//echo "<pre>";
//print_r($admin_bar);
//echo "<pre>";
$admin_bar->add_menu( array(
'id' => 'my-item',
'parent' => 'top-secondary',
'title' => 'My Item',
'href' => '#',
'meta' => array(
'title' => __('My Item'),
),
) );
}
你必須調整一下,因為這個代碼將新的項目添加到最左邊!我認為檢查 $admin_bar 變量應該給你一個如何添加一個項目的想法。
更新:'parent' => 'top-secondary'將項目添加到’Howdy…’ 的左側!所以我敢打賭還有其他這樣的位置值。我正在找他們;)
更新 2:'parent' => 'new-content'將項目添加到 New > Post, Media 等菜單中。
更新 3:'parent' => 'wp-logo-external'添加從左側 WordPress 徽標彈出的菜單中的項目。
更新:
我已經寫了一個 blog post 與所有可能的位置在管理員菜單中的新項目… 如果有興趣請 read here 。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
