钩子文档

rightnow_end

💡 云策文档标注

概述

rightnow_end 是一个 WordPress 动作钩子,在仪表盘的 'At a Glance' 小部件末尾触发。该钩子允许开发者在 'At a Glance' 小部件中添加自定义内容,例如显示服务器信息或自定义文章类型统计。

关键要点

  • rightnow_end 钩子在 'At a Glance' 仪表盘小部件的末尾执行,用于添加自定义输出。
  • 在 WordPress 3.8.0 之前,该小部件名为 'Right Now',但钩子名称保持不变。
  • 开发者可以使用 add_action('rightnow_end', 'callback_function') 来挂接自定义函数,以扩展小部件功能。

代码示例

add_action('rightnow_end', 'wpdocs_display_php_mysql_versions');

function wpdocs_display_php_mysql_versions() {
    echo wp_kses(
        sprintf(
            __( 'You are running on PHP %1$s and MySQL %2$s.', 'text_domain' ),
            phpversion(),
            $GLOBALS['wpdb']->db_version()
        ),
        array(
            'p' => array(),
            'strong' => array(),
        )
    );
}

注意事项

  • 确保自定义函数安全输出,使用 wp_kses 等函数过滤内容,防止 XSS 攻击。
  • 在添加自定义内容前,检查相关文章类型或条件是否存在,以避免错误。

📄 原文内容

Fires at the end of the ‘At a Glance’ dashboard widget.

Description

Prior to 3.8.0, the widget was named ‘Right Now’.

Source

do_action( 'rightnow_end' );

Changelog

Version Description
2.5.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Add something at the end of the ‘At a Glance’ dashboard widget.

    if ( ! function_exists( 'wpdocs_display_php_mysql_versions' ) ) {
    
    	add_action( 'rightnow_end', 'wpdocs_display_php_mysql_versions' );
    
    	/**
    	 * Displays the current server's PHP and MySQL versions right below the WordPress version
    	 * in the At a Glance [ Right Now ] dashboard widget.
    	 */
    	function wpdocs_display_php_mysql_versions() {
    		echo wp_kses(
    			sprintf(
    				__( '<p>You are running on <strong>PHP %1$s</strong> and <strong>MySQL %2$s</strong>.</p>', 'text_domain' ),
    				phpversion(),
    				$GLOBALS['wpdb']->db_version()
    			),
    			array(
    				'p' => array(),
    				'strong' => array(),
    			)
    		);
    	}
    }

  2. Skip to note 4 content

    Example:

    add_action('rightnow_end', 'add_recipe_counts');
    
    function add_recipe_counts() {
            if (!post_type_exists('recipes')) {
                 return;
            }
    
            $num_posts = wp_count_posts( 'recipes' );
            $num = number_format_i18n( $num_posts->publish );
            $text = _n( 'Recipe', 'Recipes', intval($num_posts->publish) );
            if ( current_user_can( 'edit_posts' ) ) {
                $num = "<a href='edit.php?post_type=recipes'>$num</a>";
                $text = "<a href='edit.php?post_type=recipes'>$text</a>";
            }
            echo '<tr>';
            echo '<td class="first b b-recipes">' . $num . '</td>';
            echo '<td class="t recipes">' . $text . '</td>';
            echo '</tr>';
    
            if ($num_posts->pending > 0) {
                $num = number_format_i18n( $num_posts->pending );
                $text = _n( 'Recipe Pending', 'Recipes Pending', intval($num_posts->pending) );
                if ( current_user_can( 'edit_posts' ) ) {
                    $num = "<a href='edit.php?post_status=pending&post_type=recipes'>$num</a>";
                    $text = "<a href='edit.php?post_status=pending&post_type=recipes'>$text</a>";
                }
                echo '<tr>';
                echo '<td class="first b b-recipes">' . $num . '</td>';
                echo '<td class="t recipes">' . $text . '</td>';
                echo '</tr>';
            }
    }