函数文档

wp_get_shortlink()

💡 云策文档标注

概述

wp_get_shortlink() 函数用于返回文章、页面、附件或网站的短链接。它提供了一个标准化的短链接生成接口,允许插件通过钩子自定义短链接逻辑,默认支持生成 ?p= 格式的链接。

关键要点

  • 函数返回指定资源的短链接字符串,若无短链接则返回空字符串。
  • 支持通过 'pre_get_shortlink' 过滤器提前中断短链接生成,或通过 'get_shortlink' 过滤器修改输出。
  • 参数包括 $id(ID,默认为0表示当前资源)、$context(上下文,如 'post'、'query' 或 'media')和 $allow_slugs(是否允许使用文章别名)。
  • 默认情况下,为公开文章类型生成 ?p= 格式的链接,首页页面可能返回主页 URL。

代码示例

// 获取当前文章的短链接
$shortlink = wp_get_shortlink();

// 获取指定文章 ID 的短链接
$shortlink = wp_get_shortlink( 123, 'post', true );

// 在模板中显示短链接
echo 'Short URL: ' . wp_get_shortlink();

注意事项

  • 短链接的实际生成依赖于插件钩入,默认功能有限,主要用于文章。
  • 使用 'query' 上下文时,函数会基于当前查询自动确定 ID。
  • 返回值可能为空,需检查是否启用了短链接支持。

📄 原文内容

Returns a shortlink for a post, page, attachment, or site.

Description

This function exists to provide a shortlink tag that all themes and plugins can target.
A plugin must hook in to provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts. Plugins can short-circuit this function via the ‘pre_get_shortlink’ filter or filter the output via the ‘get_shortlink’ filter.

Parameters

$idintoptional
A post or site ID. Default is 0, which means the current post or site.
$contextstringoptional
Whether the ID is a 'site' ID, 'post' ID, or 'media' ID. If 'post', the post_type of the post is consulted. If 'query', the current query is consulted to determine the ID and context. Default 'post'.
$allow_slugsbooloptional
Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.

Default:true

Return

string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.

Source

function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
	/**
	 * Filters whether to preempt generating a shortlink for the given post.
	 *
	 * Returning a value other than false from the filter will short-circuit
	 * the shortlink generation process, returning that value instead.
	 *
	 * @since 3.0.0
	 *
	 * @param false|string $return      Short-circuit return value. Either false or a URL string.
	 * @param int          $id          Post ID, or 0 for the current post.
	 * @param string       $context     The context for the link. One of 'post' or 'query',
	 * @param bool         $allow_slugs Whether to allow post slugs in the shortlink.
	 */
	$shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );

	if ( false !== $shortlink ) {
		return $shortlink;
	}

	$post_id = 0;
	if ( 'query' === $context && is_singular() ) {
		$post_id = get_queried_object_id();
		$post    = get_post( $post_id );
	} elseif ( 'post' === $context ) {
		$post = get_post( $id );
		if ( ! empty( $post->ID ) ) {
			$post_id = $post->ID;
		}
	}

	$shortlink = '';

	// Return `?p=` link for all public post types.
	if ( ! empty( $post_id ) ) {
		$post_type = get_post_type_object( $post->post_type );

		if ( 'page' === $post->post_type
			&& 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $post->ID
		) {
			$shortlink = home_url( '/' );
		} elseif ( $post_type && $post_type->public ) {
			$shortlink = home_url( '?p=' . $post_id );
		}
	}

	/**
	 * Filters the shortlink for a post.
	 *
	 * @since 3.0.0
	 *
	 * @param string $shortlink   Shortlink URL.
	 * @param int    $id          Post ID, or 0 for the current post.
	 * @param string $context     The context for the link. One of 'post' or 'query',
	 * @param bool   $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
	 */
	return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
}

Hooks

apply_filters( ‘get_shortlink’, string $shortlink, int $id, string $context, bool $allow_slugs )

Filters the shortlink for a post.

apply_filters( ‘pre_get_shortlink’, false|string $return, int $id, string $context, bool $allow_slugs )

Filters whether to preempt generating a shortlink for the given post.

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes