Created
August 23, 2011 16:09
-
-
Save billerickson/1165671 to your computer and use it in GitHub Desktop.
Allows you to override WordPress' auto-generated images
This file contains 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 | |
/* | |
Plugin Name: Image Override | |
Plugin URI: http://www.billerickson.net | |
Description: Allows you to override image sizes | |
Version: 1.0 | |
Author: Bill Erickson | |
Author URI: http://www.billerickson.net | |
License: GPLv2 | |
*/ | |
class BE_Image_Override { | |
var $instance; | |
public function __construct() { | |
$this->instance =& $this; | |
add_action( 'init', array( $this, 'init' ) ); | |
} | |
public function init() { | |
add_filter( 'cmb_meta_boxes', array( $this, 'create_metaboxes' ) ); | |
add_action( 'init', array( $this, 'initialize_cmb_meta_boxes' ), 50 ); | |
add_action( 'image_override_display', array( $this, 'display' ), 10, 1 ); | |
add_action( 'return_image_override_display', array( $this, 'return_display' ), 10, 2 ); | |
} | |
public function create_metaboxes( $meta_boxes ) { | |
$post_types = apply_filters( 'image-override-post-types', array_keys( get_post_types( array('show_ui' => true ) ) ) ); | |
$sizes = apply_filters( 'image-override-sizes', array( 'thumbnail', 'medium', 'large' ) ); | |
if ( !empty( $sizes ) ) { | |
$prefix = 'image_override_'; | |
$fields = array(); | |
foreach( $sizes as $size ) { | |
$fields[] = array( | |
'name' => ucwords( $size ), | |
'desc' => '', | |
'id' => $prefix.$size, | |
'type' => 'file' | |
); | |
} | |
$meta_boxes[] = array( | |
'id' => 'image-override', | |
'title' => 'Image Override', | |
'pages' => $post_types, | |
'context' => 'normal', | |
'priority' => 'low', | |
'show_names' => true, | |
'fields' => $fields | |
); | |
} | |
return $meta_boxes; | |
} | |
public function initialize_cmb_meta_boxes() { | |
$sizes = apply_filters( 'image-override-sizes', array( 'thumbnail', 'medium', 'large' ) ); | |
if ( !class_exists('cmb_Meta_Box') && !empty( $sizes ) ) { | |
require_once( dirname( __FILE__) . '/Custom-Metaboxes-and-Fields-for-WordPress/init.php' ); | |
} | |
} | |
public function display( $size = '' ) { | |
global $post; | |
echo $this->return_display( $post->ID, $size ); | |
} | |
public function return_display( $id = '', $size = '' ) { | |
$override = get_post_meta( $id, 'image_override_'.$size, true ); | |
if ( !empty( $override ) ) return '<img src="' . esc_url( $override ) . '" class="attachment-'. esc_attr( $size ) . '" alt="' . get_the_title($id) . '" title="' . get_the_title($id) .'" />'; | |
else return get_the_post_thumbnail( $id, $size ); | |
} | |
} | |
new BE_Image_Override; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses this custom metabox code: https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress