【wordpress4.6.1 svgファイルを許可するためのfunctions.phpカスタマイズ → 効きません!】
svgファイルをwordpressで許可するため。
子テーマのfunctions.php に下記コードを貼り付けてみたのですが、
下記、4種類のコードともNGでした。
(メディアのアップロード時に、
”このファイルタイプは許可されていません。別のファイルをお試しください。”とエラーが表示される)
ちなみに、htaccessファイルの最後に、
AddType image/svg+xml .svg .svgz
の一文を追加して上書きしています。
PHPのコードが全く分からないので、
コードは、参考にさせてもらったサイトなどからのコピーです。
初歩的なことかもしれませんが、
どうぞ、よろしくお願いいたします。
★P.S.★
元々、functions.phpに記述しているコードは以下で、
最後の部分に、下記コードを追加してみました。
現在、コメント入力欄を消すコードなど元々の部分は動いていると思います。
<?php //子テーマで利用する関数を書く add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' ); function theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); } // コメント入力欄、ウェブサイト欄、「メールアドレスが公開されることはありません」の文言を消す add_filter('comment_form_defaults','my_comment_form'); function my_comment_form($args){ $args['comment_notes_before'] = ''; $args['comment_notes_after'] = ''; $args['title_reply'] = 'コメントする'; $args['label_submit'] = 'コメント送信'; return $args; } remove_filter('the_content', 'wpautop'); // 記事の自動整形を無効にする remove_filter('the_excerpt', 'wpautop'); // 抜粋の自動整形を無効にする
★★★ この最後の部分にコード追加 ★★★
↓
参考にしたサイト★1番目
1番目のコード----------------------------------------------------
// SVGファイルに対応する add_filter( 'upload_mimes', 'my_add_mime_type' ); if( !function_exists('my_add_mime_type') ) { function my_add_mime_type($mime_types) { $mime_types['svg'] = 'image/svg+xml'; $mime_types['svgz'] = 'image/svg+xml'; return $mime_types; } } add_filter('ext2type', 'my_ext2type'); if( !function_exists('my_ext2type') ) { function my_ext2type($ext2types) { array_push($ext2types['image'], 'svg', 'svgz'); return $ext2types; } } add_filter('wp_generate_attachment_metadata', 'my_wp_generate_attachment_metadata',1,2); if( !function_exists('my_wp_generate_attachment_metadata') ) { function my_wp_generate_attachment_metadata($metadata, $attachment_id) { $attachment_post = $post = get_post($attachment_id); $type = $attachment_post->post_mime_type; if ($type === 'image/svg+xml' && empty($metadata)) { $upload_dir = wp_upload_dir(); $base_name = basename($attachment_post->guid); $metadata = array( 'file' => $upload_dir['subdir'] . '/' . $base_name ); } return $metadata; } }
[参考にしたサイト★2番目]
2番目のコード----------------------------------------------------
function my_ext2type($ext2types) { array_push($ext2types, array('image' => array('svg', 'svgz'))); return $ext2types; } add_filter('ext2type', 'my_ext2type'); function my_mime_types($mimes){ $mimes['svg'] = 'image/svg+xml'; $mimes['svgz'] = 'image/svg+xml'; return $mimes; } add_filter('upload_mimes', 'my_mime_types'); function my_mime_to_ext($mime_to_ext) { $mime_to_ext['image/svg+xml'] = 'svg'; return $mime_to_ext; } add_filter('getimagesize_mimes_to_exts', 'my_mime_to_ext');
[参考にしたサイト★3番目]
3番目のコード----------------------------------------------------
function fix_svg_thumb_display() { echo ' td.media-icon img[src$=".svg"], img[src$=".svg"].attachment-post-thumbnail, #set-post-thumbnail img[src$=".svg"]{ width: 100% !important; height: auto !important; }'; } add_action('admin_head', 'fix_svg_thumb_display');
[参考にしたサイト★4番目]
4番目のコード----------------------------------------------------
function add_file_types_to_uploads($file_types){ $new_filetypes = array(); $new_filetypes['svg'] = 'image/svg+xml'; $file_types = array_merge($file_types, $new_filetypes ); return $file_types; } add_action('upload_mimes', 'add_file_types_to_uploads');