Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created November 16, 2012 20:30
Show Gist options
  • Save trepmal/4090637 to your computer and use it in GitHub Desktop.
Save trepmal/4090637 to your computer and use it in GitHub Desktop.
[WordPress] Do multiple featured-image-esque meta boxes
<?php
/*
Plugin Name: Multiple Featured Images
Description: NOT production ready. May cause tears.
Version: 2012.12.13
Author: Kailey Lampert
Author URI: kaileylampert.com
*/
new MFI_Meta_Box( 'derp', 'Derp Image', '200', '400', 'post' );
new MFI_Meta_Box( 'herp', 'Herp Image', '600', '150', 'page' );
new MFI_Meta_Box( 'berp', 'Berp Image', '300', '300', 'post' );
class MFI_Meta_Box {
//size_name should be alphanumeric withOUT dashes. underscores okay
function __construct( $size_name, $size_title, $size_width, $size_height, $post_type ) {
if ( empty( $size_name ) || empty( $size_width ) || empty( $size_height ) || empty( $post_type ) )
return false;
$this->size_name = $size_name;
$this->size_title = $size_title;
$this->size_width = $size_width;
$this->size_height = $size_height;
$this->post_type = $post_type;
$this->meta_key = "_{$this->size_name}_thumbnail_id";
add_image_size( $this->size_name, $this->size_width, $this->size_height, true );
add_filter( 'MFI_Sizes', array( &$this, 'MFI_Sizes' ) );
add_action( 'add_meta_boxes', array( &$this, 'add_meta_boxes' ) );
add_action( 'save_post', array( &$this, 'save_post' ), 10, 2 );
add_action( 'wp_ajax_set_post_thumbnail_'.$this->size_name, array( &$this, 'set_post_thumbnail' ) );
add_action( 'admin_footer-post.php', array( &$this, 'admin_footer' ) );
add_action( 'admin_footer-post-new.php', array( &$this, 'admin_footer' ) );
// apply_filters( "postbox_classes_{$page}_{$id}", $classes );
add_filter( "postbox_classes_{$this->post_type}_mfi_postimagediv-{$this->size_name}", array( &$this, 'postbox_class' ) );
}
function MFI_Sizes( $sizes ) {
$sizes[] = $this->size_name;
return $sizes;
}
function postbox_class( $classes ) {
$classes[] = 'mfi-feat-image';
return $classes;
}
function add_meta_boxes() {
add_meta_box( 'mfi_postimagediv-'. $this->size_name, __( $this->size_title ), array( &$this, 'mfi_post_thumbnail_meta_box' ), $this->post_type, 'side', 'low');
}
function mfi_post_thumbnail_meta_box( $post ) {
$thumbnail_id = get_post_meta( $post->ID, $this->meta_key, true );
echo $this->_wp_post_thumbnail_html_2( $thumbnail_id, $post->ID );
}
//hijacked from wp-admin/includes/post.php
function _wp_post_thumbnail_html_2( $thumbnail_id = null, $post = null ) {
global $content_width, $_wp_additional_image_sizes;
$post = get_post( $post );
$upload_iframe_src = esc_url( get_upload_iframe_src('image', $post->ID ) );
$set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set '.$this->size_title ) . '" href="%s" id="set-'. $this->size_name .'" class="thickbox">%s</a></p>';
$content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set '.$this->size_title ) );
if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
$old_content_width = $content_width;
$content_width = 266;
if ( !isset( $_wp_additional_image_sizes[ $this->size_name ] ) )
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
else
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, $this->size_name );
if ( !empty( $thumbnail_html ) ) {
$ajax_nonce = wp_create_nonce( 'set_'.$this->size_name.'-' . $post->ID );
$content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
$content .= '<p class="hide-if-no-js"><a href="#" id="remove-'.$this->size_name.'" onclick="WPRemoveThumbnail_2(\'' . $ajax_nonce . '\', \''.$this->size_name .'\');return false;">' . esc_html__( 'Remove '.$this->size_title ) . '</a></p>';
}
$content_width = $old_content_width;
}
return $content;
}
function save_post( $post_id, $post ) {
if ( empty( $_POST ) ) return;
if ( ! isset( $_POST[ 'nn-save-mfi-'.$this->size_name ] ) )
return $post_id;
if ( ! wp_verify_nonce( $_POST[ 'nn-save-mfi-'.$this->size_name ], 'na-save-mfi-'.$this->size_name ) ) //verify intent
return $post_id;
if ( ! isset( $_POST[ $this->meta_key ] ) )
return $post_id;
// die( "$post_id, {$this->meta_key}, {$_POST[ $this->meta_key ]}" );
update_post_meta( $post_id, $this->meta_key, intval( $_POST[ $this->meta_key ] ) );
}
//hijacked from wp-admin/ajax-actions.php
function set_post_thumbnail() {
$json = ! empty( $_REQUEST['json'] ); // New-style request
$post_ID = intval( $_POST['post_id'] );
if ( ! current_user_can( 'edit_post', $post_ID ) )
wp_die( -1 );
$thumbnail_id = intval( $_POST['thumbnail_id'] );
if ( $json )
check_ajax_referer( "update-post_$post_ID" );
else
check_ajax_referer( "set_{$this->size_name}-$post_ID" );
if ( $thumbnail_id == '-1' ) {
if ( delete_post_meta( $post_ID, $this->meta_key ) ) {
$return = $this->_wp_post_thumbnail_html_2( null, $post_ID );
$json ? wp_send_json_success( $return ) : wp_die( $return );
} else {
wp_die( 0 );
}
}
if ( update_post_meta( $post_ID, $this->meta_key, $thumbnail_id ) ) {
$return = $this->_wp_post_thumbnail_html_2( $thumbnail_id, $post_ID );
$json ? wp_send_json_success( $return ) : wp_die( $return );
}
wp_die( 0 );
}
function admin_footer() {
if ( $this->post_type != get_post_type() ) return;
//this will probably make you cry
echo '<script>';
$file = file_get_contents( plugin_dir_path( __FILE__) .'multi-feat.js' );
$file = str_replace( 'multi_feat.size', "'{$this->size_name}'", $file );
$file = str_replace( 'secondaryFeaturedImage', "{$this->size_name}FeaturedImage", $file );
echo $file;
echo '</script>';
}
}
new MFI_Meta_Box_once();
class MFI_Meta_Box_once {
function __construct() {
$this->sizes = apply_filters( 'MFI_Sizes', array() );
if ( empty( $this->sizes ) ) return;
add_action( 'admin_enqueue_scripts', array( &$this, 'admin_scripts' ) );
add_action( 'admin_footer-post.php', array( &$this, 'admin_footer' ) );
add_action( 'admin_footer-post-new.php', array( &$this, 'admin_footer' ) );
}
function admin_scripts( $hook ) {
if ( $hook != 'post.php' && $hook != 'post-new.php' ) return;
?><style>
.mfi-feat-image img { max-width: 100%; height: auto;}
</style><?php
}
function admin_footer() {
?><script>
(function($){
WPRemoveThumbnail_2 = function(nonce, size_name){
$.post(ajaxurl, {
action:"set_post_thumbnail_"+size_name, post_id: $('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
}, function(str){
if ( str == '0' ) {
alert( setPostThumbnailL10n.error );
} else {
$('.inside', '#mfi_postimagediv-'+size_name).html(str);
}
}
);
};
}(jQuery));
</script><?php
}
}
(function($){
wp.media.secondaryFeaturedImage = {
get: function() {
return wp.media.view.settings.post.featuredImageId;
},
set: function( id ) {
var settings = wp.media.view.settings;
settings.post.featuredImageId = id;
wp.media.post( 'set_post_thumbnail_'+multi_feat.size, {
json: true,
post_id: settings.post.id,
thumbnail_id: settings.post.featuredImageId,
_wpnonce: settings.post.nonce
}).done( function( html ) {
$( '.inside', '#mfi_postimagediv-'+multi_feat.size ).html( html );
});
},
frame: function() {
if ( this._frame )
return this._frame;
this._frame = wp.media({
state: 'featured-image',
states: [ new wp.media.controller.FeaturedImage() ]
});
this._frame.on( 'toolbar:create:featured-image', function( toolbar ) {
this.createSelectToolbar( toolbar, {
text: wp.media.view.l10n.setFeaturedImage
});
}, this._frame );
this._frame.state('featured-image').on( 'select', this.select );
return this._frame;
},
select: function() {
var settings = wp.media.view.settings,
selection = this.get('selection').single();
if ( ! settings.post.featuredImageId )
return;
wp.media.secondaryFeaturedImage.set( selection ? selection.id : -1 );
},
init: function() {
// Open the content media manager to the 'featured image' tab when
// the post thumbnail is clicked.
$('#mfi_postimagediv-'+multi_feat.size).on( 'click', '#set-'+multi_feat.size, function( event ) {
event.preventDefault();
// Stop propagation to prevent thickbox from activating.
event.stopPropagation();
wp.media.secondaryFeaturedImage.frame().open();
// Update the featured image id when the 'remove' link is clicked.
}).on( 'click', '#remove-'+multi_feat.size, function() {
wp.media.view.settings.post.featuredImageId = -1;
});
}
};
// alert( '#mfi_postimagediv-'+multi_feat.size );
$( wp.media.secondaryFeaturedImage.init );
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment