wp_attachment_is() - WordPress 函数参考中文文档

返回介绍

wp_attachment_is()

发布于 2017-09-11 字数2976 浏览 897 评论 0

wp_attachment_is( string $type,  int|WP_Post $post = null )

Verifies an attachment is of a given type.


描述


参数

$type

(string)
(Required)
Attachment type. Accepts ‘image’, ‘audio’, or ‘video’.

$post

(int|WP_Post)
(Optional)
Attachment ID or object. Default is global $post.

Default value: null


返回值

(bool) True if one of the accepted types, false otherwise.


源代码

File: wp-includes/post.php

function wp_attachment_is( $type, $post = null ) {
	if ( ! $post = get_post( $post ) ) {
		return false;
	}

	if ( ! $file = get_attached_file( $post->ID ) ) {
		return false;
	}

	if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) {
		return true;
	}

	$check = wp_check_filetype( $file );
	if ( empty( $check['ext'] ) ) {
		return false;
	}

	$ext = $check['ext'];

	if ( 'import' !== $post->post_mime_type ) {
		return $type === $ext;
	}

	switch ( $type ) {
	case 'image':
		$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
		return in_array( $ext, $image_exts );

	case 'audio':
		return in_array( $ext, wp_get_audio_extensions() );

	case 'video':
		return in_array( $ext, wp_get_video_extensions() );

	default:
		return $type === $ext;
	}
}

更新日志

Version 描述
4.2.0 Introduced.

相关函数

Uses

  • wp-includes/functions.php:
    wp_check_filetype()
  • wp-includes/media.php:
    wp_get_video_extensions()
  • wp-includes/media.php:
    wp_get_audio_extensions()
  • wp-includes/post.php:
    get_post()
  • wp-includes/post.php:
    get_attached_file()

Used By

  • wp-includes/widgets/class-wp-widget-media.php:
    WP_Widget_Media::is_attachment_with_mime_type()
  • wp-includes/embed.php:
    get_oembed_response_data_rich()
  • wp-admin/includes/image.php:
    wp_generate_attachment_metadata()
  • wp-admin/includes/media.php:
    edit_form_image_editor()
  • wp-admin/includes/ajax-actions.php:
    wp_ajax_send_attachment_to_editor()
  • wp-admin/includes/ajax-actions.php:
    wp_ajax_save_attachment()
  • wp-includes/post-template.php:
    prepend_attachment()
  • wp-includes/media.php:
    wp_enqueue_media()
  • wp-includes/post.php:
    wp_attachment_is_image()
  • wp-includes/post.php:
    wp_insert_post()
  • Show 5 more used by
    Hide more used by


User Contributed Notes

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!