Unregistering Menu Items

I'm using this code in start.php of my theme and it works fine.

function mysite_init() {

elgg_unregister_menu_item('topbar', 'elgg_logo');

$logo_url = elgg_get_site_url() . "mod/mysite/img/topbar-logo.png";

elgg_register_menu_item('topbar', array(

    'name' => 'my_logo',

    'href' => 'http://www.mysite.com/',

    'text' => "<img src=\"$logo_url\" alt=\"My logo\" width=\"280\" height=\"40\" />",

    'priority' => 1,

    'link_class' => 'elgg-topbar-logo',

));

}

 

I'm wanting to remove some other items within the topbar, so have included these lines:

    elgg_unregister_menu_item('topbar', 'profile');

    elgg_unregister_menu_item('topbar', 'friends');

    elgg_unregister_menu_item('topbar', 'messages');

However, they still remain in the topbar. What am I doing wrong?

  • It's wrong way!

    You need to change the mod's start.php

    Ex., 4 messages:

    Go 2 mod/messages

    Edit start.php as:

    elgg_unregister_menu_item('topbar', array(
                'name' => 'messages',
                'href' => 'messages/inbox/' . elgg_get_logged_in_user_entity()->username,
                'text' => $text,
                'priority' => 600,
            ));

    Do it in other mods/plugin if u need :)

  • @RVR : Burg has done it the correct way. Thats the purpose of using plugin hooks. If we modify the start.php in individual files then we have to modify it each and everytime we do an upgrade of Elgg. So its better to use a common tweak plugin for your system and then make all the modification in your plugin.

    @ Burg, can you paste the complete code here.

  • @Burgs

    I think those three functions do not work because they are executed before the menu items are registered.

    For example the friends menu item is registered on "pagesetup, system" event and your mysite_init is run on "system, init" event which is run before the pagesetup.

    You can try something like this instead:

    elgg_register_plugin_hook_handler('register', 'menu:topbar', 'custom_topbarmenu_setup');

    function custom_topbarmenu_setup ($hook, $type, $return, $params) {
        // Remove friends menu item
        foreach($return as $key => $item) {
            if ($item->getName() == 'friends') {
                unset($return[$key]);
            }
        }
        return $return;
    }

     

  • Here's the same for removing multiple items:

    elgg_register_plugin_hook_handler('register', 'menu:topbar', 'custom_topbarmenu_setup');

    function custom_topbarmenu_setup ($hook, $type, $return, $params) {
        $remove = array('profile', 'friends', 'messages');

        foreach($return as $key => $item) {
            if (in_array($item->getName(), $remove)) {
                unset($return[$key]);
            }
        }

        return $return;
    }

  • @RVR - Fantastic avatar

    @JJ - Here's my full file, where would I insert your code exactly?

     

    <?php

    /**

     * Elgg demo custom index page plugin

     * 

     */

     

    elgg_register_event_handler('init', 'system', 'mysite_init');

     

    function mysite_init() {

     

    // Extend system CSS with our own styles

    // elgg_extend_view('css/elgg', 'mysite/css');

     

    // Replace the default index page

    //elgg_register_plugin_hook_handler('index', 'system', ' mysite');

     

     

    elgg_unregister_menu_item('topbar', 'elgg_logo');

     

    $logo_url = elgg_get_site_url() . "mod/mysite/img/logo-top.png";

    elgg_register_menu_item('topbar', array(

        'name' => 'my_logo',

        'href' => 'http://community.mysite.com/',

        'text' => "<img src=\"$logo_url\" alt=\"My logo\" width=\"126\" height=\"40\" />",

        'priority' => 1,

        'link_class' => 'elgg-topbar-logo',

     

     

     

     

     

    ));

    }

  • Alternative way:

    elgg_register_event_handler('pagesetup', 'system', 'mysite_pagesetup');

    function mysite_pagesetup () {
    elgg_unregister_menu_item('topbar', 'friends');
    }

    If using this, make sure that your plugin is located in the plugin list after the plugins which register the items you want to disable.

  • @Burgs

    Insert the elgg_register_plugin_hook_handler() in the mysite_init() function and the custom_topbarmenu_setup() function anywhere in your plugin's start.php

  • @TeamW - Thanks

    @JJ - It works... thanks and to clarify here's my complete code...

    <?php

    /**

     * Elgg demo custom index page plugin

      changed by nawtnet

     * 

     */

     

    elgg_register_event_handler('init', 'system', 'nawtnet_init');

     

    function nawtnet_init() {

     

    // Extend system CSS with our own styles

    // elgg_extend_view('css/elgg', 'nawtnet/css');

     

    // Replace the default index page

    //elgg_register_plugin_hook_handler('index', 'system', 'nawtnet');

     

     

    elgg_unregister_menu_item('topbar', 'elgg_logo');

     

    $logo_url = elgg_get_site_url() . "mod/nawtnet/img/logo-top.png";

    elgg_register_menu_item('topbar', array(

        'name' => 'my_logo',

        'href' => 'http://community.canyonlets.com/',

        'text' => "<img src=\"$logo_url\" alt=\"My logo\" width=\"126\" height=\"40\" />",

        'priority' => 1,

        'link_class' => 'elgg-topbar-logo',

     

    ));

     

    elgg_register_plugin_hook_handler('register', 'menu:topbar', 'custom_topbarmenu_setup');

    }

     

    function custom_topbarmenu_setup ($hook, $type, $return, $params) {

        $remove = array('profile', 'friends', 'messages');

     

        foreach($return as $key => $item) {

            if (in_array($item->getName(), $remove)) {

                unset($return[$key]);

            }

        }

     

        return $return;

    }

  • @Burgs

    Fantastic avatar

    ;)

    @Burgs

    @Juho Jaakkola

    I tried your complete code.. It works!

Feedback and Planning

Feedback and Planning

Discussions about the past, present, and future of Elgg and this community site.