Accessing the current author or user

The WordPress template hierarchy defines a number of templates that can be used to display archived posts for a specific user in your WordPress site: author.php, author-$nicename.php, and author-$id.php.

The MasterPress API defines 2 methods for accessing the current author being displayed, WOOF::the_author and WOOF::author, both of which return a WOOF_User object:

Example 1: Accessing the current author

The WOOF::author method can also return specific authors (users), but will return the current author when no arguments are supplied.

The examples above would be suitable in the author.php series of templates, where you would generally display information about the author of the posts above a list of those posts.

The current post author in single templates

The methods used above to access the current author in an author archive both have a logical fallback if you’re not currently viewing an author archive – if we’re currently in a single template, the method will return the author of the current post instead. This lets you use code like in line 8 below, for a single template:

Example 2: Author methods fallback

Note that you could also rewrite line 8 above to access the author on the post object too:

Example 3: Accessing the author directly on $the

The current logged-in user

Similar to those for accessing authors, the MasterPress API has two methods to access the current logged in user, WOOF::the_user and WOOF::user:

Example 4: Accessing the current logged-in user

The WOOF::user can also return specific authors (users), but will return the current user (if a user is logged in) when no arguments are supplied.

How to check if a user is logged in

The examples above made the assumption that a user is indeed logged in. This is obviously never guaranteed and there’s a few ways to check if there is a current user. First there’s the more traditional way:

Example 5: The standard way to check for a logged in user

… or we can make use of the fact that the methods which return our active user will return a WOOF_Silent object when a user is not logged in, which can be tested for with the exists method:

Example 6: Checking for an active user with WOOF

Latest From the Blog

MasterPress 1.4.1 is now available

27th May 2024

This update addresses backwards compatibility issues that were introduced in the previous update. There were some edge cases where PHP 7 had issues and it didn’t work at all on PHP 5.6. We have now changed it so that the PHP 8 compatibility changes will now only affect PHP 8 and will be more thorough… 

Plugin Requirements

MasterPress requires a minimum of WordPress version 4.9, MySQL 5.6, and PHP version 5.6.20.

We also recommend that PHP is configured to use a memory limit of 64MB per request (128MB may be required for sites with higher complexity).

This plug-in is not compatible with the hosted service.

Three AM