WordPressのmedia画面で独自のフォーム項目を増やす
スライドショー用の画像URLを取得できるフォームを設置してみた。
ファイルサイズは
http://your.blog.url.com/wp-admin/options-media.php
で設定でき、その中でlarge画像をスライドショー画像としてサイズを指定。
1. フォーム項目の追加
/wp-admin/includes/media.php
function get_attachment_fields_to_edit
の$form_fieldsに新しいフォームの記述を追加。
$form_fields = array(
...
),
// add the form to see medium image file url.
'file_url' => array(
'label' => 'Slideshow Image URL',
'input' => 'html',
'html' => image_file_link_input_fields($post)
),
....
2. 出力したいフォーム項目のHTMLをつくるfunctionを追加
function image_file_link_input_fields($post) {
$file = wp_get_attachment_url($post->ID);
$path = pathinfo($file);
$purl = parse_url($file);
// you can get sizes of thumbnail,medium and large.
$downsize =image_downsize($post->ID, 'large');
$size= sprintf( "%dx%d", $downsize[1], $downsize[2] );
$url = $path['dirname'].'/'.$path['filename'].'-'.$size.'.'.$path['extension'];
return "<input type='text' class='urlfield' name='attachments[$post->ID][file_url]' value='" . attribute_escape($url) . "' />";
}
media.phpはさまざまなファイル形式のアップロードに対応したコードが書かれているため、
読んでて勉強になるね。
