WordPress include SVG file error

2019-05-07 07:04发布

I use PHP and WordPress.

On localhost I can include a SVG file without problems, but on the live server. I try to include an SVG file to be able to style it with CSS.

I got this error message

Parse error: syntax error, unexpected T_STRING in
/www/webvol1/34q/h6njx/mysite.com/public_html/wp-content/uploads/2014/02/fish_icon.svg on line 1

I run a file_exists on the file and it returns true but the include does not work. Why not? Why on the localhost and not the live server?

3条回答
小情绪 Triste *
2楼-- · 2019-05-07 07:11

My hosting allowed <? and the include thougt it was a PHP file, not an XML file.

I had to run echo before the XML.

<?php echo '<?xml version="1.0" encoding="utf8"?>'; ?>

It makes WordPress not be able to read this image, it prevents a good image preview. Other clever solutions are still welcome...

查看更多
祖国的老花朵
3楼-- · 2019-05-07 07:12

It works for me - just remove xml header tag from your SVG file completely, as this is not needed by the browser. SVG optimization tools like the web-based SVGOMG tool do this automatically for you.

查看更多
冷血范
4楼-- · 2019-05-07 07:18

You probably sorted this long ago but I've just had the problem and it's to do with apache's short_open_tag setting.

You can modify this on a per site basis by including the following in your .htaccess

 # .htaccess
 <IfModule mod_php5.c>
   php_value short_open_tag 0
 </IfModule>

Or you can fix for all sites by editing your php.ini file and finding the short open tag setting and setting it to off.

# php.ini
short_open_tag: Off
查看更多
登录 后发表回答