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.
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.
<?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(); ?>
Posted by <?php the_author_posts_link(); ?>
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:
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.
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.
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.
<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&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>
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.
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.
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:
<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&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>
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.
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.
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&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:
And the control panel for logged in users will look similar to the following screenshot:
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.
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.
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.
Now that we have configured the author roles, let’s learn how to control the author’s actions.
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.
Here are the 5 predefined roles for WordPress users:
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.
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:
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:
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.
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.
Once you have made sure that your authors have successfully filled their information, you can start coding by carrying out the following steps:
<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-->
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.
Here are all the available template tags related to authors:
I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…
Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…
Once we learn how to deploy an Ubuntu server, how to manage users, and how…
Key-takeaways: Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…
While developing a web application, or setting dynamic pages and meta tags we need to deal with…
Software architecture is one of the most discussed topics in the software industry today, and…