Adding admin bar links

Hi,

I'm using a custom admin-bar.php file in my theme to add a custom link to the admin bar. The following only works for logged in users. How can I add a link for other users?

<nav id="admin-bar" class="dc">

<?php if($user = current_user()) {

    $links = array(
        array(
            'label' => __('Welcome, %s', $user->name),
            'uri' => admin_url('/users/edit/'.$user->id)
        ),
        array(
            'label' => __('Omeka Admin'),
            'uri' => admin_url('/')
        ),
        array(
            'label' => __('Log Out'),
            'uri' => url('/users/logout')
        ),
        array(
            'label' => __('Share Your Story'),
            'uri' => url('/contribution'),
            'class'=>'share-logged-in'
        )
    );

} else {
    $links = array(
    	array(
            'label' => __('Share Your Story'),
            'uri' => url('/users/login'),
            'class'=>'share-logged-out'
            )
     );
}

echo nav($links, 'public_navigation_admin_bar');
?>

</nav>

I've also tried...

array_unshift($links,array(
            'label' => __('Share Your Story'),
            'uri' => url('/contribution'),
            'class'=>'share'
            )
     );

But that's not working either.

How can I add a link here?

Thanks,

Erin

Which part of this doesn't work?

You don't get "Share Your Story" on the bar for logged-out users?

Do you have a plugin or other setup that's making the bar appear at all for users who aren't logged in?

Where are you putting your admin-bar.php? should be in yourtheme/common. Not having the file in the right place seems the likely culprit. Don't think that unshift will help you either way.

Yeah, you'll need to make sure hooks to display the login bar get added regardless of whether someone's logged in. Think you can just add the following to your theme's custom.php or a plugin:

<?php

    add_plugin_hook('public_head', 'admin_bar_css');
    add_plugin_hook('public_body', 'admin_bar');
    add_filter('body_tag_attributes', 'admin_bar_class');

This runs the same stuff as https://github.com/omeka/Omeka/blob/master/application/views/scripts/custom.php#L19-24 without the checks for a current user and such.

Or even better, just use the public_show_admin_bar filter to add your link instead of what I just said.

The file is in mytheme/common, as required.

I'm using the GuestUser plugin (v.1.0) to make the admin bar visible to unauthenticated users.

array_unshift() works fine for adding the link for logged in users, but has the same limitation in that it does not work for unauthenticated users.

So yeah, you can do what the GuestUser plugin does, which is pass a function to the public_show_admin_bar filter that simply returns true. Could do this in a custom plugin or in your theme's custom.php file like so:

<?php

function return_it_true($show) {
    return true;
}

add_filter('public_show_admin_bar', 'return_it_true');

This should show the contents of your own admin-bar.php for everyone.

This approach works fine as long as the Guest User plugin is inactive (de-activating the plugin would be a problem for me because it seems the new Contribution plugin requires it).

I think the Guest User plugin is doing its own filtering of the public_navigation_admin_bar links, which is interfering with my own modifications. I can't quite figure out a way around that.

You can also filter the public_navigation_admin_bar. Think if you do it in your theme it'll override whatever gets added by the plugins, but I can't remember the order in which they're fired. But you can try it and see:

<?php

function add_my_links($navLinks) {

    $navLinks[] = array(
            'label' => __('Share Your Story'),
            'uri' => url('/users/login'),
            'class'=>'share-logged-out'
            );

    return $navLinks;
}

add_filter('public_navigation_admin_bar', 'add_my_links');

This should just append your link to the output of the admin bar links. You can clobber the whole thing by just redefining the $navLinks array. You can var_dump($navLinks); instead of just returning them to see what's in that nav array, and remove any specific links you want.

That's the ticket, thanks

Looks like contribution should also add that link?