HowTo: Disable Access to the WordPress Dashboard for Non-Admins

Update: I had a few requested to bundle this into a plugin so I did. You can download it here.

Currently, I’m working on a site where we didn’t want non-admins to even be able to access the wp-admin dashboard. I searched around quite a bit looking for a complete shutoff-solution but most of the results detail how to literally disable the “Dashboard” menu in wp-admin.

Finally, trolling the comments on a like-solution in a post by c. bavota, I stumbled across a simplified version of bavotasan’s function that does exactly what I want, plus it redirects unworthy users to the homepage!

The simplified function was authored by somebody going only by the moniker of Jake.

It’s a pretty simple solution. It adds an action calling a function called ‘redirect_dashboard’ which checks the user level, and if the currently-logged-in-user is unworthy, they get bounced to the homepage. Pretty neat. On line #4, the function checks the user level, with the default set as ‘level_10’ or administrator. I modified this to ‘level_7’ to exclude anyone below the Editor level, but you could choose whichever capability level suits your purpose. Vist the Roles and Capabilities Codex page to find out more about user levels.

Here’s the snippet (which should be added to your theme’s functions.php file)

<br />
add_action('admin_init', 'no_mo_dashboard');<br />
function no_mo_dashboard() {<br />
  if (!current_user_can('manage_options') &amp;&amp; $_SERVER['DOING_AJAX'] != '/wp-admin/admin-ajax.php') {<br />
  wp_redirect(home_url()); exit;<br />
  }<br />
}<br />

Add something to post title based on post type

Recently, I was working on a WordPress install employing several custom post types to handle two separate “blogs” within a single site. The site used normal ‘posts’ to handle articles and the client wanted to append the word “BLOG” to the beginning of blog posts’ titles only.

Using the Codex I slapped something together rather easily.

Using the following snippet, I built a simple if statement to test for blog posts, and if not, to render the_title() minus any extra text.

<br />
// First test for the post type(s)<br />
&lt;?php global $post<br />
if (get_post_type($post) == 'post_type_1') || get_post_type($post) == 'post_type_2') { ?&gt;<br />
// IF either test registers true, then set the $before parameter in this format: the_title($before, $after);<br />
&lt;h1&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;?php the_title('BLOG: ', ''); ?&gt;&lt;/a&gt;&lt;/h1&gt;<br />
// IF not, then display as normal<br />
&lt;?php } else { ?&gt;<br />
&lt;h1&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt;<br />
// Close the IF statement<br />
&lt;?php } ?&gt;<br />

Using this example, posts that register as true would display like this:

BLOG: The post title

Append ‘Read More’ to the end of the_excerpt()

This snippet, when added to your theme’s functions.php file, will allow you to append ‘… Read More’ or any other text to the end of your excerpts. Creates a more streamlined look.

&lt;br /&gt;<br />
function excerpt_readmore($more) {&lt;br /&gt;<br />
	return '... &amp;lt;a href=&amp;quot;'. get_permalink($post-&amp;gt;ID) . '&amp;quot; class=&amp;quot;readmore&amp;quot;&amp;gt;' . 'Read More' . '&amp;lt;/a&amp;gt;';&lt;br /&gt;<br />
}&lt;br /&gt;<br />
add_filter('excerpt_more', 'excerpt_readmore');&lt;br /&gt;<br />