网上有很多以上类似的代码,但都不支持媒体库网格模式显示SVG图片,下面的代码可以实现让WordPress支持上传SVG格式图片:
// 媒体库网格模式显示SVG图片 function zm_display_svg_media($response, $attachment, $meta){ if($response['type'] === 'image' && $response['subtype'] === 'svg+xml' && class_exists('SimpleXMLElement')){ try { $path = get_attached_file($attachment->ID); if(@file_exists($path)){ $svg = new SimpleXMLElement(@file_get_contents($path)); $src = $response['url']; $width = (int) $svg['width']; $height = (int) $svg['height']; $response['image'] = compact( 'src', 'width', 'height' ); $response['thumb'] = compact( 'src', 'width', 'height' ); $response['sizes']['full'] = array( 'height' => $height, 'width' => $width, 'url' => $src, 'orientation' => $height > $width ? 'portrait' : 'landscape', ); } } catch(Exception $e){} } return $response; } add_filter('wp_prepare_attachment_for_js', 'zm_display_svg_media', 10, 3);
另一个相对代码较少的支持媒体库网格模式显示SVG图片代码,不过如果开启调试模式会有错误提示,但不影响使用。
// 媒体库网格模式显示SVG图片 function zm_svg_metadata($data, $post_id) { $data = array( 'sizes' => array( 'large' => array( 'file' => pathinfo(wp_get_attachment_url($post_id), PATHINFO_BASENAME) ) ) ); return $data; } add_filter('wp_get_attachment_metadata', 'zm_svg_metadata', 10, 2);
至于加这个功能用于什么,那要看你用的主题是否有这个功能需要了,直接FTP上传后获取链接也一样在网页中使用。
常见问题FAQ
- 【点击查看】免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 【点击查看】提示下载完但解压或打开不了?
- 【点击查看】开通终身至尊下载源码 “不完整” 或 “不能用” 怎么办?
- 【点击查看】开通终身会员能下载全站资源码?