Last active
December 17, 2015 19:39
-
-
Save torounit/5662467 to your computer and use it in GitHub Desktop.
WordPressで追加した画像サイズを本文に挿入できるようにする | Simple Colors ( http://www.warna.info/archives/2270/ ) をクラスにして、使い回しが聞くように。
add_image_size()風味で使えるadd_selectable_image_size()という関数も追加。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Class Add_Selectable_Image_Size { | |
public $image_sizes = array(); | |
public function __construct() { | |
add_action( 'after_setup_theme', array(&$this, 'add_image_sizes') ); | |
add_filter( 'image_size_names_choose', array(&$this, 'add_image_size_select') ); | |
} | |
public function add_image_size( $name, $width, $height, $crop, $label, $selectable ) { | |
$this->image_sizes[$name] = array( | |
'name' => $label, // 選択肢のラベル名 | |
'width' => $width, // 最大画像幅 | |
'height' => $height, // 最大画像高さ | |
'crop' => $crop, // 切り抜きを行うかどうか | |
'selectable' => $selectable // 選択肢に含めるかどうか | |
); | |
} | |
public function add_image_sizes() { | |
foreach ( $this->image_sizes as $slug => $size ) { | |
add_image_size( $slug, $size['width'], $size['height'], $size['crop'] ); | |
} | |
} | |
public function add_image_size_select( $size_names ) { | |
$custom_sizes = get_intermediate_image_sizes(); | |
foreach ( $custom_sizes as $custom_size ) { | |
if ( isset( $this->image_sizes[$custom_size]['selectable'] ) && $this->image_sizes[$custom_size]['selectable'] ) { | |
$size_names[$custom_size] = $this->image_sizes[$custom_size]['name']; | |
} | |
} | |
return $size_names; | |
} | |
} | |
$asis = new Add_Selectable_Image_Size(); | |
function add_selectable_image_size( $name, $width = 0, $height = 0, $crop = false, $label = "" ) { | |
global $asis; | |
if( $label == "" ) { | |
$label = $name; | |
} | |
$asis->add_image_size( $name, $width, $height, $crop , $label, true ); | |
}?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//WordPress標準のAPI | |
add_image_size( "size1", 125 , 100 , true ); | |
//WordPressの"画像を追加"から選択できる画像を追加。 | |
add_selectable_image_size( "size2", 200 , 100 , true ); | |
//ラベルも設定化 サイズ3 - 200x200 みたいに表示。未設定なら設定名がラベルに。 | |
add_selectable_image_size( "size3", 200 , 200 , true , "サイズ3" ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment