Embed the secure player in WordPress

Add to your site3 min read

A step-by-step guide to showing XAPlayer’s protected videos inside WordPress pages and posts with one shortcode, for signed-in users only.

All guides

You’ll add an [xaplayer] shortcode to your WordPress site. Wherever you put it, a signed-in student watches the course right on that page, with their own name on the picture and no license code to type.

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.

Put the key in wp-config.php#

File: wp-config.phpIn WordPress’s root folder, next to wp-load.php

wp-config.php
// wp-config.php: above the line "That's all, stop editing!"
define('XAPLAYER_API', 'https://en.xaplayer.ir/api/v1');
define('XAPLAYER_API_KEY', 'xap_sk_...'); // from Settings in your XAPlayer panel

Add the shortcode#

Put this in your child theme’s functions.php, or add it with a plugin such as Code Snippets so theme updates don’t remove it.

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

functions.php
<?php
// Shortcodes: [xaplayer course="COURSE_ID"]  or  [xaplayer video="VIDEO_ID"]
add_shortcode('xaplayer', function ($atts) {
    $atts = shortcode_atts(['course' => '', 'video' => ''], $atts, 'xaplayer');

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

    $user = wp_get_current_user();
    $body = [
        'viewer'     => ['id' => (string) $user->ID, 'name' => $user->display_name],
        'ttlSeconds' => 4 * HOUR_IN_SECONDS,
    ];
    $phone = get_user_meta($user->ID, 'billing_phone', true);
    if ($phone) {
        $body['viewer']['phone'] = $phone; // shown on the video instead of the name
    }
    if ($atts['video'] !== '') {
        $body['videoId'] = $atts['video'];
    } else {
        $body['courseId'] = $atts['course'];
    }

    $res = wp_remote_post(XAPLAYER_API . '/embed-tokens', [
        'timeout' => 10,
        'headers' => [
            'Authorization' => 'Bearer ' . XAPLAYER_API_KEY,
            'Content-Type'  => 'application/json',
        ],
        'body' => wp_json_encode($body),
    ]);

    if (is_wp_error($res) || wp_remote_retrieve_response_code($res) !== 200) {
        return '<p>The player could not be loaded. Please refresh the page.</p>';
    }

    $url = json_decode(wp_remote_retrieve_body($res), true)['url'] ?? '';

    return sprintf(
        '<iframe src="%s" allow="fullscreen" style="width:100%%;aspect-ratio:16/9;border:0;border-radius:12px"></iframe>',
        esc_url($url)
    );
});

Use it on a lesson page#

  1. Open the lesson page or post in the editor.
  2. Add a Shortcode block with [xaplayer course="COURSE_ID"]. For a single lesson use [xaplayer video="VIDEO_ID"].
  3. Publish, then open the page with a student account.

This shortcode shows the player to any signed-in user. If only buyers should see it, restrict the page with a membership plugin or follow the WooCommerce guide.

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