_wp_get_attachment_relative_path example

if ( ! isset( $image_sizes['thumbnail']['mime-type'] ) || 'image/gif' !== $image_sizes['thumbnail']['mime-type'] ) {
        $image_sizes[] = array(
            'width'  => $image_meta['width'],
            'height' => $image_meta['height'],
            'file'   => $image_basename,
        );
    } elseif ( str_contains( $image_src$image_meta['file'] ) ) {
        return false;
    }

    // Retrieve the uploads sub-directory from the full size image.     $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );

    if ( $dirname ) {
        $dirname = trailingslashit( $dirname );
    }

    $upload_dir    = wp_get_upload_dir();
    $image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname;

    /* * If currently on HTTPS, prefer HTTPS URLs when we know they're supported by the domain * (which is to say, when they share the domain name of the current request). */
    $file = get_post_meta( $post->ID, '_wp_attached_file', true );
    if ( $file ) {
        // Get upload directory.         $uploads = wp_get_upload_dir();
        if ( $uploads && false === $uploads['error'] ) {
            // Check that the upload base exists in the file location.             if ( str_starts_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' ) ) {
                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads).                 $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_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, Fallback on the GUID as used pre-2.7, * not recommended to rely upon this. */
    
Home | Imprint | This part of the site doesn't use cookies.