【问题标题】:implementing plugins loaded action hook in wordpress在wordpress中实现插件加载动作钩子
【发布时间】:2013-11-17 04:24:28
【问题描述】:

我的插件代码如下所示,

if (!current_user_can('administrator')){
function hide_post_page_options() {
//global $post;
// Set the display css property to none for add category and add tag functions
$hide_post_options = "<style type=\"text/css\"> .jaxtag { display: none; } #category-adder { display: none; } </style>";
print($hide_post_options);
}
add_action( 'admin_head', 'hide_post_page_options'  );
}

但是当我激活我得到一个错误

Fatal error: Call to undefined function wp_get_current_user()

我可以通过在capabilities.php 中包含pluggable.php 来解决这个问题。但我不认为对这些文件进行更改是更好的方法。因为 wp_get_current_user() 是一个可插入的函数,所以它只有在插件加载后才可用。有没有办法在不更改核心文件的情况下使用它?

【问题讨论】:

  • 您是否尝试过使用此功能get_currentuserinfo();
  • 停用其余插件并检查。如果您没有收到错误,请查找哪些插件存在冲突。特别勾选“Multisite Plugin Manager”。
  • 这不起作用,因为您应该检查用户capability(不是角色)inside回调函数.无论如何,我更喜欢 boomShiva 的方法。

标签: php wordpress


【解决方案1】:

如果不是管理员,我建议您将其从菜单中删除,而不是使用 CSS 隐藏,这将是一种 WordPress 方法

add_action('admin_menu', 'dot1_remove_dahsboard_menu', 111);

function dot1_remove_dahsboard_menu(){
   global $submenu, $menu;

   //to check array key you want to unset in your case, print the array
   echo "<pre>";
   print_r($menu);
   echo "</pre>";

   echo "<pre>";
   print_r($submenu);
   echo "</pre>";
   /* Unset menu array */
   if(  !current_user_can('manage_options')  ){
        unset($menu['10']);
   }

   /* Unset submenu array */
  if(  !current_user_can('manage_options')  ){
        unset($submenu['edit.php']['10']);
  }
}

【讨论】:

  • 您的答案中有一些重复的代码。另外,请检查this note about roles and capabilities
  • 你的副本应该是别的东西,我也更新了 current_user_can 参数,感谢指出:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
相关资源
最近更新 更多