Sell video courses with WooCommerce and the secure player

Add to your site2 min read

Show the XAPlayer player only to customers who bought the course product in WooCommerce, with one shortcode and no extra plugin.

All guides

Here the lesson page shows the player only to people who bought that course’s product in your WooCommerce store. Everyone else sees a message with a link to buy it.

Before you start#

  1. In your XAPlayer panel, open Settings and create an API key. It is shown only once, so copy it right away.
  2. Under Websites allowed to show your player, add your site’s full address, such as https://academy.example.com. If your site also opens with www, add that too.
  3. Get the course ID: in Settings, pick the course in “Show the player on your website” and the ID is filled into the sample code. It is also the last part of the course page’s address in the panel.
  4. Follow “Embed the secure player in WordPress” first; this shortcode builds on [xaplayer].

A shortcode for buyers#

File: wp-content/themes/your-child-theme/functions.php

functions.php
<?php
// [xaplayer_course product="123" course="COURSE_ID"]
// Shows the player only to customers who bought WooCommerce product 123.
add_shortcode('xaplayer_course', function ($atts) {
    $atts = shortcode_atts(['product' => 0, 'course' => ''], $atts, 'xaplayer_course');
    $product = (int) $atts['product'];

    if (!is_user_logged_in()) {
        return '<p>Please sign in to watch this lesson.</p>';
    }

    $user = wp_get_current_user();
    if (!wc_customer_bought_product($user->user_email, $user->ID, $product)) {
        return sprintf(
            '<p>This lesson is for students who bought the course. <a href="%s">Buy the course</a></p>',
            esc_url(get_permalink($product))
        );
    }

    return do_shortcode(sprintf('[xaplayer course="%s"]', esc_attr($atts['course'])));
});

Use it#

  1. Find the course product’s ID in WooCommerce: hover the product name in the Products list and the ID appears.
  2. On the lesson page write [xaplayer_course product="123" course="COURSE_ID"].
  3. Open the page as a buyer and as someone else to see both cases.

If it doesn’t work#

What you seeCause and fix
The player area is empty or the browser says it refused to connectYour site isn’t in the allowed websites, or www differs. Add the page’s exact address.
401 unauthorizedThe API key is wrong, or you created a new key and the old one stopped working. Copy the key again.
404 not_foundThe course or video ID is wrong or belongs to another account.
400 bad_requestThe body is incomplete: send exactly one of courseId or videoId, and a non-empty viewer.id.
After a few hours the player says the page timed outThe token expired. Get a fresh token every time the page opens, and never cache the player address.

Try XAPlayer free for 14 days, with 10 licenses on us.

Try it free with your own course