Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Allows you to override WordPress' auto-generated images
<?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;
?>
@billerickson

This comment has been minimized.

Copy link
Owner Author

billerickson commented Aug 23, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.