wp_dashboard_primary()
概述
wp_dashboard_primary() 函数用于定义和输出 WordPress 仪表盘中的“WordPress 事件与新闻”小工具。它配置了两个 RSS 源(新闻和 Planet),并通过 wp_dashboard_cached_rss_widget() 缓存和渲染内容。
关键要点
- 函数返回一个包含新闻和 Planet 源的数组,每个源有链接、URL、标题、项目数等配置。
- 提供了多个过滤器(如 dashboard_primary_feed、dashboard_secondary_items)允许开发者自定义源 URL、链接、标题和项目数量。
- 使用 wp_dashboard_cached_rss_widget() 处理 RSS 缓存和输出,以提高性能。
- 在 WordPress 4.8.0 版本中移除了流行插件源,从 2.7.0 版本引入。
代码示例
function wp_dashboard_primary() {
$feeds = array(
'news' => array(
'link' => apply_filters('dashboard_primary_link', __('https://wordpress.org/news/')),
'url' => apply_filters('dashboard_primary_feed', __('https://wordpress.org/news/feed/')),
'title' => apply_filters('dashboard_primary_title', __('WordPress Blog')),
'items' => 2,
'show_summary' => 0,
'show_author' => 0,
'show_date' => 0
),
'planet' => array(
'link' => apply_filters('dashboard_secondary_link', __('https://planet.wordpress.org/')),
'url' => apply_filters('dashboard_secondary_feed', __('https://planet.wordpress.org/feed/')),
'title' => apply_filters('dashboard_secondary_title', __('Other WordPress News')),
'items' => apply_filters('dashboard_secondary_items', 3),
'show_summary' => 0,
'show_author' => 0,
'show_date' => 0
)
);
wp_dashboard_cached_rss_widget('dashboard_primary', 'wp_dashboard_primary_output', $feeds);
}注意事项
- 过滤器参数如 dashboard_secondary_items 从 4.4.0 版本开始可用,允许动态调整项目数量。
- 函数依赖于 __() 进行国际化翻译,确保链接和标题可本地化。
- 相关函数包括 wp_dashboard_events_news() 和 wp_ajax_dashboard_widgets(),用于渲染和处理 AJAX 请求。
‘WordPress Events and News’ dashboard widget.
Source
function wp_dashboard_primary() {
$feeds = array(
'news' => array(
/**
* Filters the primary link URL for the 'WordPress Events and News' dashboard widget.
*
* @since 2.5.0
*
* @param string $link The widget's primary link URL.
*/
'link' => apply_filters( 'dashboard_primary_link', __( 'https://wordpress.org/news/' ) ),
/**
* Filters the primary feed URL for the 'WordPress Events and News' dashboard widget.
*
* @since 2.3.0
*
* @param string $url The widget's primary feed URL.
*/
'url' => apply_filters( 'dashboard_primary_feed', __( 'https://wordpress.org/news/feed/' ) ),
/**
* Filters the primary link title for the 'WordPress Events and News' dashboard widget.
*
* @since 2.3.0
*
* @param string $title Title attribute for the widget's primary link.
*/
'title' => apply_filters( 'dashboard_primary_title', __( 'WordPress Blog' ) ),
'items' => 2,
'show_summary' => 0,
'show_author' => 0,
'show_date' => 0,
),
'planet' => array(
/**
* Filters the secondary link URL for the 'WordPress Events and News' dashboard widget.
*
* @since 2.3.0
*
* @param string $link The widget's secondary link URL.
*/
'link' => apply_filters(
'dashboard_secondary_link',
/* translators: Link to the Planet website of the locale. */
__( 'https://planet.wordpress.org/' )
),
/**
* Filters the secondary feed URL for the 'WordPress Events and News' dashboard widget.
*
* @since 2.3.0
*
* @param string $url The widget's secondary feed URL.
*/
'url' => apply_filters(
'dashboard_secondary_feed',
/* translators: Link to the Planet feed of the locale. */
__( 'https://planet.wordpress.org/feed/' )
),
/**
* Filters the secondary link title for the 'WordPress Events and News' dashboard widget.
*
* @since 2.3.0
*
* @param string $title Title attribute for the widget's secondary link.
*/
'title' => apply_filters( 'dashboard_secondary_title', __( 'Other WordPress News' ) ),
/**
* Filters the number of secondary link items for the 'WordPress Events and News' dashboard widget.
*
* @since 4.4.0
*
* @param string $items How many items to show in the secondary feed.
*/
'items' => apply_filters( 'dashboard_secondary_items', 3 ),
'show_summary' => 0,
'show_author' => 0,
'show_date' => 0,
),
);
wp_dashboard_cached_rss_widget( 'dashboard_primary', 'wp_dashboard_primary_output', $feeds );
}
Hooks
- apply_filters( ‘dashboard_primary_feed’, string $url )
-
Filters the primary feed URL for the ‘WordPress Events and News’ dashboard widget.
- apply_filters( ‘dashboard_primary_link’, string $link )
-
Filters the primary link URL for the ‘WordPress Events and News’ dashboard widget.
- apply_filters( ‘dashboard_primary_title’, string $title )
-
Filters the primary link title for the ‘WordPress Events and News’ dashboard widget.
- apply_filters( ‘dashboard_secondary_feed’, string $url )
-
Filters the secondary feed URL for the ‘WordPress Events and News’ dashboard widget.
- apply_filters( ‘dashboard_secondary_items’, string $items )
-
Filters the number of secondary link items for the ‘WordPress Events and News’ dashboard widget.
- apply_filters( ‘dashboard_secondary_link’, string $link )
-
Filters the secondary link URL for the ‘WordPress Events and News’ dashboard widget.
- apply_filters( ‘dashboard_secondary_title’, string $title )
-
Filters the secondary link title for the ‘WordPress Events and News’ dashboard widget.