尝试在外部php文件中编写函数来管理管理菜单可见性

时间:2015-01-13 作者:wxT3APyGfkYJVK

我正在加载我的php文件,将其放入我的主题函数中。php:

require get_stylesheet_directory() . \'/inc/manage-menus.php\';
alter_menu(\'my-user\', \'plugins.php\', false);
管理菜单中的代码。php如下所示:

function alter_item ($user, $items, $action) {
    global $current_user, $menu;
    get_currentuserinfo();

    switch ($action) {
        case false:
            if ($current_user->user_login == $user) {
                remove_menu_page ($items);
            }
            break;

        case true:
            if ($current_user->user_login == $user) {
                //Do something else
            }
            break;
    }   
}

function alter_menu($user, $items, $action) {
    add_action( \'admin_menu\', \'alter_item\' );
    alter_item( $user, $items, $action );           
}
我想当我使用admin\\u菜单挂钩时,我应该出于安全考虑。。。但它告诉我:

Call to undefined function remove_menu_page() in ....
有什么想法吗?

PSI尝试了一种OOP方法,这给了我同样的结果:

管理菜单。php:

abstract class admin_menu_alterations {
    function hook_alter_menu(){
        echo \'executing hook\';
        add_action( \'admin_menu\',array( $this, \'alter_menu\' ) );
    }
    public abstract function alter_menu($user, $items);
}

class hide_admin_menu_items extends admin_menu_alterations {
    public function alter_menu($user, $items) {
        $this->hook_alter_mennu();

        global $current_user;
        get_currentuserinfo();  

        echo $current_user->user_login;
        if ($current_user->user_login == $user) {
            remove_menu_page ($items_to_be_removed);
        }   
    }
}
功能。php:

$hide_menu_items = new hide_admin_menu_items();
$hide_menu_items->alter_menu("my-user", "plugins.php");

2 个回复
最合适的回答,由SO网友:wxT3APyGfkYJVK 整理而成

我认为МиаиСиииииииииииииииииииииииииииииииииииииииииииииииииииёииииииииииааааааааааааааааааааааааааааааа1072

功能。php:

$menu_alterations   = array(
    \'user\'          => array(\'my-userA\', \'my-userB\'),
    \'items\'         => array(\'plugins.php\', \'options-general.php\'),
    \'action\'        => false,
);  

include get_template_directory() . \'/inc/manage-menus.php\';
/inc/管理菜单。php:

add_action( \'admin_menu\', function() use ($menu_alterations) {
    global $current_user, $menu;
    get_currentuserinfo();
    foreach ($menu_alterations[\'user\'] as $user) {
        switch ($menu_alterations[\'action\']) {
            case false:
                if ($current_user->user_login == $user) {
                    foreach ($menu_alterations[\'items\'] as $item) {
                        remove_menu_page ($item);
                    }
                }
            break;
        }
    }
} );

SO网友:Михаил Семёнов

这是因为您正在调用action外部的alter\\u item函数admin_menu

这是我刚刚做的一个工作示例,试着自己找出你的问题所在,如果你失败了,我会解释

add_action( \'admin_menu\', \'alter_items\' );
function alter_items() {
    global $current_user, $menu;
    get_currentuserinfo();
    $scopes = apply_filters( \'alter_items\', array() );
    if( ! empty( $scopes ) ) {
        foreach( $scopes as $scope ) {
            switch ($scope[\'action\']) {
                case false:
                    if ($current_user->user_login == $scope[\'user\']) {
                        remove_menu_page( $scope[\'items\'] );
                    }
                    break;

                case true:
                    if ($current_user->user_login == $scope[\'user\']) {
                        //Do something else
                    }
                    break;
            }
        }
    }
}
function alter_menu($user, $items, $action) {
    add_filter( \'alter_items\', function( $scope ) use( $user, $items, $action ) {
        //echo \'<pre>\'; var_dump( $user, $items, $action ); echo \'</pre>\';
        $scope[] = array(
            \'user\' => $user,
            \'items\' => $items,
            \'action\' => $action
        );
        return $scope;
    } );
}

alter_menu(\'admin\', \'plugins.php\', false);
PS:请注意,您至少需要PHP 5.3.0

结束

相关推荐

Custom Post Row Actions

我偶然发现this question 在写这个问题的时候。我有一个问题是关于这个问题的。我发现你用的是get_delete_post_link 筛选为我的操作创建一个新的url(或一个类似的函数——在任何情况下,我都会将该函数与布尔值一起使用)。唯一的问题是,I don\'t know how to capture the event now. 考虑到我在谷歌上找不到很多关于行后操作的例子,我将不胜感激-/public function _wp_filter_get_delete_post_link( $