0

I am loading my php file by putting this into my theme's functions.php:

require get_stylesheet_directory() . '/inc/manage-menus.php';
alter_menu('my-user', 'plugins.php', false);

The code in manage-menus.php looks like this:

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 );           
}

I though when I use the admin_menu hook I should be on the safe side... but it tells me that:

Call to undefined function remove_menu_page() in ....

Any ideas?


PS I tried an OOP approach, which gave me the same result:

manage-menus.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);
        }   
    }
}

functions.php:

$hide_menu_items = new hide_admin_menu_items();
$hide_menu_items->alter_menu("my-user", "plugins.php");
  • This is too complicated and I wanted to remove this and ask a simpler question. If possible it would be lovely if a moderator could delete it. – sogathon Jan 15 '15 at 10:06
1

It's becouse you'r calling alter_item function outside action admin_menu

here's working example i just made, try to figure what's wrong with your's by your self, if you fail i'll explain

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: note that you need PHP 5.3.0 minimum

| improve this answer | |
  • This seems to work, but seems very complicated. The above procedure is described in the wordpress codex and I am sure there must be an easier way. – sogathon Jan 14 '15 at 14:34
  • 1
    You might skip add_filter in alter_menu and use add_action( 'admin_menu', function() use( $user, $items, $action ).... The thing is that in your examples you'r dubble calling a function alter_menu first time when u parse args and seccond is when the action is heppening.. So in action you don't have variables wich you write to alter_menu function.. As an enothere way you can create global variable and write your parsing variables to global and in action read them, but then, if you call your function later then action it woun't work.. do u undestand? – Михаил Семёнов Jan 15 '15 at 6:44
0

I think Михаил Семёнов is right, but I had to get another push by cybmeta in another thread, which lead to this:

functions.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/manage-menus.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;
        }
    }
} );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.