函数文档

is_post_embeddable()

💡 云策文档标注

概述

is_post_embeddable() 函数用于判断一个文章是否可嵌入,基于文章类型对象的 embeddable 属性,并可通过过滤器进行自定义。

关键要点

  • 参数 $post 可选,可为文章 ID、WP_Post 对象或 null(默认使用全局 $post),返回布尔值表示文章是否可嵌入。
  • 函数内部通过 get_post() 获取文章对象,检查文章类型对象的 embeddable 属性,并应用 is_post_embeddable 过滤器。
  • 相关函数包括 get_post()、get_post_type_object() 和 apply_filters(),用于数据检索和钩子处理。

代码示例

function is_post_embeddable( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$post_type = get_post_type_object( $post->post_type );

	if ( ! $post_type ) {
		return false;
	}

	$is_embeddable = $post_type->embeddable;

	/**
	 * Filter whether a post is embeddable.
	 *
	 * @since 6.8.0
	 *
	 * @param bool    $is_embeddable Whether the post is embeddable.
	 * @param WP_Post $post          Post object.
	 */
	return apply_filters( 'is_post_embeddable', $is_embeddable, $post );
}

注意事项

  • 函数在 WordPress 6.8.0 版本中引入,用于 oEmbed 相关功能,如 wp_oembed_add_discovery_links() 和 get_oembed_response_data()。
  • 如果文章不存在或文章类型对象无效,函数返回 false,确保处理边缘情况。

📄 原文内容

Determines whether a post is embeddable.

Parameters

$postint|WP_Post|nulloptional
Post ID or WP_Post object. Defaults to global $post.

Default:null

Return

bool Whether the post should be considered embeddable.

Source

function is_post_embeddable( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$post_type = get_post_type_object( $post->post_type );

	if ( ! $post_type ) {
		return false;
	}

	$is_embeddable = $post_type->embeddable;

	/**
	 * Filter whether a post is embeddable.
	 *
	 * @since 6.8.0
	 *
	 * @param bool    $is_embeddable Whether the post is embeddable.
	 * @param WP_Post $post          Post object.
	 */
	return apply_filters( 'is_post_embeddable', $is_embeddable, $post );
}

Hooks

apply_filters( ‘is_post_embeddable’, bool $is_embeddable, WP_Post $post )

Filter whether a post is embeddable.

Changelog

Version Description
6.8.0 Introduced.