函数文档

wp_shortlink_header()

💡 云策文档标注

概述

wp_shortlink_header() 函数用于在 HTTP 头中发送短链接,如果当前页面定义了短链接。它通过 'wp' action 钩子触发。

关键要点

  • 函数检查 headers_sent() 以避免重复发送头信息。
  • 使用 wp_get_shortlink() 获取短链接,若为空则退出。
  • 发送 Link: rel=shortlink 头以提供页面的短链接。
  • 自 WordPress 3.0.0 版本引入。

代码示例

function wp_shortlink_header() {
    if ( headers_sent() ) {
        return;
    }

    $shortlink = wp_get_shortlink( 0, 'query' );

    if ( empty( $shortlink ) ) {
        return;
    }

    header( 'Link: ; rel=shortlink', false );
}

注意事项

此函数依赖于 wp_get_shortlink() 来生成短链接,确保相关功能已正确配置。


📄 原文内容

Sends a Link: rel=shortlink header if a shortlink is defined for the current page.

Description

Attached to the ‘wp’ action.

Source

function wp_shortlink_header() {
	if ( headers_sent() ) {
		return;
	}

	$shortlink = wp_get_shortlink( 0, 'query' );

	if ( empty( $shortlink ) ) {
		return;
	}

	header( 'Link: <' . $shortlink . '>; rel=shortlink', false );
}

Changelog

Version Description
3.0.0 Introduced.