Implementing Post Subscriptions with the WP User Frontend User Heart Plugin

The WP User Frontend plugin is a popularFront-end user center pluginWe use this plugin to implement an article submission function, according to the need we can charge for the article submission function, the user can purchase a resource package, you can publish a number of articles to our site.

So, is it possible to extend this to realize how many articles on the site a user can read after purchasing a resource pack? After some testing, it turns out that it's not too difficult.

Ideas and code to implement the article subscription feature

To implement the paid article reading feature in the same way as the default paid article posting feature, we first need to be able to determine if the current user has purchased a valid resource pack and that this resource pack has not expired. It is easy to find the code of WP User Fronted plugin. The specific code is as follows.

$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();

With the code above, we can use the the_content Filter to modify the content of the article, for the need to pay to read the article, if the user is not logged in, or does not have a valid subscription package, to give the appropriate prompts; if the user has an available subscription package, directly display the full text of the article, the complete code is as follows.

add_filter('the_content', function ($content)
{
    $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();

    $is_paid_post      = get_post_meta(get_the_ID(), '_wpuf_is_paid_user', true) === 'on';

    // 非付费文章不做处理,直接返回
    if ( ! $is_paid_post) {
        return $content;
    }

    if ($sub_id) {
        // 有订阅包,并且已过期时,$subs_expired 为 true
        $subs_expired = $user_subscription->expired();
    } else {
        $subs_expired = false;
    }

    if ( ! is_user_logged_in()) {
        $filtered_content = '此文章需要登录后才能查看,请登录。';
    } else {

        if ($subs_expired) {
            $filtered_content = '此文章需要订阅才能查看,请订阅。';
        } else {
            $filtered_content = $content;
        }

    }

    return $filtered_content;

});

The above code doesn't realize the functions of judging the number of remaining paid articles and paying by articles, according to the existing functions of WP User Frontend, it's not too difficult to realize these two functions, so if you are interested, you can try it.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *