WordPress: Restrict direct access to files – Prevent hotlinking
A useful bit of code to prevent files from being accessed (or...
17.Aug.2021 | Code Snippets, Wordpress
Add this code to your functions.php file to add the dimensions to the image object array for svgs.
add_filter( 'wp_get_attachment_image_src', 'fix_wp_get_attachment_image_svg', 10, 4 );
function fix_wp_get_attachment_image_svg($image, $attachment_id, $size, $icon) {
if (is_array($image) && preg_match('/\.svg$/i', $image[0]) && $image[1] <= 1) { if(is_array($size)) { $image[1] = $size[0]; $image[2] = $size[1]; } elseif(($xml = simplexml_load_file($image[0])) !== false) { $attr = $xml->attributes();
$viewbox = explode(' ', $attr->viewBox);
$image[1] = isset($attr->width) && preg_match('/\d+/', $attr->width, $value) ? (int) $value[0] : (count($viewbox) == 4 ? (int) $viewbox[2] : null);
$image[2] = isset($attr->height) && preg_match('/\d+/', $attr->height, $value) ? (int) $value[0] : (count($viewbox) == 4 ? (int) $viewbox[3] : null);
} else {
$image[1] = $image[2] = null;
}
}
return $image;
}
ACF, Coding Tips, Wordpress, WordPress Tips
Share
A useful bit of code to prevent files from being accessed (or...
Get the featured image dimensions (width and height) in WordPress. Paste the...
In the early days (2003), WordPress was essentially a blogging tool and...
Need a bespoke WooCommerce site with dynamic content for two types of visitor? For example, this is from a recent...
For me, the ACF plugin is a standard install for every WordPress project. Its a great tool for developers to...
Handy bit of code you can use in your custom templates that grabs the all the page or post data by ID:...