Example: members going live
Live streams of your users, the way a community has them: every member goes live, and their followers find them in the feed. This plugin does it with WordPress alone, so it runs on any site:
- A stream of their own for each member, made the first time they open the Go live page, a page with the
[go_live]shortcode. They host it, and only they do, besides the administrators. - They go live on that page, with the camera, a screen or OBS, in the studio of their stream. No access to the WordPress admin is needed.
- A post goes up as they go live, with the stream in it, the chat beside the player. It is their post, and the Live now list links to it.
- As it ends, the post says how long they were live.
<?php
/**
* Plugin Name: Members live
* Description: Every member goes live from the Go live page, and a post with the stream goes up while they are live.
*/
defined( 'ABSPATH' ) || exit;
// The stream of a member, made the first time they open the Go live page.
function members_live_stream( $user_id ) {
$stream = wordplus_streams()->get_object_stream( 'member', $user_id );
if ( $stream ) {
return $stream['id'];
}
$made = wordplus_streams()->create( array(
'title' => get_the_author_meta( 'display_name', $user_id ),
'host' => $user_id,
'object_type' => 'member',
'object_id' => $user_id,
'settings' => array( 'access' => 'logged_in', 'chat_with' => 'light' ),
) );
return is_wp_error( $made ) ? 0 : $made;
}
// [go_live]: the studio of the member, and their stream as viewers see it.
add_shortcode( 'go_live', function () {
if ( ! function_exists( 'wordplus_streams' ) || ! is_user_logged_in() ) {
return '';
}
$stream_id = members_live_stream( get_current_user_id() );
if ( ! $stream_id ) {
return '';
}
return wordplus_streams()->render( $stream_id, array( 'part' => 'studio' ) ) . wordplus_streams()->render( $stream_id, array( 'part' => 'player' ) );
} );
// As a member goes live, a post goes up, which shows the stream while it is live.
add_action( 'wordplus_stream_live', function ( $stream_id, $stream ) {
if ( 'member' !== $stream['object_type'] ) {
return;
}
$post_id = wp_insert_post( array(
'post_status' => 'publish',
'post_author' => $stream['host'],
/* translators: %s: the name of a member. */
'post_title' => sprintf( __( '%s is live', 'members-live' ), $stream['title'] ),
'meta_input' => array( 'members_live_stream' => $stream_id ),
) );
update_user_meta( $stream['host'], 'members_live_post', $post_id );
}, 10, 2 );
add_filter( 'the_content', function ( $content ) {
$stream_id = (int) get_post_meta( get_the_ID(), 'members_live_stream', true );
if ( ! $stream_id || ! function_exists( 'wordplus_streams' ) || get_the_ID() !== get_queried_object_id() ) {
return $content;
}
return $content . wordplus_streams()->render( $stream_id, array( 'layout' => 'side' ) );
} );
// The Live now list and the links the player shares go to that post.
add_filter( 'wordplus_stream_page_url', function ( $url, $stream_id ) {
$member = wordplus_streams()->get_host( $stream_id );
$post_id = $member ? (int) get_user_meta( $member, 'members_live_post', true ) : 0;
return $post_id ? get_permalink( $post_id ) : $url;
}, 10, 2 );
// As it ends, the post says how long it lasted, without the stream.
add_action( 'wordplus_stream_ended', function ( $stream_id, $stream, $times ) {
$post_id = 'member' === $stream['object_type'] ? (int) get_user_meta( $stream['host'], 'members_live_post', true ) : 0;
if ( ! $post_id ) {
return;
}
$minutes = max( 1, (int) round( $times['seconds'] / 60 ) );
wp_update_post( array(
'ID' => $post_id,
/* translators: %s: the name of a member. */
'post_title' => sprintf( __( '%s was live', 'members-live' ), $stream['title'] ),
/* translators: %d: how many minutes. */
'post_content' => sprintf( _n( 'Live for %d minute.', 'Live for %d minutes.', $minutes, 'members-live' ), $minutes ),
) );
delete_post_meta( $post_id, 'members_live_stream' );
delete_user_meta( $stream['host'], 'members_live_post' );
}, 10, 3 );
How it works
- The stream of each member is made with them as its
host, and for them (object_typemember,object_idtheir ID), sowordplus_streams()->get_object_stream()finds it again each time. They host it, and nobody else but the administrators. It stays out of the studio of the administrators, who still find it in Live Stream → Studio → Streams. - The post comes from
wordplus_stream_live, and the end fromwordplus_stream_ended: within a second where WordPlus Cloud reaches the site, else within a minute. The post shows the stream throughrender()while it is live, andwordplus_stream_page_urlmakes it the page the Live now list links to and the player shares, read withget_host(), which that filter calls for. - Viewers are logged-in users (
access), as on any stream, and a membership plugin narrows it further withwordplus_stream_can_watch. - Every stream of the site counts toward its viewers. The audiences of your members are viewers of the site like any other (Viewers and credits).
Going further
- A feed of your own: put your post, activity or status in place of the WordPress post, with the stream in it from
wordplus_streams()->render(). - Followers told: send your notices from
wordplus_stream_live, with the page of the stream ($stream['page']). - Go live in a screen drawn in the browser:
wordplusStreams.create()makes the stream from your script, wherewordplus_stream_can_createlets the user. - A stream for each live: make a new stream each time, and delete it with its post when you like.