Skip to content

Instantly share code, notes, and snippets.

@cmcculloh
Created November 23, 2011 05:50
Show Gist options
  • Save cmcculloh/1387968 to your computer and use it in GitHub Desktop.
Save cmcculloh/1387968 to your computer and use it in GitHub Desktop.
Full Example
$thumb_sizes = array(
'breaking' => array( 'breaking', 80, 80, true ),
'slider1' => array( 'slider1', 390, 280, true ),
'slider1-wide' => array( 'slider1-wide', 450, 320, true ),
'slider1-thumbnail' => array( 'slider1-thumbnail', 120, 72, true ),
'slider2' => array( 'slider2', 710, 385, true ),
'slider2-wide' => array( 'slider2-wide', 880, 385, true ),
'slider3' => array( 'slider3', 455, 430, true ),
'slider3-wide' => array( 'slider3-wide', 630, 430, true ),
'slider3-small' => array( 'slider3-small', 80, 64, true ),
'latest' => array( 'latest', 200, 120, true ),
'feed' => array( 'feed', 260, 152, true ),
'feed-latest' => array( 'feed-latest', 290, 170, true ),
'feed-vertical' => array( 'feed-vertical', 200, 200, true ),
'single' => array( 'single', 602, 602, false ),
'single-medium' => array( 'single-medium', 300, 300, false ),
'single-small' => array( 'single-small', 180, 180, false ),
'archive' => array( 'archive', 120, 120, true ),
'review' => array( 'review', 300, 300, false )
);
//add thumbnail support
if ( function_exists( 'add_theme_support' ) ) { // Added in 2.9
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 160, 160, true ); // default post thumbnails
foreach($thumb_sizes as $thumb_size){
$name = $thumb_size[0];
$width = get_option('con_'.$name.'_width', $thumb_size[1]);
$height = get_option('con_'.$name.'_height', $thumb_size[2]);
$crop = get_option('con_'.$name.'_crop', $thumb_size[3]);
add_image_size($name, $width, $height, $crop);
}
}
function con_global_callback($field){
global $thumb_sizes;
if($field["part"] == "width"){
$part = 1;
}else if($field["part"] == "height"){
$part = 2;
}else if($field["part"] == "crop"){
$part = 3;
}
$selected = get_option($field["id"], $thumb_sizes[$field['desc']][$part]);
if($field["type"] == "text"){
?>
<input type="text" name="<?php echo $field["id"]; ?>" id="<?php echo $field["id"]; ?>" value="<?php echo $selected ?>">
<?php
}else if($field["type"] == "checkbox"){
if($selected){
$selText = ' checked="checked" ';
}else{
$selText = ' ';
}
?>
<input type="checkbox" name="<?php echo $field["id"]; ?>" id="<?php echo $field["id"]; ?>" <?php echo $selText; ?>>
<?php
}
}
add_action('admin_init', 'con_thumbnails_init');
function con_thumbnails_init(){
global $thumb_sizes;
$parts = array(array("width", "text"), array("height", "text"), array("crop", "checkbox"));
$section = "thumb_settings";
con_register_section($section, 'Custom Thumbnail Settings');
////////////////////////////////////////////////
// THUMB SETTINGS
foreach($thumb_sizes as $thumb_size){
foreach($parts as $part){
$id = 'con_' . $thumb_size[0] . '_' . $part[0];
con_register_field($id, $thumb_size[0], $part[0], $section, $part[1]);
}
}
////////////////////////////////////////////////
}
function con_register_field($id, $desc, $part, $section, $type, $filter = NULL){
$safeCallback = preg_replace("/\-/", "_", $id);
add_settings_field($id,
$desc . " $part",
"con_global_callback",
'media',
'con_setting_' . $section,
array("id" => $id,
"desc" => $desc,
"part" => $part,
"type" => $type)
);
register_setting('con_settings_group', $id, $filter);
}
function con_register_section($section, $title){
add_settings_section('con_setting_' . $section,
$title,
'con_setting_'.$section.'_callback_function',
'media');
}
function con_setting_thumb_settings_callback_function(){
settings_fields("con_settings_group");
echo "<p>Thumbnail sizes custom to continuum</p>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment