wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



wp_attachment_is › WordPress Function

Seit4.2.0
Veraltetn/v
wp_attachment_is ( $type, $post = null )
Parameter: (2)
  • (string) $type Attachment type. Accepts `image`, `audio`, `video`, or a file extension.
    Erforderlich: Ja
  • (int|WP_Post) $post Optional. Attachment ID or object. Default is global $post.
    Erforderlich: Nein
    Standard: null
Gibt zurück:
  • (bool) True if an accepted type or a matching file extension, false otherwise.
Definiert in:
Codex:

Verifies an attachment is of a given type.



Quellcode

function wp_attachment_is( $type, $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$file = get_attached_file( $post->ID );

	if ( ! $file ) {
		return false;
	}

	if ( str_starts_with( $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', 'webp', 'avif', 'heic' );
			return in_array( $ext, $image_exts, true );

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

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

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