18 min read

Creating an author page template

If you have different authors on your blog, then my suggestion to you would be to display the biographical and contact information of each author on his own dedicated page. Luckily, WordPress allow us to do just that.

Getting ready

In this recipe, we’re going to create an author page template for the purpose of displaying author related information. Make sure that you have understood the creation and usage of a page template.

How to do it

  1. Create a new file named authors.php on your WordPress theme directory.
  2. Insert the following code into your file named authors.php:
    <?php
    /*
    Template Name: Authors Page
    */
    ?>
    <?php get_header(); ?>
    <div id="content" class="narrowcolumn">
    <?php
    if(isset($_GET['author_name'])) :
    $curauth = get_userdatabylogin($author_name);
    else :
    $curauth = get_userdata(intval($author));
    endif;
    ?>
    <h2>About <?php echo $curauth->nickname; ?></h2>
    <div class="excerpt">
    <?php echo $curauth->nickname; ?> personal website:
    <a href="<?php echo $curauth->user_url; ?>">
    <?php echo $curauth->user_url; ?></a>
    </div>
    <?php echo $curauth->user_description; ?>
    <h2>Latest posts by <?php echo $curauth->nickname; ?>:</h2>
    Chapter 6
    133
    <ul>
    <?php if ( have_posts() ) : while ( have_posts() ) :
    the_post(); ?>
    <li>
    <a href="<?php the_permalink() ?>"><?php the_title();
    ?></a> on <?php the_time('d M Y'); ?>
    </li>
    <?php endwhile; else: ?>
    <p><?php _e('No posts by this author.'); ?></p>
    <?php endif; ?>
    </ul>
    </div><!--/content-->
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  3. Save the file and upload it to the wp-content/themes/yourtheme folder of your WordPress install.
  4. Log in to your WordPress dashboard, create a new page, and select the Authors Page as a page template. Give it the title of your choice, such as, About the Author and publish the page.
  5. Open the single.php file from your theme. Depending on the theme that you’re using, you may need to add the following code in order to display the author’s name and a link to the author’s page:
    Posted by <?php the_author_posts_link(); ?>
  6. Once you have saved the modifications made in your single.php file, visit one of your blog posts and click on the author name. The author page is displayed showing the author name, description, and web site.

How it works

The first thing that we need to know is the name of the author whose information is to be displayed. To do so, we have to get the author_name parameter sent via the GET method. With this value, we can initialize a $curauth php object that will allow us to get some personal information about the author, such as his web site, email, biography, and so on, with the help of the classic php syntax, that is, $curauth->nickname;.

Once the author data, that is to be displayed, has been retrieved, we shall add a WordPress loop in order to be able to view the recent posts by this author.

The following screenshot shows a well-prepared author page:

Managing and Enhancing Multi-Author Blogs with WordPress 2.7(Part 1)

There’s more…

In the preceding example we retrieved the author name, description, and web site URL. However, as you may know, users can provide much more information (in Administration, Profile, Your Profile options) such as their email address, AIM and Yahoo! messenger nickname, and login information.

A few more template tags can be used to retrieve another kind of information from the author data. These tags are listed under the There’s more! section of Displaying author-related information on posts, which we will see later in this article.

Displaying a custom login form in your blog’s sidebar

It doesn’t matter whether you’re running a multi-author blog, or a blog where readers can register. Having a login form embedded in your sidebar will make your blog look a lot more professional and user friendly.

Here is what you can expect from this recipe. In the following screenshot, a login form has been added to the K2 theme sidebar.

Managing and Enhancing Multi-Author Blogs with WordPress 2.7(Part 1)

Getting ready

To achieve this recipe, you’ll have to edit the sidebar.php file from your theme. The following hack works with WordPress 2.0 to 2.8.

How to do it

  1. Open the sidebar.php file for editing.
  2. Find the opening <ul> tag and paste the following code under it:
    <li>
    <?php global $user_ID, $user_identity, $user_level ?>
    <?php if ( $user_ID ) : ?>
    <h2><?php echo $user_identity ?></h2>
    <ul>
    <li><a href="<?php bloginfo('url') ?>/wp-login.php?action=
    logout&amp;redirect_to=<?php echo urlencode
    ($_SERVER['REQUEST_URI']) ?>">Logout</a></li>
    </ul>
    <?php elseif ( get_option('users_can_register') ) : ?>
    Managing and Enhancing Multi-Author Blogs
    136
    <h2>Identification</h2>
    <ul>
    <li>
    <form action="<?php bloginfo('url')
    ?>/wp-login.php" method="post">
    <p>
    <label for="log"><input type="text" name="log"
    id="log" value="<?php echo wp_specialchars (stripslashes
    ($user_login), 1) ?>" size="22" /> User</label><br />
    <label for="pwd"><input type="password"
    name="pwd" id="pwd" size="22" /> Password</label><br />
    <input type="submit" name="submit" value="Login"
    class="button" />
    <label for="rememberme"><input name="rememberme"
    id="rememberme" type="checkbox" checked="checked"
    value="forever" /> Remember me</label><br />
    </p>
    <input type="hidden" name="redirect_to" value="<?php echo
    $_SERVER['REQUEST_URI']; ?>"/>
    </form>
    </li>
    <li><a href="<?php bloginfo('url')
    ?>/wp-register.php">Register</a></li>
    <li><a href="<?php bloginfo('url') ?>/wp-login.php?action=
    lostpassword">Recover password</a></li>
    </ul>
    <?php endif ?>
    </li>
  3. Save the file. Your users can now login directly from your blog’s sidebar.

How it works

The working of this code is quite simple. First, you initialize the global variables to get the user ID, name, and level. Then, you check the value of the $user_ID variable. If the value is not null, which means that the current user is logged in, you then display a quick hello user text and a link to log out.

If the user isn’t logged in, you check whether registering is allowed on the blog. If the user is logged in, then you simply display an HTML form that allows the user to log in directly from the blog. A link has also been included for registration if the current user doesn’t have an account yet.

This code was inspired from a tutorial available at www.wpdesigner.com.

Adding a control panel to your blog’s sidebar

Now that you have learned how to check whether a user is logged in or not, why not learn how to add a small control panel to your blog’s sidebar that is only visible to the logged in users.

In this recipe, you’ll learn how to achieve this task.

Getting ready

The upcoming piece of code works in exactly the same way as the code from the previous recipe does. It is all about checking if the user is logged in and whether he or she has the right to do a certain kind of thing.

The following screenshot shows a simple, but useful, control panel which is similar to the one we’re about to create:

Managing and Enhancing Multi-Author Blogs with WordPress 2.7(Part 1)

How to do it

  1. Open sidebar.php for editing.
  2. Find the first opening <ul> HTML tag, and paste the following code under the <ul> tag:
    <li>
    <?php global $user_ID, $user_identity, $user_level ?>
    <?php if ( $user_ID ) : ?>
    <h2>Control panel</h2>
    <ul>
    <li>Identified as <strong><?php echo $user_identity ?>
    </strong>.
    <ul>
    <li><a href="<?php bloginfo('url') ?>/wp-
    admin/">Dashboard</a></li>
    <?php if ( $user_level >= 1 ) : ?>
    <li><a href="<?php bloginfo('url') ?>/wp-admin/
    post-new.php">Write an article</a></li>
    <?php endif; ?>
    <li><a href="<?php bloginfo('url') ?>/wp-admin/
    profile.php">Profile and personal options</a></li>
    <li><a href="<?php bloginfo('url') ?>/wp-login.php?
    action=logout&amp;redirect_to=<?php echo
    urlencode($_SERVER['REQUEST_URI']) ?>">Logout</a></li>
    <?php
    if (is_single()) {?>
    <li><a href="<?php bloginfo('wpurl');?>/wp-admin/
    edit.php?p=<?php the_ID(); ?>">Edit Post</a>
    </li>
    <?php } ?>
    </ul>
    </li>
    </ul>
    <?php endif; ?>
    </li>
  3. Once you are done, save the file. The allowed users can now go to their dashboard, edit their profile, or write a new post directly from the blog.

How it works

As mentioned earlier, this code works in the same way as the code that was used to create a login form in the sidebar.

After you’ve made sure that the $user_ID variable isn’t null, you work towards displaying the options available to the user. It is possible to define what a user can perform according to his role (administrator, author, contributor, subscriber, and so on). We’re going to have a look at this in the next recipe.

There’s more…

Now that you have learned how to add a control panel to the blog’s sidebar, let’s go ahead and try out something new.

Adding a login form and a control panel

Now that you know how to add a login form and a mini control panel to your blog’s sidebar, why not try mixing the two codes? If the user isn’t logged in, we’ll display the login form. Otherwise, the custom panel will be shown to the user.

The code below works in the same way as the two that we studied previously. Add the following code to the sidebar.php file of your theme:

<li>
<?php global $user_ID, $user_identity, $user_level ?>
<?php if ( $user_ID ) : ?>
<h2>Control panel</h2>
<ul>
<li>Identified as <strong><?php echo $user_identity ?>
</strong>.
<ul>
<li><a href="<?php bloginfo('url') ?>/wp-admin/">
Dashboard</a></li>
<?php if ( $user_level >= 1 ) : ?>
<li><a href="<?php bloginfo('url') ?>/wp-admin/
post-new.php">Write an article</a></li>
<?php endif; ?>
<li><a href="<?php bloginfo('url') ?>/wp-admin/
profile.php">Profile and personal options</a></li>
<li><a href="<?php bloginfo('url') ?>/wp-login.php?
action=logout&amp;redirect_to=<?php echo urlencode
($_SERVER['REQUEST_URI']) ?>">Logout</a></li>
<?php
if (is_single()) {?>
<li><a href="<?php bloginfo('wpurl');?>/wp-admin/
edit.php?p=<?php the_ID(); ?>">Edit Post</a>
</li>
<?php } ?>
</ul>
</li>
</ul>
<?php elseif ( get_option('users_can_register') ) : ?>
<h2>Identification</h2>
<ul>
<li>
<form action="<?php bloginfo('url') ?>/wp-login.php"
method="post">
<p>
<label for="log"><input type="text" name="log" id="log" value
="<?php echo wp_specialchars(stripslashes($user_login), 1)
?>" size="22" /> User</label><br />
<label for="pwd"><input type="password" name="pwd" id="pwd"
size="22" /> Password</label><br />
<input type="submit" name="submit" value="Login"
class="button" />
<label for="rememberme"><input name="rememberme" id=
"rememberme" type="checkbox" checked="checked"
value="forever" /> Remember me</label><br />
</p>
<input type="hidden" name="redirect_to" value="<?php echo
$_SERVER['REQUEST_URI']; ?>"/>
</form>
</li>
<li><a href="<?php bloginfo('url') ?>/wp-register.php">
Register</a></li>
<li><a href="<?php bloginfo('url') ?>/wp-login.php?
action=lostpassword">Recover password</a></li>
</ul>
<?php endif; ?>
</li>

The custom logging form for unregistered users will look similar to the following screenshot:

Managing and Enhancing Multi-Author Blogs with WordPress 2.7(Part 1)

And the control panel for logged in users will look similar to the following screenshot:

Managing and Enhancing Multi-Author Blogs with WordPress 2.7(Part 1)

Configuring author roles

Now that you have learned about the different aspects of the user’s roles and capabilities, there’s probably something that you’re finding a little frustrating. By default, you can’t configure author roles to fit your blog’s needs. For example, a contributor can’t upload images. Moreover, by default, you can’t change it. Luckily, there’s a plugin called Role Manager which allows you to configure author roles in the way that you want.

Getting ready

The Role Manager plugin can be found at the following link:

http://www.im-web-gefunden.de/wordpress-plugins/role-manager/

Download it, unzip it onto your hard drive, and install it as any other WordPress plugin.

How to do it

  1. Once the Role Manager plugin is installed, log in to your WordPress dashboard and go to Users | Roles.
  2. A list of all of the available user roles will be displayed. For each role you can define what the user can do. For example, you can choose to let a contributor upload images.
  3. What is even better is that you’re not limited to the 5 default user roles that are provided by WordPress. The Role Manager plugin allows you to create new roles, as well as the ability to rename, copy, or delete existing ones.

How it works

The job of the Role Manager plugin is pretty easy. It simply creates custom roles with the options that you have defined and save it on the WordPress database.

There’s more…

Now that we have configured the author roles, let’s learn how to control the author’s actions.

Controlling what authors can do

Even if your blog is powered by multiples authors, it is still your blog. Therefore, you shouldn’t allow every author to have the right to edit posts or delete comments.

Since version 2.0, WordPress features user roles. User roles are defined as a group of actions that can be accomplished by a specific range of users. For example, the administrator can edit theme files, but the subscribers can’t.

User roles and their capabilities

Here are the 5 predefined roles for WordPress users:

  1. Administrator: The administrator is the blog owner. He has unlimited access to all of the administration features such as writing posts, editing his own posts along with the posts from other authors, installing plugins, selecting a new theme, editing themes, and editing plugin files.
  2. Editor: The editor can write or publish posts, upload images, edit his own posts, and manage other’s posts.
  3. Author: The author can write, publish, and edit his own his own posts. He’s also allowed to upload images for use in his posts.
  4. Contributor: A contributor can write posts but can’t publish them himself. Once he has written a post, the post is pending approval from the administrator. The contributor can’t upload images either. This role is very good for guest authors on your own blog.
  5. Subscriber: A subscriber is a registered user of your blog, but can’t write posts.

For an exhaustive description of user roles and capabilities, you should read the related page in WordPress Codex: http://codex.wordpress.org/Roles_and_Capabilities.

Controlling what users can see in your theme

In the previous example, we built a sidebar control panel that allows the user to edit the current post. However, the code doesn’t let you control which kind of author is allowed to edit the current post. For now, even if only the users with a sufficient role level will be capable of editing the post, every logged in user can see the related link.

The solution to that problem is a built-in WordPress function, called current_user_can(). As an argument, this function takes a string describing the action or the required role level to perform a specific task. For example, the following code will provide a link to edit the current post to the administrators only:

<?php
if (current_user_can('level_10')){ ?>
<a href="<?php bloginfo('wpurl');?>/wp-admin/edit.php?p=
<?php the_ID(); ?>">Edit Post</a>
<?php } ?>

The current_user_can() function accepts user_0 to user_10 as a parameter. Here is the conversion table between the role levels and the roles:

  • Suscriber: level_0
  • Contributor: level_1
  • Author: level_2 to level_4
  • Editor: level_5 to level_7
  • Administrator: level_8 to level_10

The current_user_can() function can also be used with a specific action as a parameter. This is the recommended use, as the level parameter is becoming obsolete.

The following example checks if the current user can edit a post he previously published. If yes, then a link to edit the post will be displayed.

<?php
if (current_user_can('edit_published_posts')){ ?>
<a href="<?php bloginfo('wpurl');?>/wp-admin/edit.php?p=
<?php the_ID(); ?>">Edit Post</a>
<?php } ?>

Here are all of the arguments that are accepted by the current_user_can() function:

        

  • switch_themes
  •     

  • edit_themes
  •     

  • activate_plugins
  •     

  • edit_plugins
  •     

  • edit_users
  •     

  • edit_files
  •     

  • manage_options
  •     

  • moderate_comments
  •     

  • manage_categories
  •     

  • manage_links
  •     

  • upload_files
  •     

  • import
  •     

  • unfiltered_html
  •     

  • edit_posts
  •     

  • edit_others_posts
  •     

  • edit_published_posts
  •     

  • edit_pages
  •     

  • edit_others_pages
  •     

  • edit_published_pages
  •     

  • edit_published_pages
  •     

  • delete_pages
  •     

  • delete_others_pages
  •     

  • delete_published_pages
  •     

  • delete_posts
  •     

  • delete_others_posts
  •     

  • delete_published_posts
  •     

  • delete_private_posts
  •     

  • edit_private_posts
  •     

  • read_private_posts
  •     

  • delete_private_pages
  •     

  • edit_private_pages
  •     

  • read_private_pages
  •     

  • delete_users
  •     

  • create_users
  •     

  • unfiltered_upload
  •     

  • edit_dashboard
  •     

  • update_plugins
  •     

  • delete_plugins

Displaying author-related information on posts

In a multi-author blog, it’s always good for the reader to know the author of the article that they’re currently reading. It’s even better if they can grab some extra information about the author, such as his website, a short bio, and so on.

In this recipe, you’ll learn how to edit your single.php theme file to automatically retrieve the author-related information, and display it at the top of the page.

Getting ready

As we’re going to display author information on posts, the first thing to do is to make sure that your contributing authors have entered their biography and other information into the WordPress database.

Any author can enter his information by logging in to the WordPress dashboard, and then going to Profile. The blog administrator can edit all of the profiles. The following screenshot shows the WordPress 2.7 profile editor for the authors.

Managing and Enhancing Multi-Author Blogs with WordPress 2.7(Part 1)

How to do it

Once you have made sure that your authors have successfully filled their information, you can start coding by carrying out the following steps:

  1. Open the file single.php for addition.
  2. Paste the following code within the loop:
    <div id="author-info">
    <h2>About the author: <?php the_author();?></h2>
    <?php the_author_description(); ?>
    <?php the_author();?>'s website: <a href="<?php the_author_url();
    ?>"><?php the_author_url(); ?></a><br />
    Other posts by <?php the_author_posts_link(); ?>
    </div><!--/author-info-->
  3. Save the file and visit your blog. You will notice that your posts now automatically display the author-related information, as shown in the following screenshot:
  4. How it works

    WordPress provides a dozen of author-related template tags, which are an easy way to retrieve information that is entered by authors in their profile.

    Note that all of these tags must be used within the loop for them to work.

    There’s more…

    Here are all the available template tags related to authors:

LEAVE A REPLY

Please enter your comment!
Please enter your name here