Steve Taylor photo

Tweaks for WordPress Subscribers

I’m currently building a WordPress site where I want people to be able to sign up as Subscribers. They won’t have the option to write anything to the site, so they won’t be Authors or Editors—Subscribers seems appropriate.

I’ve ended up having to tweak WordPress in several ways to make the system work as I want. At the moment I’m erring on the side of coding minor adjustments into my custom themes. Plugins are more flexible, but if you just need to change a small thing, coding it into the theme leaves your site with less overhead from another plugin being loaded.

So, here are a few little plugin-free techniques for tweaking you WP Subscriber system.

(In all this, remember that functions.php is your friend. It’s a standard WP theme file that gets included in every request to your site, front- or back-end. It’s generally used—of course—to include custom functions for your theme, but you can also use it just to run code to tweak your installation.)

Allowing Subscribers to see Private posts and pages

I want to be able to create pages that are only viewable when Subscribers (or Admins, of course) are logged in. Unfortunately, for some reason the “Private” visibility status that you can assign to posts or pages in WordPress excludes Subscribers—only higher user roles can see Private content.

Installing a full role management plugin is overkill for this, so I trawled around for and eventually found the core functions that allow you to add specific capabilities to a role (check out this handy table for a quick overview of roles and capabilities). I popped these lines into functions.php, which seem to do the trick nicely:

$subRole = get_role( 'subscriber' );
$subRole->add_cap( 'read_private_pages' );

Redirecting logins to home page

When Subscribers log in, you don’t necessarily want them dumped onto the WP dashboard—with loads of info on the blogging aspect that’s irrelevant to them. Of course there’s plugins to do this, but how about a few lines of theme code? Nathan Youngman sorted me out with this:

function loginRedirect( $redirect_to, $request_redirect_to, $user ) {
	if ( is_a( $user, 'WP_User' ) && $user->has_cap( 'edit_posts' ) === false ) {
		return get_bloginfo( 'siteurl' );
	}
	return $redirect_to;
}
add_filter( 'login_redirect', 'loginRedirect', 10, 3 );

Check Nathan’s post for a description of the code. Note for now that the edit_posts capability is used as a check for the Subscriber role; all other roles can do this, but Subscribers can’t.

What about the logout?

Good question, glad you asked. I have a bit on my sidebar with links to special Subscriber-only pages, including the profile page (/wp-admin/profile.php). For the logout link, if you want to do it properly, and want to redirect to the home page, use something like this:

<a href="<?php echo wp_logout_url( get_bloginfo( 'url' ) ); ?>">Log out</a>

Customizing the WordPress screens

How about a little customization of the login screen and the WordPress admin area? A custom logo, at least? Again, plugins can help, but if you want to just slap some code into functions.php

Here’s the code to pull in a CSS file for the login screen. Note that I close then re-open the PHP block to output straight HTML; the code extracts below assume that they’re extacted from a larger file that begins with and ends with ?>.

function customLogin() { ?>
style-login.css
in your theme directory.

Here’s the code for adding custom CSS to WordPress itself.

function customAdmin() {
global $user; ?> ul#adminmenu li#menu-dashboard { display: none !important; }

What if they manage to navigate there anyway? Try hiding all the boxes:

.postbox { display: none !important; }

Again, these are rather inelegant hacks. But they do the job (so far!), and leave the site a little leaner plugin-wise.