Creating an Admin Bar for Expression Engine
A while back I got tired of now having a way to access the control panel from the front-end of my site. I decided to investigate the best way to create this type of functionality. I had no doubts that ExpressionEngine contained a way, since it’s such a powerful system, but I wasn’t sure how. Here’s what I found.
ExpressionEngine uses a variable called group_id to determine what group the currently logged-in user is in. In most cases, a group_id of 1 means you’re a Super Admin, meaning you have access to the control panel.
The other piece of the puzzle is finding out how to link to certain areas of the control panel. I was not able to find any information about this in the EE Documentation, so i’ll present my findings here.
When you’re logged in, look at the end of the URL and you should see something like “…&C=publish”. Each area of the admin has a C value that corresponds to it (as well as some other variables in more complicated areas). The only variable that should be disregarded is “S” which stands for your session_id and is irrelevant to our goals.
Here are a couple of common ones:
- Publish tab - C=publish
- Edit tab - C=edit
- Templates tab - C=templates
- Admin tab - C=admin
- Log-out - C=logout
And a couple more complicated (but very useful) ones:
- New Post in Weblog 1 - C=publish&M=entry_form&weblog_id=1
- Edit Post 88 - C=edit&M=edit_entry&weblog_id=1&entry_id=88
Now that we know how to only show this bar to our Super Admin users, as well as the urls we need to link to, we can add in our code to the template.
Here’s the code i’m using. You’re very welcome to use this code in your own sites, as it should work perfectly without any customization.
{if group_id == 1}
<!-- if user is in Super Admin (group #1) -->
<div class="admin_bar">
<!-- the container for our admin bar -->
<ul>
<li>Welcome {screen_name}!</li>
<!-- welcome our current logged in user by their screen name -->
<li><a href="{site_url}system" title="Access the CP">
Control Panel</a></li>
<li><a href="{site_url}system/index.php?C=publish
&M=entry_form&weblog_id=1" title="Write a New Post">
New Post</a></li>
<li><a href="{site_url}system/index.php?C=edit">
Edit Posts</a></li>
<li><a href="http://expressionengine.com/docs/">
Documentation</a></li>
<li><a href="{site_url}system/index.php?C=logout">
Log Out</a></li>
</ul>
</div>
{/if}
And that’s it! You’ve just added your very own Admin bar. I would make sure this is working for your specific set up, and double check things like the group_id just to be safe.
One thing that has always annoyed me was that you don’t stay logged in for very long, therefore the bar disappears pretty often. If you’d like to remain logged in much longer than the default configuration allows, there is an easy way to enable that.
Simply go to Admin → System Preferences → Security & Session Preferences and set the Control Panel Session Type to “Cookies only.”
This will keep you logged in longer, but you will lose some security if you log in on a public computer and forget to log out. The implications are minor, but still there.
I hope you’ve enjoyed this code sample and I hope it inspires even more cool ideas, and be sure to blog them so we can all enjoy. Let me know in the comments if you’ve got questions or other feedback.
Update:
There are some variables you’ll want to double-check before using this on your site:
- group_id - Double check that this ID is your super admin group
- system/ - Make sure that you change “system” to whatever folder you’re using. “system” is the default configuration, but many people change it.
- weblog_id - You’ll want to make sure that this is the ID of the weblog you’d like to post/edit from. Again, under default configuration, this should be 1, but this could likely change if you have multiple “weblogs” on your install.
Also if you’re trying to use something like this in the weblog entries tag, comments tag, or within other member-related tags you’ll need to add the prefix “logged_in_” to your group_id, screen_name and other member-related variables. This specifies that you only want the info about the logged in user and not the user who posted the current content.



0 comments