wp_dashboard_cached_rss_widget

函式
wp_dashboard_cached_rss_widget ( $widget_id, $callback, $check_urls = array(), $args )
引數
  • (string) $widget_id The widget ID.
    Required:
  • (callable) $callback The callback function used to display each feed.
    Required:
  • (array) $check_urls RSS feeds.
    Required:
    Default: array()
  • (mixed) $args Optional additional parameters to pass to the callback function.
    Required:
返回值
  • (bool) True on success, false on failure.
定義位置
相關方法
wp_add_dashboard_widgetwp_ajax_dashboard_widgetswp_dashboard_rss_outputwp_dashboard_quick_presswp_dashboard_rss_control
引入
2.5.0
棄用
-

wp_dashboard_cached_rss_widget: 這是一個過濾鉤,允許你修改WordPress儀表盤上的RSS提要小工具。這個小工具顯示來自RSS提要的最新文章。

檢查$check_urls中的所有feed url是否被快取。

如果$check_urls是空的,尋找$widget_id的儀表盤小工具選項中的rss feed url。如果快取了,就呼叫$callback,一個為這個小工具撥出輸出的函式。如果沒有快取,回顯一個””載入中……””的存根,隨後被Ajax呼叫取代(見/wp-admin/index.php的頂部)。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array(), ...$args ) {
$loading = '<p class="widget-loading hide-if-no-js">' . __( 'Loading&hellip;' ) . '</p><div class="hide-if-js notice notice-error inline"><p>' . __( 'This widget requires JavaScript.' ) . '</p></div>';
$doing_ajax = wp_doing_ajax();
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;
}
function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array(), ...$args ) { $loading = '<p class="widget-loading hide-if-no-js">' . __( 'Loading&hellip;' ) . '</p><div class="hide-if-js notice notice-error inline"><p>' . __( 'This widget requires JavaScript.' ) . '</p></div>'; $doing_ajax = wp_doing_ajax(); 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; }
function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array(), ...$args ) {
	$loading    = '<p class="widget-loading hide-if-no-js">' . __( 'Loading&hellip;' ) . '</p><div class="hide-if-js notice notice-error inline"><p>' . __( 'This widget requires JavaScript.' ) . '</p></div>';
	$doing_ajax = wp_doing_ajax();

	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;
}

常見問題

FAQs
檢視更多 >