wp_dashboard_cached_rss_widget()WP 2.5.0

Checks to see if all of the feed url in $check_urls are cached.

If $check_urls is empty, look for the rss feed url found in the dashboard widget options of $widget_id. If cached, call $callback, a function that echoes out output for this widget. If not cache, echo a "Loading..." stub which is later replaced by Ajax call (see top of /wp-admin/index.php)

No Hooks.

Return

true|false. True on success, false on failure.

Usage

wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls, ...$args );
$widget_id(string) (required)
The widget ID.
$callback(callable) (required)
The callback function used to display each feed.
$check_urls(array)
RSS feeds.
Default: array()
...$args(mixed) (required)
Optional additional parameters to pass to the callback function.

Changelog

Since 2.5.0 Introduced.
Since 5.3.0 Formalized the existing and already documented ...$args parameter by adding it to the function signature.

wp_dashboard_cached_rss_widget() code WP 6.6.2

function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array(), ...$args ) {
	$doing_ajax = wp_doing_ajax();
	$loading    = '<p class="widget-loading hide-if-no-js">' . __( 'Loading&hellip;' ) . '</p>';
	$loading   .= wp_get_admin_notice(
		__( 'This widget requires JavaScript.' ),
		array(
			'type'               => 'error',
			'additional_classes' => array( 'inline', 'hide-if-js' ),
		)
	);

	if ( empty( $check_urls ) ) {
		$widgets = get_option( 'dashboard_widget_options' );

		if ( empty( $widgets[ $widget_id ]['url'] ) && ! $doing_ajax ) {
			echo $loading;
			return false;
		}

		$check_urls = array( $widgets[ $widget_id ]['url'] );
	}

	$locale    = get_user_locale();
	$cache_key = 'dash_v2_' . md5( $widget_id . '_' . $locale );
	$output    = get_transient( $cache_key );

	if ( false !== $output ) {
		echo $output;
		return true;
	}

	if ( ! $doing_ajax ) {
		echo $loading;
		return false;
	}

	if ( $callback && is_callable( $callback ) ) {
		array_unshift( $args, $widget_id, $check_urls );
		ob_start();
		call_user_func_array( $callback, $args );
		// Default lifetime in cache of 12 hours (same as the feeds).
		set_transient( $cache_key, ob_get_flush(), 12 * HOUR_IN_SECONDS );
	}

	return true;
}