Integration

Show the song that's playing on your station's WordPress site

Put AudD's now-playing widget on a WordPress site: paste it into a Custom HTML block, or add a small plugin with a shortcode that keeps your API token in wp-config.php. Recently played songs, the look, and what to check when the widget shows nothing.

view .md auddwordpressnow playingradio station

If your radio station’s website runs on WordPress, you can show your listeners the song that’s playing now. You can use the AudD real-time music recognition service for audio streams to identify the songs that are being played on your station. We also provide a widget displaying the last identified song, and you can add it to any page with a Custom HTML block:

<iframe src="https://widget.audd.tech/?ch=-22cd61d45&background"
        width="400" height="150" style="border:0; max-width:100%"
        title="Now playing on Radio Example"></iframe>

Here, 22cd61d45 is an example of a stream’s longpoll category. Steps 1 and 2 get you the category of your stream. If other people edit the site, or you want the widget on many pages, use the small plugin below: it adds a shortcode, gets the category for you, and keeps your API token on the server.

What you need

  • An AudD API token with one stream. You can get a token from our API Dashboard. Set the limit of audio streams and pay on the API Dashboard: stream monitoring is $45 per stream per month. New accounts that get the free trial may also get a 3-day trial of one stream. The test token doesn’t work for streams.
  • The address of your station’s stream: the URL that players connect to, not your website. We support most of the stream formats, including DASH, Icecast, HLS, m3u/m3u8, and plain http:// URLs, so an Icecast or SHOUTcast server with its default settings is fine. See Icecast, SHOUTcast and AzuraCast for the URL to use.
  • A WordPress user that can save an iframe (see the note in Step 3), or access to your site’s files to add a small plugin.

Step 1: Add your stream

You don’t need to send any API requests: our web interface allows adding, removing, and updating streams.

  1. Open audd.io/streams, enter your API token and click Submit.
  2. Under Callback URL, enter https://audd.tech/empty/ and click Update Callback URL. If the field already has an address, leave it as it is.
  3. Under Add Stream, enter the address of your stream and 1 as the Radio ID. As the Callback Type, choose Before (when song starts). Click Add Stream.
  4. Your stream appears under Current Streams. Click Open Widget to see the widget in a new window.

The widget needs a callback URL on your account, even if you don’t have a server: https://audd.tech/empty/ always responds with 200 OK. With Before (when song starts), we send the results as soon as a song starts playing. By default, we would send them after songs end playing, and the widget would show each song once it’s over.

Step 2: Get the category

If you’re going to use the plugin, skip this step: the plugin gets the category for you.

Use the longpoll category with ”-” before it as the ch URL parameter. As the category, use the longpoll_category field from the getStreams response: open this address in your browser, with your API token in it.

https://api.audd.io/getStreams/?api_token=your-api-token

The browser shows all the streams you sent us. Find "radio_id": 1, and copy the longpoll_category next to it:

{"status":"success","result":[{"radio_id":1,"url":"http://radio.example:8000/stream","stream_running":true,"longpoll_category":"22cd61d45"}]}

Note. The address contains your API token. You can share the categories with your listeners, but please don’t include your API tokens in your website’s pages.

If you use the AudD CLI, you can also get the category with audd streams list --format json.

Step 3: Add the widget to a page

  1. Click on the Block Inserter icon and choose Custom HTML. You can also type /html and hit Enter in a new paragraph block to add one quickly.
  2. Once you add the block, click on the Edit HTML button, and paste the code from the top of this page.
  3. Put your category after ch=-, and your station’s name in title.
  4. When you are done entering code, press the Update button. Then save the page.

The song is shown in the top 150 pixels of the widget, so you can set the height to 150. Thanks to max-width:100%, the widget will fit on a phone, too. Please also put your station’s name in title: people navigating with assistive technology can use the title attribute to label the frame’s content.

If you want the widget on every page, add the block to the sidebar or the footer. With a classic theme, you can customize your widget areas, like Footer and Sidebar, using blocks in Appearance > Widgets. Block themes use the Site Editor instead: add the block to the footer template part there.

Note. Only some users can save an iframe in a Custom HTML block. WordPress removes the <iframe> tag when a user without the unfiltered_html capability saves the page. On a single site, Administrators and Editors have it; in WordPress Multisite, only Super Admins do. Everyone else can use the shortcode.

Show the songs played before

Add history to the address, and make the frame taller. The history goes under the song, and you can scroll it.

<iframe src="https://widget.audd.tech/?ch=-22cd61d45&background&history"
        width="400" height="300" style="border:0; max-width:100%"
        title="Recently played on Radio Example"></iframe>

For every song, the history has the time, the artist and the title, with a link to the song’s page. Before we recognize the first song, you’ll see this text instead: “Once a song is recognized, it will appear on the top of the extension. The history will be shown here.”

Change the look

You can also use the URL parameters to get the look you want. Add them to the end of the widget address, each one after &.

background puts the song on a colorful panel, with a faded, grey copy of the cover behind it, and shadow adds a shadow under the cover. With black-font, the text is black instead of white. And with history, the widget shows the songs played before the last one.

Without background, the widget has no background, and the text is white: it’s made to be placed on top of a video. On a light page, keep background, or add black-font. The cover links to the song’s page, which opens in a new tab. There’s also a small AudD logo in the bottom-right corner.

Reuse the widget with a shortcode

Shortcodes can embed files or create objects that would normally require lots of complicated, ugly code in just one line. The plugin below adds one, [audd_now_playing], and it’s useful if:

  • your pages are edited by people who can’t save an iframe, like Authors, or anyone but Super Admins on Multisite;
  • you want the widget on many pages, and its look in one place;
  • you don’t want to paste your token into a browser: the plugin gets the categories with your token, and the token stays on your server.

1. Add your token to wp-config.php

Please make sure you have a backup of wp-config.php and know how to restore it. The file is in the folder where WordPress is installed. Add the define line to it, above the line that says “That’s all, stop editing!”:

define( 'AUDD_API_TOKEN', 'your-api-token' );

/* That's all, stop editing! Happy publishing. */

Your database password and the secret keys of your site are in this file too. A plugin’s settings are stored in the database, in the options table; the token in wp-config.php isn’t stored there, and no page on your site shows it.

2. Add the plugin

Save this code as wp-content/plugins/audd-now-playing/audd-now-playing.php:

<?php
/**
 * Plugin Name:  AudD Now Playing
 * Description:  Shows the song that's playing on your station with the AudD widget: [audd_now_playing radio_id="1"]
 * Version:      1.0.0
 * Requires PHP: 7.4
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Gets the streams on your AudD account (the getStreams API method).
 *
 * @return array|WP_Error radio_id => array( 'url', 'running', 'category' ), or the error.
 */
function auddnp_fetch_streams() {
	if ( ! defined( 'AUDD_API_TOKEN' ) || '' === AUDD_API_TOKEN ) {
		return new WP_Error( 'auddnp_no_token', 'Add AUDD_API_TOKEN to wp-config.php first.' );
	}

	$response = wp_remote_post(
		'https://api.audd.io/getStreams/',
		array(
			'timeout' => 30,
			'body'    => array( 'api_token' => AUDD_API_TOKEN ),
		)
	);
	if ( is_wp_error( $response ) ) {
		return $response;
	}

	$body = json_decode( wp_remote_retrieve_body( $response ), true );
	if ( ! is_array( $body ) ) {
		return new WP_Error( 'auddnp_bad_response', 'AudD answered with HTTP ' . wp_remote_retrieve_response_code( $response ) . ' and no JSON. Try again later.' );
	}
	if ( 'success' !== ( $body['status'] ?? '' ) ) {
		$error = isset( $body['error'] ) && is_array( $body['error'] ) ? $body['error'] : array();
		return new WP_Error(
			'auddnp_api_error',
			sprintf( 'AudD error %d: %s', (int) ( $error['error_code'] ?? 0 ), (string) ( $error['error_message'] ?? 'unknown error' ) )
		);
	}

	$streams = array();
	foreach ( (array) ( $body['result'] ?? array() ) as $stream ) {
		if ( is_array( $stream ) && isset( $stream['radio_id'] ) && ! empty( $stream['longpoll_category'] ) ) {
			$streams[ (int) $stream['radio_id'] ] = array(
				'url'      => (string) ( $stream['url'] ?? '' ),
				'running'  => filter_var( $stream['stream_running'] ?? false, FILTER_VALIDATE_BOOLEAN ),
				'category' => (string) $stream['longpoll_category'],
			);
		}
	}
	return $streams;
}

/* Settings > AudD Now Playing: a button that loads the streams, and a table of them. */

add_action( 'admin_menu', 'auddnp_add_settings_page' );

function auddnp_add_settings_page() {
	add_options_page( 'AudD Now Playing', 'AudD Now Playing', 'manage_options', 'auddnp', 'auddnp_settings_page' );
}

function auddnp_settings_page() {
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}
	$streams = get_option( 'auddnp_streams', array() );
	$notice  = get_transient( 'auddnp_notice' );
	delete_transient( 'auddnp_notice' );
	?>
	<div class="wrap">
		<h1>AudD Now Playing</h1>
		<?php if ( is_array( $notice ) ) : ?>
			<div class="notice notice-<?php echo esc_attr( $notice['type'] ); ?>"><p><?php echo esc_html( $notice['message'] ); ?></p></div>
		<?php endif; ?>
		<p>The plugin asks AudD for the streams on your account. Your API token stays in wp-config.php and isn't shown here.</p>
		<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post">
			<input type="hidden" name="action" value="auddnp_load_streams">
			<?php wp_nonce_field( 'auddnp_load_streams' ); ?>
			<?php submit_button( 'Load streams from AudD' ); ?>
		</form>
		<?php if ( empty( $streams ) ) : ?>
			<p>No streams loaded yet.</p>
		<?php else : ?>
			<table class="widefat striped">
				<thead><tr><th>radio_id</th><th>Stream URL</th><th>Running</th><th>Shortcode</th></tr></thead>
				<tbody>
				<?php foreach ( $streams as $radio_id => $stream ) : ?>
					<tr>
						<td><?php echo esc_html( $radio_id ); ?></td>
						<td><?php echo esc_html( $stream['url'] ); ?></td>
						<td><?php echo $stream['running'] ? 'Yes' : 'No'; ?></td>
						<td><code>[audd_now_playing radio_id="<?php echo esc_html( $radio_id ); ?>"]</code></td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
	</div>
	<?php
}

add_action( 'admin_post_auddnp_load_streams', 'auddnp_load_streams' );

function auddnp_load_streams() {
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( 'You are not allowed to change these settings.', 403 );
	}
	check_admin_referer( 'auddnp_load_streams' );

	$streams = auddnp_fetch_streams();
	if ( is_wp_error( $streams ) ) {
		$notice = array( 'type' => 'error', 'message' => $streams->get_error_message() );
	} else {
		update_option( 'auddnp_streams', $streams );
		$notice = array( 'type' => 'success', 'message' => sprintf( 'Loaded %d stream(s).', count( $streams ) ) );
	}
	set_transient( 'auddnp_notice', $notice, 60 );

	wp_safe_redirect( admin_url( 'options-general.php?page=auddnp' ) );
	exit;
}

/* [audd_now_playing radio_id="1" style="background history" width="400" height="300" title="Now playing"] */

add_shortcode( 'audd_now_playing', 'auddnp_shortcode' );

function auddnp_shortcode( $atts = array() ) {
	$atts = shortcode_atts(
		array(
			'radio_id' => '',
			'style'    => 'background',
			'width'    => 400,
			'height'   => 0,
			'title'    => 'Now playing',
		),
		array_change_key_case( (array) $atts, CASE_LOWER ),
		'audd_now_playing'
	);

	$streams  = get_option( 'auddnp_streams', array() );
	$radio_id = absint( $atts['radio_id'] );
	if ( empty( $streams[ $radio_id ]['category'] ) ) {
		if ( current_user_can( 'manage_options' ) ) {
			return '<p>' . esc_html( "AudD Now Playing: no stream with radio_id $radio_id. Click Load streams from AudD in Settings > AudD Now Playing." ) . '</p>';
		}
		return ''; // Visitors see nothing.
	}

	$url     = 'https://widget.audd.tech/?ch=-' . rawurlencode( $streams[ $radio_id ]['category'] );
	$options = preg_split( '/[\s,]+/', strtolower( (string) $atts['style'] ), -1, PREG_SPLIT_NO_EMPTY );
	foreach ( array( 'background', 'history', 'shadow', 'black-font' ) as $option ) {
		if ( in_array( $option, $options, true ) ) {
			$url .= '&' . $option;
		}
	}
	$height = absint( $atts['height'] );
	if ( 0 === $height ) {
		$height = in_array( 'history', $options, true ) ? 300 : 150;
	}

	return sprintf(
		'<iframe src="%s" width="%d" height="%d" title="%s" style="border:0;max-width:100%%"></iframe>',
		esc_url( $url ),
		absint( $atts['width'] ),
		$height,
		esc_attr( $atts['title'] )
	);
}

register_uninstall_hook( __FILE__, 'auddnp_uninstall' );

function auddnp_uninstall() {
	delete_option( 'auddnp_streams' );
}

After you save the file, you should be able to see the plugin in your WordPress site. Click Plugins in the admin menu, find AudD Now Playing in the list, and click Activate.

3. Load your streams

In Settings > AudD Now Playing, click Load streams from AudD. The plugin uses the getStreams method to get all the streams you sent us, and shows them in a table with their shortcodes. If you add a stream or change your API token, click the button again: the category is calculated from your API token and the radio_id.

4. Add the shortcode

Add a Shortcode block where you want the widget: it can be found in the Widgets section of the Block Inserter, or you can type /shortcode and press Enter. Then paste the shortcode from the table:

[audd_now_playing radio_id="1"]
[audd_now_playing radio_id="1" style="history black-font" title="Recently played on Radio Example"]

The shortcode needs the radio_id of your stream. In style, you can list the widget’s URL parameters, separated by spaces: background (the default), history, shadow and black-font. width is 400 pixels by default, and height is 150, or 300 with history. title is the label of the frame, “Now playing” by default.

How the plugin works

The plugin only sends a request to our API when you click Load streams from AudD. It calls the getStreams method and saves the list of your streams; the shortcode reads that list, so your server doesn’t call our API when someone opens a page. Here’s how it follows the WordPress developer handbooks:

  • It uses wp_remote_post() from the WordPress HTTP API to send the request, with the token in the api_token form field and a timeout of 30 seconds (the default is 5).
  • The button sends a form to admin-post.php with a nonce, which wp_nonce_field() adds and check_admin_referer() checks. Nonces should never be relied on for authentication, authorization, or access control, so the handler checks first that you have the manage_options capability with current_user_can().
  • It escapes everything it prints with esc_html(), esc_attr() or esc_url(), as late as possible.
  • All its functions and its option have the auddnp_ prefix, and the file exits if someone opens it directly, as the best practices ask.
  • Its uninstall hook removes the saved list from the options table when you delete the plugin.

On a site that uses Composer: the PHP SDK

If you use Composer, you can make the request with our PHP SDK instead. It requires PHP 8.1 or later. Install it in the plugin’s folder:

composer require audd/audd

After that, set Requires PHP to 8.1 in the plugin’s header, load the SDK with require __DIR__ . '/vendor/autoload.php'; after the ABSPATH check, and use this version of auddnp_fetch_streams():

function auddnp_fetch_streams() {
	if ( ! defined( 'AUDD_API_TOKEN' ) || '' === AUDD_API_TOKEN ) {
		return new WP_Error( 'auddnp_no_token', 'Add AUDD_API_TOKEN to wp-config.php first.' );
	}
	try {
		$list = ( new \AudD\AudD( AUDD_API_TOKEN ) )->streams()->list();
	} catch ( \AudD\Errors\AudDException $e ) {
		return new WP_Error( 'auddnp_api_error', $e->getMessage() );
	}

	$streams = array();
	foreach ( $list as $stream ) {
		if ( $stream->longpoll_category ) {
			$streams[ $stream->radio_id ] = array(
				'url'      => $stream->url,
				'running'  => $stream->stream_running,
				'category' => $stream->longpoll_category,
			);
		}
	}
	return $streams;
}

If our servers respond with an HTTP 5xx error, the SDK tries again. On a site without Composer, you would have to install the SDK on your own computer and upload the plugin together with its vendor folder. We used wp_remote_post() in the plugin above for this reason: you don’t have to install anything.

When the widget shows nothing

Before the first song

If we haven’t recognized any songs on your stream yet, the widget has nothing to show. You’ll see the first song seconds or minutes after you add the stream, while music is playing on it. After that, the widget shows the last recognized song until we recognize the next one.

The iframe is gone after you save the page

WordPress removes the <iframe> tag if the user who saves the page doesn’t have the unfiltered_html capability. Ask an Administrator to add the block, or use the shortcode instead.

White text on a white page

Without background, the text is white and the widget has no background. Add &background or &black-font to the address. If you set style in the shortcode, keep background in it, or add black-font.

The shortcode shows nothing

Please make sure that you’ve clicked Load streams from AudD in Settings > AudD Now Playing, and that the table has the radio_id from your shortcode. If you see the shortcode itself on the page, like [audd_now_playing radio_id="1"], the plugin isn’t active.

The address

The ch parameter is the category with ”-” before it: ch=-22cd61d45, not ch=22cd61d45.

The callback URL

If the account has no callback URL, the widget gets no songs. Set https://audd.tech/empty/ as in Step 1. If your callback URL has disable-lastsong=true in it, remove it: it disables the longpoll for your streams, and the widget gets the songs with longpoll.

The songs show up late

By default, we will send new results after songs end playing. If that’s what you see, delete the stream in the web interface and add it again with Before (when song starts). Use the same Radio ID: the category is calculated from your API token and the Radio ID, so the widget address won’t change.

The stream has a problem

If something happens to an audio stream, we’ll let you know by sending a notification to your callback URL, but https://audd.tech/empty/ just discards it. So look at the Running column in the plugin’s table, or at the stream’s Status in the web interface (click Refresh Streams). If the stream isn’t running, check its address, and change it with Update URL. See Stream health for more.

Load streams from AudD shows an error

If you see “AudD error 900”, please check the token in wp-config.php, and the status of your account on dashboard.audd.io: it needs to have either a trial or an active subscription. If you see “cURL error” or “AudD answered with HTTP 502”, your server couldn’t connect to ours or didn’t get a proper answer. Please try again in a few minutes. For the other codes, see the error codes reference.

A design of your own

If you want to design the widget yourself, you’ll need a server that receives the callbacks. In Build a now-playing widget for a livestream, you’ll find the server and an HTML widget that you can change.


Related

Reading this as an AI agent? The raw Markdown is at integrations/wordpress.md, and the full index is /resources/llms.txt.