WP User Frontend is a popular plugin for building comprehensive frontend user centers. While often used for front-end post submission, its robust subscription system can be easily extended to create a full-fledged article subscription (paywall) service. In this guide, we’ll show you how to restrict content access based on the user’s active subscription package.
The core requirement for a subscription-based model is determining if the current user has an active, non-expired resource pack. After analyzing the WP User Frontend codebase, we can achieve this using the following logic:
1. Validating User Subscriptions
First, we need to retrieve the user’s current subscription details. The plugin provides classes and functions to fetch the active pack ID and its status:
$current_user = wpuf_get_user();
$user_subscription = new WPUF_User_Subscription($current_user);
$user_sub = $user_subscription->current_pack();
$sub_id = $current_user->subscription()->current_pack_id();
2. Restricting Content Access
With the subscription data in hand, we can use the the_content filter to conditionally display the article. We’ll check if the post is marked for paid users and then verify the logged-in user’s subscription status. If they don’t have a valid pack, we’ll display a polite message instead of the article content.
add_filter('the_content', function ($content)
{
$current_user = wpuf_get_user();
$user_subscription = new WPUF_User_Subscription($current_user);
$sub_id = $current_user->subscription()->current_pack_id();
// Check if the current post is set to require a subscription
$is_paid_post = get_post_meta(get_the_ID(), '_wpuf_is_paid_user', true) === 'on';
if ( ! $is_paid_post) {
return $content; // Return content as normal for free posts
}
if ($sub_id) {
// If they have a pack, check if it's expired
$subs_expired = $user_subscription->expired();
} else {
$subs_expired = false;
}
if ( ! is_user_logged_in()) {
return 'This article is for subscribers only. Please log in to read the full content.';
} else {
if ($subs_expired) {
return 'Your subscription has expired. Please renew your package to continue reading.';
} else {
return $content; // User is logged in and has an active subscription
}
}
});
This implementation handles basic paywall functionality effectively. You can further expand this to include per-post payment options or countdowns for remaining posts in a tier. Given WP User Frontend’s flexible API, implementing advanced features like these is straightforward for any developer looking to monetize their content.
