wppaste
WordPress

wp_playlist_shortcode( array $attr ): string

Since
3.9.0
Source
wp-includes/media.php:3192
Builds the Playlist shortcode output.

Description

This implements the functionality of the playlist shortcode for displaying a collection of WordPress audio or video files in a post.

Compatibility

WordPress
since 3.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$attrarray
Array of default playlist attributes.
  • $typestringdefault: 'audio'

    Type of playlist to display. Accepts 'audio' or 'video'.
  • $orderstringdefault: 'ASC'

    Designates ascending or descending order of items in the playlist. Accepts 'ASC', 'DESC'.
  • $orderbystring

    Any column, or columns, to sort the playlist. If $ids are passed, this defaults to the order of the $ids array ('post__in'). Otherwise default is 'menu_order ID'.
  • $idintdefault: is the current post ID

    If an explicit $ids array is not present, this parameter will determine which attachments are used for the playlist.
  • $idsarraydefault: empty

    Create a playlist out of these explicit attachment IDs. If empty, a playlist will be created from all $type attachments of $id.
  • $excludearraydefault: empty

    List of specific attachment IDs to exclude from the playlist.
  • $stylestringdefault: 'light'

    Playlist style to use. Accepts 'light' or 'dark'.
  • $tracklistbooldefault: true

    Whether to show or hide the playlist.
  • $tracknumbersbooldefault: true

    Whether to show or hide the numbers next to entries in the playlist.
  • $imagesbooldefault: true

    Show or hide the video or audio thumbnail (Featured Image/post thumbnail).
  • $artistsbooldefault: true

    Whether to show or hide artist name in the playlist.

Return value

string
Playlist output. Empty string if the passed type is unsupported.

Performance profile

How much work a call to wp_playlist_shortcode() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
68–201

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 407.

Plugin surface
2 hooks

Third-party callbacks on 'post_playlist', 'wp_playlist_scripts' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below wp_playlist_shortcode()

Further down the call graph this can also reach option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 13 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_playlist_shortcode() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($attr) && empty($output) && empty($atts) && empty($args) && empty($attachments)68–72get_post(), apply_filters(), audio(), get_children()
empty($attr) && empty($output) && empty($atts) && empty($args) && !empty($attachments) && is_feed()74–79get_post(), apply_filters(), audio(), get_children(), is_feed()
empty($attr) && empty($output) && empty($atts) && !empty($args) && !current_user_can()77–81get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can()
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can()81–83get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required()
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() && !post_password_required() && !empty($attachments) && is_feed()89–90get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required(), is_feed()
empty($attr) && empty($output) && empty($atts) && empty($args) && !empty($attachments) && !is_feed() && empty($content_width)160–172get_post(), apply_filters(), audio(), get_children(), is_feed(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), wp_json_encode(), ob_get_clean()
empty($attr) && empty($output) && empty($atts) && empty($args) && !empty($attachments) && !is_feed() && !empty($content_width)164–176get_post(), apply_filters(), audio(), get_children(), is_feed(), round(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), wp_json_encode(), ob_get_clean()
empty($attr) && empty($output) && empty($atts) && empty($args) && !empty($attachments) && !is_feed() && empty($content_width)170–182get_post(), apply_filters(), audio(), get_children(), is_feed(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), do_action(), wp_json_encode(), ob_get_clean()
empty($attr) && empty($output) && empty($atts) && empty($args) && !empty($attachments) && !is_feed() && !empty($content_width)174–186get_post(), apply_filters(), audio(), get_children(), is_feed(), round(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), do_action(), wp_json_encode(), ob_get_clean()
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() && !post_password_required() && !empty($attachments) && !is_feed() && empty($content_width)175–186get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required(), is_feed(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), wp_json_encode(), ob_get_clean()
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() && !post_password_required() && !empty($attachments) && !is_feed() && !empty($content_width)179–191get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required(), is_feed(), round(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), wp_json_encode(), ob_get_clean()
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() && !post_password_required() && !empty($attachments) && !is_feed() && empty($content_width)185–196get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required(), is_feed(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), do_action(), wp_json_encode(), ob_get_clean()
1 further outcome, up to 201 instructions
empty($attr) && empty($output) && empty($atts) && !empty($args) && current_user_can() && !post_password_required() && !empty($attachments) && !is_feed() && !empty($content_width)189–201get_post(), apply_filters(), audio(), get_children(), get_post(), current_user_can(), post_password_required(), is_feed(), round(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), wp_validate_boolean(), esc_attr(), esc_attr(), ob_start(), do_action(), wp_json_encode(), ob_get_clean()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev40968–200362 more instructions than PHP 8.5
8.540768–20136
8.440768–20136
8.340768–20136
8.240768–20136
8.140768–201362 fewer instructions than PHP 7.4
7.440968–20136

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_playlist_shortcode() runs, in this order:

  1. apply_filters( post_playlist )filterline 3222 (+30 into the body)

    Filters the playlist output.

  2. do_action( wp_playlist_scripts )actionline 3404 (+212 into the body)

    Prints and enqueues playlist scripts, styles, and JavaScript templates.

Uses · 21

Show all 21

Source code

function wp_playlist_shortcode( $attr ) {	global $content_width;	$post = get_post(); 	static $instance = 0;	++$instance; 	static $is_loaded = false; 	if ( ! empty( $attr['ids'] ) ) {		// 'ids' is explicitly ordered, unless you specify otherwise.		if ( empty( $attr['orderby'] ) ) {			$attr['orderby'] = 'post__in';		}		$attr['include'] = $attr['ids'];	} 	/**	 * Filters the playlist output.	 *	 * Returning a non-empty value from the filter will short-circuit generation	 * of the default playlist output, returning the passed value instead.	 *	 * @since 3.9.0	 * @since 4.2.0 The `$instance` parameter was added.	 *	 * @param string $output   Playlist output. Default empty.	 * @param array  $attr     An array of shortcode attributes.	 * @param int    $instance Unique numeric ID of this playlist shortcode instance.	 */	$output = apply_filters( 'post_playlist', '', $attr, $instance ); 	if ( ! empty( $output ) ) {		return $output;	} 	$atts = shortcode_atts(		array(			'type'         => 'audio',			'order'        => 'ASC',			'orderby'      => 'menu_order ID',			'id'           => $post ? $post->ID : 0,			'include'      => '',			'exclude'      => '',			'style'        => 'light',			'tracklist'    => true,			'tracknumbers' => true,			'images'       => true,			'artists'      => true,		),		$attr,		'playlist'	); 	$id = (int) $atts['id']; 	if ( 'audio' !== $atts['type'] ) {		$atts['type'] = 'video';	} 	$args = array(		'post_status'    => 'inherit',		'post_type'      => 'attachment',		'post_mime_type' => $atts['type'],		'order'          => $atts['order'],		'orderby'        => $atts['orderby'],	); 	if ( ! empty( $atts['include'] ) ) {		$args['include'] = $atts['include'];		$_attachments    = get_posts( $args ); 		$attachments = array();		foreach ( $_attachments as $key => $val ) {			$attachments[ $val->ID ] = $_attachments[ $key ];		}	} elseif ( ! empty( $atts['exclude'] ) ) {		$args['post_parent'] = $id;		$args['exclude']     = $atts['exclude'];		$attachments         = get_children( $args );

Changelog

Introduced in 3.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.