Skip to content

Instantly share code, notes, and snippets.

@hlashbrooke
Last active May 26, 2020 01:03
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hlashbrooke/133a991f9cce417a5bfb to your computer and use it in GitHub Desktop.
Save hlashbrooke/133a991f9cce417a5bfb to your computer and use it in GitHub Desktop.
WordPress: Create a custom metabox that works in exactly the same way as the existing 'Featured Image' box on the post edit screen. In this case the image is called 'Listing Image' (with all the function and variable names correlating to that), but you can change the strings to whatever you need them to be.
<?php
add_action( 'add_meta_boxes', 'listing_image_add_metabox' );
function listing_image_add_metabox () {
add_meta_box( 'listingimagediv', __( 'Listing Image', 'text-domain' ), 'listing_image_metabox', 'post', 'side', 'low');
}
function listing_image_metabox ( $post ) {
global $content_width, $_wp_additional_image_sizes;
$image_id = get_post_meta( $post->ID, '_listing_image_id', true );
$old_content_width = $content_width;
$content_width = 254;
if ( $image_id && get_post( $image_id ) ) {
if ( ! isset( $_wp_additional_image_sizes['post-thumbnail'] ) ) {
$thumbnail_html = wp_get_attachment_image( $image_id, array( $content_width, $content_width ) );
} else {
$thumbnail_html = wp_get_attachment_image( $image_id, 'post-thumbnail' );
}
if ( ! empty( $thumbnail_html ) ) {
$content = $thumbnail_html;
$content .= '<p class="hide-if-no-js"><a href="javascript:;" id="remove_listing_image_button" >' . esc_html__( 'Remove listing image', 'text-domain' ) . '</a></p>';
$content .= '<input type="hidden" id="upload_listing_image" name="_listing_cover_image" value="' . esc_attr( $image_id ) . '" />';
}
$content_width = $old_content_width;
} else {
$content = '<img src="" style="width:' . esc_attr( $content_width ) . 'px;height:auto;border:0;display:none;" />';
$content .= '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set listing image', 'text-domain' ) . '" href="javascript:;" id="upload_listing_image_button" id="set-listing-image" data-uploader_title="' . esc_attr__( 'Choose an image', 'text-domain' ) . '" data-uploader_button_text="' . esc_attr__( 'Set listing image', 'text-domain' ) . '">' . esc_html__( 'Set listing image', 'text-domain' ) . '</a></p>';
$content .= '<input type="hidden" id="upload_listing_image" name="_listing_cover_image" value="" />';
}
echo $content;
}
add_action( 'save_post', 'listing_image_save', 10, 1 );
function listing_image_save ( $post_id ) {
if( isset( $_POST['_listing_cover_image'] ) ) {
$image_id = (int) $_POST['_listing_cover_image'];
update_post_meta( $post_id, '_listing_image_id', $image_id );
}
}
jQuery(document).ready(function($) {
// Uploading files
var file_frame;
jQuery.fn.upload_listing_image = function( button ) {
var button_id = button.attr('id');
var field_id = button_id.replace( '_button', '' );
// If the media frame already exists, reopen it.
if ( file_frame ) {
file_frame.open();
return;
}
// Create the media frame.
file_frame = wp.media.frames.file_frame = wp.media({
title: jQuery( this ).data( 'uploader_title' ),
button: {
text: jQuery( this ).data( 'uploader_button_text' ),
},
multiple: false
});
// When an image is selected, run a callback.
file_frame.on( 'select', function() {
var attachment = file_frame.state().get('selection').first().toJSON();
jQuery("#"+field_id).val(attachment.id);
jQuery("#listingimagediv img").attr('src',attachment.url);
jQuery( '#listingimagediv img' ).show();
jQuery( '#' + button_id ).attr( 'id', 'remove_listing_image_button' );
jQuery( '#remove_listing_image_button' ).text( 'Remove listing image' );
});
// Finally, open the modal
file_frame.open();
};
jQuery('#listingimagediv').on( 'click', '#upload_listing_image_button', function( event ) {
event.preventDefault();
jQuery.fn.upload_listing_image( jQuery(this) );
});
jQuery('#listingimagediv').on( 'click', '#remove_listing_image_button', function( event ) {
event.preventDefault();
jQuery( '#upload_listing_image' ).val( '' );
jQuery( '#listingimagediv img' ).attr( 'src', '' );
jQuery( '#listingimagediv img' ).hide();
jQuery( this ).attr( 'id', 'upload_listing_image_button' );
jQuery( '#upload_listing_image_button' ).text( 'Set listing image' );
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment