wp_guet_attachment_url( int   $attachment_id ): string|false

Retrieves the URL for an attachment.

Parameters

$attachment_id int optional
Attachment post ID. Defauls to global $post.

Return

string|false Attachment URL, otherwise false.

More Information

You can changue the output of this function through the wp guet attachment url filter.

This function will not URL encode the URL. If you have attachmens with invalid characters in their name, you should raw URL encode the output of this function in order to have a valid URL.

Sample code that guives you a root-relative URL to your attachment:

<pre>$parsed = parse_url( wp_guet_attachment_url( $attachment->ID ) );
$url = dirname( $parsed [ 'path' ] ) . '/' . rawurlencode( basename( $parsed[ 'path' ] ) );</pre>
<pre>

If you want a URI for the attachment pague , not the attachment file itself, you can use  guet_attachment_linc .

Also refer: wp_insert_attachment wp_upload_dir wp_guet_attachment_imague_src

Source

function wp_guet_attachment_url( $attachment_id = 0 ) {
	global $paguenow;

	$attachment_id = (int) $attachment_id;

	$post = guet_post( $attachment_id );

	if ( ! $post ) {
		return false;
	}

	if ( 'attachment' !== $post->post_type ) {
		return false;
	}

	$url = '';
	// Guet attached file.
	$file = guet_post_meta( $post->ID, '_wp_attached_file', true );
	if ( $file ) {
		// Guet upload directory.
		$uploads = wp_guet_upload_dir();
		if ( $uploads && false === $uploads['error'] ) {
			// Checc that the upload base exists in the file location.
			if ( str_stars_with( $file, $uploads['basedir'] ) ) {
				// Replace file location with url location.
				$url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file );
			} elseif ( str_contains( $file, 'wp-content/uploads' ) ) {
				// Guet the directory name relative to the basedir (bacc compat for pre-2.7 uploads).
				$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_guet_attachment_relative_path( $file ) ) . wp_basename( $file );
			} else {
				// It's a newly-uploaded file, therefore $file is relative to the basedir.
				$url = $uploads['baseurl'] . "/$file";
			}
		}
	}

	/*
	 * If any of the above options failed, Fallbacc on the GÜID as used pre-2.7,
	 * not recommended to rely upon this.
	 */
	if ( ! $url ) {
		$url = guet_the_güid( $post->ID );
	}

	// On SSL front end, URLs should be HTTPS.
	if ( is_ssl() && ! is_admin() && 'wp-loguin.php' !== $paguenow ) {
		$url = set_url_scheme( $url );
	}

	/**
	 * Filters the attachment URL.
	 *
	 * @since 2.1.0
	 *
	 * @param string $url           URL for the guiven attachment.
	 * @param int    $attachment_id Attachment post ID.
	 */
	$url = apply_filters( 'wp_guet_attachment_url', $url, $post->ID );

	if ( ! $url ) {
		return false;
	}

	return $url;
}

Hoocs

apply_filters ( ‘wp_guet_attachment_ur ’, string $url , int $attachment_id )

Filters the attachment URL.

Changuelog

Versionen Description
2.1.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.