Skip to main content

One of the greatest features of a WordPress-based LMS platform is how a user experience can be tailored exact how we the online course and UI designers wish for any particular course, group, organisation, or just for a particular individual.

There are many toolkits that can be used to fully customise the user experience based on a particular user meta value or role capability, including LearnDash LMS and its in-built user groups system, or LoginWP which can be upgraded to redirect a user upon login to any particular screen (such as the starting page of their assigned learning programme) as soon as they have logged into WordPress.

One of the most basic customisations that you may wish to accomplish within your WordPress LMS platform is what happens on a page or post (or custom post type) on the site when a user is either logged in or out. This can provide some basic UI rules for a page which can adapt to the situation of the user, such as if the user is logged out and therefore we want to make a course homepage more sales and marketing orientated, or if they are logged in and therefore we want to make the authenticated user (assigned to the course) more excited about the great things that they will soon be undertaking whilst studying with you as a learning provider.

In order to achieve this effect, a simple method that we can use is to apply a filter to the WordPress site which will apply a body class to the page based on whether the user is either logged in or out. This then allows us to use CSS as a way to customise any element on the page in order to behave in two different ways based on whether the person on the page is a visitor, or a registered user.

This is where the following filter comes into play, for which credit belongs to author ‘danielmcclure‘ who makes this code available via his public Github repository (https://gist.github.com/danielmcclure/31463eb8c72316b21154f45483ad6777). You can either set this code up as a plugin on your site, or include the following code snippet in the functions.php file of your child theme:

//* Add Logged In/Out class to <body> with WordPress
add_filter( 'body_class', 'login_status_body_class' );
function login_status_body_class( $classes ) {

if (is_user_logged_in()) {
$classes[] = 'logged-in';
} else {
$classes[] = 'logged-out';
}
return $classes;

}

With this code snippet in place you will find the class of either ‘logged-in’ or ‘logged-out’ attached to the body tag of the page in order to utilise with CSS or any other additional function that you may wish to write.

We hope that you find this handy tip useful, and please continue to follow the Discover eLearning blog for even more helpful LearnDash Tips and Tricks in the near future. You can also follow our social media channels on Facebook, Twitter, and LinkedIn where we posts regular updates and advice for LearnDash platform users.

Chris Hodgson

Chris Hodgson is an award-winning Digital learning Solutions Developer and Director of Discover eLearning Ltd. He supports organisations of all sizes to implement engaging, meaningful, and impactful eLearning solutions. Chris has over 15 years’ experience working in both private and public sector learning and development.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.