Edit File by line
/home/barbar84/www/wp-inclu...
File: media.php
*
[1000] Fix | Delete
* @since 2.5.0
[1001] Fix | Delete
* @since 4.4.0 The `$srcset` and `$sizes` attributes were added.
[1002] Fix | Delete
* @since 5.5.0 The `$loading` attribute was added.
[1003] Fix | Delete
*
[1004] Fix | Delete
* @param int $attachment_id Image attachment ID.
[1005] Fix | Delete
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
[1006] Fix | Delete
* of width and height values in pixels (in that order). Default 'thumbnail'.
[1007] Fix | Delete
* @param bool $icon Optional. Whether the image should be treated as an icon. Default false.
[1008] Fix | Delete
* @param string|array $attr {
[1009] Fix | Delete
* Optional. Attributes for the image markup.
[1010] Fix | Delete
*
[1011] Fix | Delete
* @type string $src Image attachment URL.
[1012] Fix | Delete
* @type string $class CSS class name or space-separated list of classes.
[1013] Fix | Delete
* Default `attachment-$size_class size-$size_class`,
[1014] Fix | Delete
* where `$size_class` is the image size being requested.
[1015] Fix | Delete
* @type string $alt Image description for the alt attribute.
[1016] Fix | Delete
* @type string $srcset The 'srcset' attribute value.
[1017] Fix | Delete
* @type string $sizes The 'sizes' attribute value.
[1018] Fix | Delete
* @type string|false $loading The 'loading' attribute value. Passing a value of false
[1019] Fix | Delete
* will result in the attribute being omitted for the image.
[1020] Fix | Delete
* Defaults to 'lazy', depending on wp_lazy_loading_enabled().
[1021] Fix | Delete
* }
[1022] Fix | Delete
* @return string HTML img element or empty string on failure.
[1023] Fix | Delete
*/
[1024] Fix | Delete
function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
[1025] Fix | Delete
$html = '';
[1026] Fix | Delete
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
[1027] Fix | Delete
[1028] Fix | Delete
if ( $image ) {
[1029] Fix | Delete
list( $src, $width, $height ) = $image;
[1030] Fix | Delete
[1031] Fix | Delete
$attachment = get_post( $attachment_id );
[1032] Fix | Delete
$hwstring = image_hwstring( $width, $height );
[1033] Fix | Delete
$size_class = $size;
[1034] Fix | Delete
[1035] Fix | Delete
if ( is_array( $size_class ) ) {
[1036] Fix | Delete
$size_class = implode( 'x', $size_class );
[1037] Fix | Delete
}
[1038] Fix | Delete
[1039] Fix | Delete
$default_attr = array(
[1040] Fix | Delete
'src' => $src,
[1041] Fix | Delete
'class' => "attachment-$size_class size-$size_class",
[1042] Fix | Delete
'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
[1043] Fix | Delete
);
[1044] Fix | Delete
[1045] Fix | Delete
// Add `loading` attribute.
[1046] Fix | Delete
if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
[1047] Fix | Delete
$default_attr['loading'] = 'lazy';
[1048] Fix | Delete
}
[1049] Fix | Delete
[1050] Fix | Delete
$attr = wp_parse_args( $attr, $default_attr );
[1051] Fix | Delete
[1052] Fix | Delete
// If the default value of `lazy` for the `loading` attribute is overridden
[1053] Fix | Delete
// to omit the attribute for this image, ensure it is not included.
[1054] Fix | Delete
if ( array_key_exists( 'loading', $attr ) && ! $attr['loading'] ) {
[1055] Fix | Delete
unset( $attr['loading'] );
[1056] Fix | Delete
}
[1057] Fix | Delete
[1058] Fix | Delete
// Generate 'srcset' and 'sizes' if not already present.
[1059] Fix | Delete
if ( empty( $attr['srcset'] ) ) {
[1060] Fix | Delete
$image_meta = wp_get_attachment_metadata( $attachment_id );
[1061] Fix | Delete
[1062] Fix | Delete
if ( is_array( $image_meta ) ) {
[1063] Fix | Delete
$size_array = array( absint( $width ), absint( $height ) );
[1064] Fix | Delete
$srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
[1065] Fix | Delete
$sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );
[1066] Fix | Delete
[1067] Fix | Delete
if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
[1068] Fix | Delete
$attr['srcset'] = $srcset;
[1069] Fix | Delete
[1070] Fix | Delete
if ( empty( $attr['sizes'] ) ) {
[1071] Fix | Delete
$attr['sizes'] = $sizes;
[1072] Fix | Delete
}
[1073] Fix | Delete
}
[1074] Fix | Delete
}
[1075] Fix | Delete
}
[1076] Fix | Delete
[1077] Fix | Delete
/**
[1078] Fix | Delete
* Filters the list of attachment image attributes.
[1079] Fix | Delete
*
[1080] Fix | Delete
* @since 2.8.0
[1081] Fix | Delete
*
[1082] Fix | Delete
* @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
[1083] Fix | Delete
* See wp_get_attachment_image().
[1084] Fix | Delete
* @param WP_Post $attachment Image attachment post.
[1085] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[1086] Fix | Delete
* an array of width and height values in pixels (in that order).
[1087] Fix | Delete
*/
[1088] Fix | Delete
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
[1089] Fix | Delete
[1090] Fix | Delete
$attr = array_map( 'esc_attr', $attr );
[1091] Fix | Delete
$html = rtrim( "<img $hwstring" );
[1092] Fix | Delete
[1093] Fix | Delete
foreach ( $attr as $name => $value ) {
[1094] Fix | Delete
$html .= " $name=" . '"' . $value . '"';
[1095] Fix | Delete
}
[1096] Fix | Delete
[1097] Fix | Delete
$html .= ' />';
[1098] Fix | Delete
}
[1099] Fix | Delete
[1100] Fix | Delete
/**
[1101] Fix | Delete
* HTML img element representing an image attachment.
[1102] Fix | Delete
*
[1103] Fix | Delete
* @since 5.6.0
[1104] Fix | Delete
*
[1105] Fix | Delete
* @param string $html HTML img element or empty string on failure.
[1106] Fix | Delete
* @param int $attachment_id Image attachment ID.
[1107] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[1108] Fix | Delete
* an array of width and height values in pixels (in that order).
[1109] Fix | Delete
* @param bool $icon Whether the image should be treated as an icon.
[1110] Fix | Delete
* @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
[1111] Fix | Delete
* See wp_get_attachment_image().
[1112] Fix | Delete
*/
[1113] Fix | Delete
return apply_filters( 'wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr );
[1114] Fix | Delete
}
[1115] Fix | Delete
[1116] Fix | Delete
/**
[1117] Fix | Delete
* Get the URL of an image attachment.
[1118] Fix | Delete
*
[1119] Fix | Delete
* @since 4.4.0
[1120] Fix | Delete
*
[1121] Fix | Delete
* @param int $attachment_id Image attachment ID.
[1122] Fix | Delete
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
[1123] Fix | Delete
* width and height values in pixels (in that order). Default 'thumbnail'.
[1124] Fix | Delete
* @param bool $icon Optional. Whether the image should be treated as an icon. Default false.
[1125] Fix | Delete
* @return string|false Attachment URL or false if no image is available. If `$size` does not match
[1126] Fix | Delete
* any registered image size, the original image URL will be returned.
[1127] Fix | Delete
*/
[1128] Fix | Delete
function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
[1129] Fix | Delete
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
[1130] Fix | Delete
return isset( $image['0'] ) ? $image['0'] : false;
[1131] Fix | Delete
}
[1132] Fix | Delete
[1133] Fix | Delete
/**
[1134] Fix | Delete
* Get the attachment path relative to the upload directory.
[1135] Fix | Delete
*
[1136] Fix | Delete
* @since 4.4.1
[1137] Fix | Delete
* @access private
[1138] Fix | Delete
*
[1139] Fix | Delete
* @param string $file Attachment file name.
[1140] Fix | Delete
* @return string Attachment path relative to the upload directory.
[1141] Fix | Delete
*/
[1142] Fix | Delete
function _wp_get_attachment_relative_path( $file ) {
[1143] Fix | Delete
$dirname = dirname( $file );
[1144] Fix | Delete
[1145] Fix | Delete
if ( '.' === $dirname ) {
[1146] Fix | Delete
return '';
[1147] Fix | Delete
}
[1148] Fix | Delete
[1149] Fix | Delete
if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) {
[1150] Fix | Delete
// Get the directory name relative to the upload directory (back compat for pre-2.7 uploads).
[1151] Fix | Delete
$dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 );
[1152] Fix | Delete
$dirname = ltrim( $dirname, '/' );
[1153] Fix | Delete
}
[1154] Fix | Delete
[1155] Fix | Delete
return $dirname;
[1156] Fix | Delete
}
[1157] Fix | Delete
[1158] Fix | Delete
/**
[1159] Fix | Delete
* Get the image size as array from its meta data.
[1160] Fix | Delete
*
[1161] Fix | Delete
* Used for responsive images.
[1162] Fix | Delete
*
[1163] Fix | Delete
* @since 4.4.0
[1164] Fix | Delete
* @access private
[1165] Fix | Delete
*
[1166] Fix | Delete
* @param string $size_name Image size. Accepts any registered image size name.
[1167] Fix | Delete
* @param array $image_meta The image meta data.
[1168] Fix | Delete
* @return array|false {
[1169] Fix | Delete
* Array of width and height or false if the size isn't present in the meta data.
[1170] Fix | Delete
*
[1171] Fix | Delete
* @type int $0 Image width.
[1172] Fix | Delete
* @type int $1 Image height.
[1173] Fix | Delete
* }
[1174] Fix | Delete
*/
[1175] Fix | Delete
function _wp_get_image_size_from_meta( $size_name, $image_meta ) {
[1176] Fix | Delete
if ( 'full' === $size_name ) {
[1177] Fix | Delete
return array(
[1178] Fix | Delete
absint( $image_meta['width'] ),
[1179] Fix | Delete
absint( $image_meta['height'] ),
[1180] Fix | Delete
);
[1181] Fix | Delete
} elseif ( ! empty( $image_meta['sizes'][ $size_name ] ) ) {
[1182] Fix | Delete
return array(
[1183] Fix | Delete
absint( $image_meta['sizes'][ $size_name ]['width'] ),
[1184] Fix | Delete
absint( $image_meta['sizes'][ $size_name ]['height'] ),
[1185] Fix | Delete
);
[1186] Fix | Delete
}
[1187] Fix | Delete
[1188] Fix | Delete
return false;
[1189] Fix | Delete
}
[1190] Fix | Delete
[1191] Fix | Delete
/**
[1192] Fix | Delete
* Retrieves the value for an image attachment's 'srcset' attribute.
[1193] Fix | Delete
*
[1194] Fix | Delete
* @since 4.4.0
[1195] Fix | Delete
*
[1196] Fix | Delete
* @see wp_calculate_image_srcset()
[1197] Fix | Delete
*
[1198] Fix | Delete
* @param int $attachment_id Image attachment ID.
[1199] Fix | Delete
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
[1200] Fix | Delete
* width and height values in pixels (in that order). Default 'medium'.
[1201] Fix | Delete
* @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
[1202] Fix | Delete
* Default null.
[1203] Fix | Delete
* @return string|false A 'srcset' value string or false.
[1204] Fix | Delete
*/
[1205] Fix | Delete
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
[1206] Fix | Delete
$image = wp_get_attachment_image_src( $attachment_id, $size );
[1207] Fix | Delete
[1208] Fix | Delete
if ( ! $image ) {
[1209] Fix | Delete
return false;
[1210] Fix | Delete
}
[1211] Fix | Delete
[1212] Fix | Delete
if ( ! is_array( $image_meta ) ) {
[1213] Fix | Delete
$image_meta = wp_get_attachment_metadata( $attachment_id );
[1214] Fix | Delete
}
[1215] Fix | Delete
[1216] Fix | Delete
$image_src = $image[0];
[1217] Fix | Delete
$size_array = array(
[1218] Fix | Delete
absint( $image[1] ),
[1219] Fix | Delete
absint( $image[2] ),
[1220] Fix | Delete
);
[1221] Fix | Delete
[1222] Fix | Delete
return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
[1223] Fix | Delete
}
[1224] Fix | Delete
[1225] Fix | Delete
/**
[1226] Fix | Delete
* A helper function to calculate the image sources to include in a 'srcset' attribute.
[1227] Fix | Delete
*
[1228] Fix | Delete
* @since 4.4.0
[1229] Fix | Delete
*
[1230] Fix | Delete
* @param int[] $size_array {
[1231] Fix | Delete
* An array of width and height values.
[1232] Fix | Delete
*
[1233] Fix | Delete
* @type int $0 The width in pixels.
[1234] Fix | Delete
* @type int $1 The height in pixels.
[1235] Fix | Delete
* }
[1236] Fix | Delete
* @param string $image_src The 'src' of the image.
[1237] Fix | Delete
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
[1238] Fix | Delete
* @param int $attachment_id Optional. The image attachment ID. Default 0.
[1239] Fix | Delete
* @return string|false The 'srcset' attribute value. False on error or when only one source exists.
[1240] Fix | Delete
*/
[1241] Fix | Delete
function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
[1242] Fix | Delete
/**
[1243] Fix | Delete
* Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
[1244] Fix | Delete
*
[1245] Fix | Delete
* @since 4.5.0
[1246] Fix | Delete
*
[1247] Fix | Delete
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
[1248] Fix | Delete
* @param int[] $size_array {
[1249] Fix | Delete
* An array of requested width and height values.
[1250] Fix | Delete
*
[1251] Fix | Delete
* @type int $0 The width in pixels.
[1252] Fix | Delete
* @type int $1 The height in pixels.
[1253] Fix | Delete
* }
[1254] Fix | Delete
* @param string $image_src The 'src' of the image.
[1255] Fix | Delete
* @param int $attachment_id The image attachment ID or 0 if not supplied.
[1256] Fix | Delete
*/
[1257] Fix | Delete
$image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );
[1258] Fix | Delete
[1259] Fix | Delete
if ( empty( $image_meta['sizes'] ) || ! isset( $image_meta['file'] ) || strlen( $image_meta['file'] ) < 4 ) {
[1260] Fix | Delete
return false;
[1261] Fix | Delete
}
[1262] Fix | Delete
[1263] Fix | Delete
$image_sizes = $image_meta['sizes'];
[1264] Fix | Delete
[1265] Fix | Delete
// Get the width and height of the image.
[1266] Fix | Delete
$image_width = (int) $size_array[0];
[1267] Fix | Delete
$image_height = (int) $size_array[1];
[1268] Fix | Delete
[1269] Fix | Delete
// Bail early if error/no width.
[1270] Fix | Delete
if ( $image_width < 1 ) {
[1271] Fix | Delete
return false;
[1272] Fix | Delete
}
[1273] Fix | Delete
[1274] Fix | Delete
$image_basename = wp_basename( $image_meta['file'] );
[1275] Fix | Delete
[1276] Fix | Delete
/*
[1277] Fix | Delete
* WordPress flattens animated GIFs into one frame when generating intermediate sizes.
[1278] Fix | Delete
* To avoid hiding animation in user content, if src is a full size GIF, a srcset attribute is not generated.
[1279] Fix | Delete
* If src is an intermediate size GIF, the full size is excluded from srcset to keep a flattened GIF from becoming animated.
[1280] Fix | Delete
*/
[1281] Fix | Delete
if ( ! isset( $image_sizes['thumbnail']['mime-type'] ) || 'image/gif' !== $image_sizes['thumbnail']['mime-type'] ) {
[1282] Fix | Delete
$image_sizes[] = array(
[1283] Fix | Delete
'width' => $image_meta['width'],
[1284] Fix | Delete
'height' => $image_meta['height'],
[1285] Fix | Delete
'file' => $image_basename,
[1286] Fix | Delete
);
[1287] Fix | Delete
} elseif ( strpos( $image_src, $image_meta['file'] ) ) {
[1288] Fix | Delete
return false;
[1289] Fix | Delete
}
[1290] Fix | Delete
[1291] Fix | Delete
// Retrieve the uploads sub-directory from the full size image.
[1292] Fix | Delete
$dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
[1293] Fix | Delete
[1294] Fix | Delete
if ( $dirname ) {
[1295] Fix | Delete
$dirname = trailingslashit( $dirname );
[1296] Fix | Delete
}
[1297] Fix | Delete
[1298] Fix | Delete
$upload_dir = wp_get_upload_dir();
[1299] Fix | Delete
$image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname;
[1300] Fix | Delete
[1301] Fix | Delete
/*
[1302] Fix | Delete
* If currently on HTTPS, prefer HTTPS URLs when we know they're supported by the domain
[1303] Fix | Delete
* (which is to say, when they share the domain name of the current request).
[1304] Fix | Delete
*/
[1305] Fix | Delete
if ( is_ssl() && 'https' !== substr( $image_baseurl, 0, 5 ) && parse_url( $image_baseurl, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) {
[1306] Fix | Delete
$image_baseurl = set_url_scheme( $image_baseurl, 'https' );
[1307] Fix | Delete
}
[1308] Fix | Delete
[1309] Fix | Delete
/*
[1310] Fix | Delete
* Images that have been edited in WordPress after being uploaded will
[1311] Fix | Delete
* contain a unique hash. Look for that hash and use it later to filter
[1312] Fix | Delete
* out images that are leftovers from previous versions.
[1313] Fix | Delete
*/
[1314] Fix | Delete
$image_edited = preg_match( '/-e[0-9]{13}/', wp_basename( $image_src ), $image_edit_hash );
[1315] Fix | Delete
[1316] Fix | Delete
/**
[1317] Fix | Delete
* Filters the maximum image width to be included in a 'srcset' attribute.
[1318] Fix | Delete
*
[1319] Fix | Delete
* @since 4.4.0
[1320] Fix | Delete
*
[1321] Fix | Delete
* @param int $max_width The maximum image width to be included in the 'srcset'. Default '2048'.
[1322] Fix | Delete
* @param int[] $size_array {
[1323] Fix | Delete
* An array of requested width and height values.
[1324] Fix | Delete
*
[1325] Fix | Delete
* @type int $0 The width in pixels.
[1326] Fix | Delete
* @type int $1 The height in pixels.
[1327] Fix | Delete
* }
[1328] Fix | Delete
*/
[1329] Fix | Delete
$max_srcset_image_width = apply_filters( 'max_srcset_image_width', 2048, $size_array );
[1330] Fix | Delete
[1331] Fix | Delete
// Array to hold URL candidates.
[1332] Fix | Delete
$sources = array();
[1333] Fix | Delete
[1334] Fix | Delete
/**
[1335] Fix | Delete
* To make sure the ID matches our image src, we will check to see if any sizes in our attachment
[1336] Fix | Delete
* meta match our $image_src. If no matches are found we don't return a srcset to avoid serving
[1337] Fix | Delete
* an incorrect image. See #35045.
[1338] Fix | Delete
*/
[1339] Fix | Delete
$src_matched = false;
[1340] Fix | Delete
[1341] Fix | Delete
/*
[1342] Fix | Delete
* Loop through available images. Only use images that are resized
[1343] Fix | Delete
* versions of the same edit.
[1344] Fix | Delete
*/
[1345] Fix | Delete
foreach ( $image_sizes as $image ) {
[1346] Fix | Delete
$is_src = false;
[1347] Fix | Delete
[1348] Fix | Delete
// Check if image meta isn't corrupted.
[1349] Fix | Delete
if ( ! is_array( $image ) ) {
[1350] Fix | Delete
continue;
[1351] Fix | Delete
}
[1352] Fix | Delete
[1353] Fix | Delete
// If the file name is part of the `src`, we've confirmed a match.
[1354] Fix | Delete
if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
[1355] Fix | Delete
$src_matched = true;
[1356] Fix | Delete
$is_src = true;
[1357] Fix | Delete
}
[1358] Fix | Delete
[1359] Fix | Delete
// Filter out images that are from previous edits.
[1360] Fix | Delete
if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
[1361] Fix | Delete
continue;
[1362] Fix | Delete
}
[1363] Fix | Delete
[1364] Fix | Delete
/*
[1365] Fix | Delete
* Filters out images that are wider than '$max_srcset_image_width' unless
[1366] Fix | Delete
* that file is in the 'src' attribute.
[1367] Fix | Delete
*/
[1368] Fix | Delete
if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && ! $is_src ) {
[1369] Fix | Delete
continue;
[1370] Fix | Delete
}
[1371] Fix | Delete
[1372] Fix | Delete
// If the image dimensions are within 1px of the expected size, use it.
[1373] Fix | Delete
if ( wp_image_matches_ratio( $image_width, $image_height, $image['width'], $image['height'] ) ) {
[1374] Fix | Delete
// Add the URL, descriptor, and value to the sources array to be returned.
[1375] Fix | Delete
$source = array(
[1376] Fix | Delete
'url' => $image_baseurl . $image['file'],
[1377] Fix | Delete
'descriptor' => 'w',
[1378] Fix | Delete
'value' => $image['width'],
[1379] Fix | Delete
);
[1380] Fix | Delete
[1381] Fix | Delete
// The 'src' image has to be the first in the 'srcset', because of a bug in iOS8. See #35030.
[1382] Fix | Delete
if ( $is_src ) {
[1383] Fix | Delete
$sources = array( $image['width'] => $source ) + $sources;
[1384] Fix | Delete
} else {
[1385] Fix | Delete
$sources[ $image['width'] ] = $source;
[1386] Fix | Delete
}
[1387] Fix | Delete
}
[1388] Fix | Delete
}
[1389] Fix | Delete
[1390] Fix | Delete
/**
[1391] Fix | Delete
* Filters an image's 'srcset' sources.
[1392] Fix | Delete
*
[1393] Fix | Delete
* @since 4.4.0
[1394] Fix | Delete
*
[1395] Fix | Delete
* @param array $sources {
[1396] Fix | Delete
* One or more arrays of source data to include in the 'srcset'.
[1397] Fix | Delete
*
[1398] Fix | Delete
* @type array $width {
[1399] Fix | Delete
* @type string $url The URL of an image source.
[1400] Fix | Delete
* @type string $descriptor The descriptor type used in the image candidate string,
[1401] Fix | Delete
* either 'w' or 'x'.
[1402] Fix | Delete
* @type int $value The source width if paired with a 'w' descriptor, or a
[1403] Fix | Delete
* pixel density value if paired with an 'x' descriptor.
[1404] Fix | Delete
* }
[1405] Fix | Delete
* }
[1406] Fix | Delete
* @param array $size_array {
[1407] Fix | Delete
* An array of requested width and height values.
[1408] Fix | Delete
*
[1409] Fix | Delete
* @type int $0 The width in pixels.
[1410] Fix | Delete
* @type int $1 The height in pixels.
[1411] Fix | Delete
* }
[1412] Fix | Delete
* @param string $image_src The 'src' of the image.
[1413] Fix | Delete
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
[1414] Fix | Delete
* @param int $attachment_id Image attachment ID or 0.
[1415] Fix | Delete
*/
[1416] Fix | Delete
$sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
[1417] Fix | Delete
[1418] Fix | Delete
// Only return a 'srcset' value if there is more than one source.
[1419] Fix | Delete
if ( ! $src_matched || ! is_array( $sources ) || count( $sources ) < 2 ) {
[1420] Fix | Delete
return false;
[1421] Fix | Delete
}
[1422] Fix | Delete
[1423] Fix | Delete
$srcset = '';
[1424] Fix | Delete
[1425] Fix | Delete
foreach ( $sources as $source ) {
[1426] Fix | Delete
$srcset .= str_replace( ' ', '%20', $source['url'] ) . ' ' . $source['value'] . $source['descriptor'] . ', ';
[1427] Fix | Delete
}
[1428] Fix | Delete
[1429] Fix | Delete
return rtrim( $srcset, ', ' );
[1430] Fix | Delete
}
[1431] Fix | Delete
[1432] Fix | Delete
/**
[1433] Fix | Delete
* Retrieves the value for an image attachment's 'sizes' attribute.
[1434] Fix | Delete
*
[1435] Fix | Delete
* @since 4.4.0
[1436] Fix | Delete
*
[1437] Fix | Delete
* @see wp_calculate_image_sizes()
[1438] Fix | Delete
*
[1439] Fix | Delete
* @param int $attachment_id Image attachment ID.
[1440] Fix | Delete
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
[1441] Fix | Delete
* width and height values in pixels (in that order). Default 'medium'.
[1442] Fix | Delete
* @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
[1443] Fix | Delete
* Default null.
[1444] Fix | Delete
* @return string|false A valid source size value for use in a 'sizes' attribute or false.
[1445] Fix | Delete
*/
[1446] Fix | Delete
function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
[1447] Fix | Delete
$image = wp_get_attachment_image_src( $attachment_id, $size );
[1448] Fix | Delete
[1449] Fix | Delete
if ( ! $image ) {
[1450] Fix | Delete
return false;
[1451] Fix | Delete
}
[1452] Fix | Delete
[1453] Fix | Delete
if ( ! is_array( $image_meta ) ) {
[1454] Fix | Delete
$image_meta = wp_get_attachment_metadata( $attachment_id );
[1455] Fix | Delete
}
[1456] Fix | Delete
[1457] Fix | Delete
$image_src = $image[0];
[1458] Fix | Delete
$size_array = array(
[1459] Fix | Delete
absint( $image[1] ),
[1460] Fix | Delete
absint( $image[2] ),
[1461] Fix | Delete
);
[1462] Fix | Delete
[1463] Fix | Delete
return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
[1464] Fix | Delete
}
[1465] Fix | Delete
[1466] Fix | Delete
/**
[1467] Fix | Delete
* Creates a 'sizes' attribute value for an image.
[1468] Fix | Delete
*
[1469] Fix | Delete
* @since 4.4.0
[1470] Fix | Delete
*
[1471] Fix | Delete
* @param string|int[] $size Image size. Accepts any registered image size name, or an array of
[1472] Fix | Delete
* width and height values in pixels (in that order).
[1473] Fix | Delete
* @param string $image_src Optional. The URL to the image file. Default null.
[1474] Fix | Delete
* @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
[1475] Fix | Delete
* Default null.
[1476] Fix | Delete
* @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id`
[1477] Fix | Delete
* is needed when using the image size name as argument for `$size`. Default 0.
[1478] Fix | Delete
* @return string|false A valid source size value for use in a 'sizes' attribute or false.
[1479] Fix | Delete
*/
[1480] Fix | Delete
function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) {
[1481] Fix | Delete
$width = 0;
[1482] Fix | Delete
[1483] Fix | Delete
if ( is_array( $size ) ) {
[1484] Fix | Delete
$width = absint( $size[0] );
[1485] Fix | Delete
} elseif ( is_string( $size ) ) {
[1486] Fix | Delete
if ( ! $image_meta && $attachment_id ) {
[1487] Fix | Delete
$image_meta = wp_get_attachment_metadata( $attachment_id );
[1488] Fix | Delete
}
[1489] Fix | Delete
[1490] Fix | Delete
if ( is_array( $image_meta ) ) {
[1491] Fix | Delete
$size_array = _wp_get_image_size_from_meta( $size, $image_meta );
[1492] Fix | Delete
if ( $size_array ) {
[1493] Fix | Delete
$width = absint( $size_array[0] );
[1494] Fix | Delete
}
[1495] Fix | Delete
}
[1496] Fix | Delete
}
[1497] Fix | Delete
[1498] Fix | Delete
if ( ! $width ) {
[1499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function