I’ve been building a lot of galleries using WordPress’ built-in gallery feature. The biggest issue is that the gallery feature is rather hidden. You have to click the “Upload Image” icon (which itself is hard for people new to WordPress to find), then click the “Gallery” tab.
To make it easier for my clients, I’ve written a simple plugin called Gallery Metabox. This adds a metabox below the post editor that displays all the attached images. It also has a lot of filters in place for a developer to customize it specifically to the project (see the plugin page for more information).
For instance, if you’re adding a custom field to the uploader to specify which images are in a rotator, you can make the metabox only show these images.
I just built a site for a client that has a photo gallery.
I created a page template called “Photos” which replaces the content area with a photo rotator. But if you edit the page, the edit screen was pretty useless. There’s a big empty wysiwyg, and to get to the actual images in the gallery you have to click a small icon, then go to the Gallery tab.
I’ve added the plugin, along with some code that limits the metabox to this page template. I’ve also added the following code to the theme’s functions.php file to remove the post editor on this page template:
<?php | |
/** | |
* Hide Editor | |
* @author Bill Erickson | |
* @link http://www.billerickson.net/code/hide-editor-on-specific-page-template/ | |
*/ | |
function be_hide_editor() { | |
// Get the Post ID | |
$post_id = false; | |
if( isset( $_GET['post'] ) ) | |
$post_id = $_GET['post']; | |
elseif( isset( $_POST['post_ID'] ) ) | |
$post_id = $_POST['post_ID']; | |
$post_id = intval( $post_id ); | |
if( ! $post_id ) | |
return; | |
$current_template = get_page_template_slug( $post_id ); | |
$exclude_on = array( 'group-classes.php', 'trainers.php' ); | |
if( in_array( $current_template, $exclude_on ) ) | |
wp_enqueue_style( 'hide-editor', get_stylesheet_directory_uri() . '/css/hide-editor.css' ); | |
} | |
add_action( 'admin_enqueue_scripts', 'be_hide_editor' ); |
#postdivrich{display: none;} |
So now this is what they see when editing that page:
This is by far a much easier user interface for this specific page template. And even if your page isn’t solely a gallery, I think this is a useful plugin for quickly seeing which images you have attached to the page or displaying in a gallery/rotator.
Jeff says
Thanks for the great plugin. I did have an issue though if there were any metaboxes after the Gallery metabox – any meta data that followed seemed to get cleared. As an alternative, I tried using get_posts instead of WP_Query to gather image info in the be_gallery_metabox_html() function and it seems to fix my issue. Wondering if there are any pitfalls to going the get_posts route.
See my modified function here: https://gist.github.com/2493068
Bill Erickson says
Yep, I’ve actually already made this change in the development version (trunk) which you can download here: http://wordpress.org/extend/plugins/gallery-metabox/download/
I haven’t pushed it out because there’s another reported issue with the jQuery that updates the metabox when you make changes in the media uploader ( http://wordpress.org/support/topic/plugin-gallery-metabox-conflict-with-wpml?replies=4 ). Once we figure out what’s wrong there, I’ll update trunk with that fix and push both out as Version 1.4.
Łukasz says
is it possible to exclude a custom meta box (namely a “custom featured image”) value? I have looked all over and found this when I was looking for it. would be great if this could somehow be implemented.
Bill Erickson says
Yes, you can use remove_meta_box(). See the first example on that page, but only do it for ‘postimagediv’ if you just want the featured image metabox removed.
sam says
hi sir
M installed meatbox plugin for gallery and uploded some images but I dont know code to retrive the gallery imges..pls say…how
Bill Erickson says
Images are attached to the post. So you query any images that are attached to the post: https://www.billerickson.net/code-tag/query/
Dave says
Hey Bill.
i’m wondering if I can wrap each image shown in your plugin with a link tag so I can have each image open up Post Thumbnail Editor. Is there a hook for wrapping each tag with a tag?
Thanks pal.
Dave
Bill Erickson says
Yes you can. Use the `be_gallery_metabox_output` filter. It receives three parameters: $output, $image_url, and $image post object.
I don’t have an example since I haven’t needed to modify it, but the code itself should be a good guide (line 147): https://github.com/billerickson/Gallery-Metabox/blob/master/gallery-metabox.php
dave says
Thanks Bill. VERY helpful. Would that essencially replace your
public function gallery_display( $loop ) with my own? So i’d need to rebuild the loop and write new HTML to output?
Thanks again,
dave
Bill Erickson says
No, that simply replaces the single image’s markup. Here’s an example of what your function should look like: https://gist.github.com/billerickson/5528267
Modify the markup in that to match how you’d like it.
dave says
DARN! I was pretty close to what you had but messed it up a bit. thank you SO much for helping me figure this out. You’re a good man, Bill Erickson!
Paul Davies says
I am using the gallery plugin and loving it, so good and it just works.
I did encounter one issue. This issue being that if i add a thumbnail to the post, the thumbnail seems to get inserted inside of the gallery automatically. Personally i would say to the client “first add your thumbnail, delete it from the gallery box, then start adding your images”.
I was wondering if there is a better way that automatically ignores the thumbnail of the post the editor added. That would be awesome.
Bill Erickson says
By default the gallery shows all images attached to the current post. Removing the image “detaches” it from the post (it removes post_parent postmeta).
In the past what I’ve done is add an additional field to the media uploader that allowed you to mark it as in the gallery, then some code to only show those in the metabox. But with the latest rebuild of the media uploader, that doesn’t work anymore. Now for these instances I use Advanced Custom Fields’ Gallery field option to allow them to upload images specifically to a gallery metabox.
This plugin is really designed to show what images were uploaded to this post.
PAUL DAVIES says
Hi Bill. Still loving the gallery.
Im also wondering how you add a second or third gallery, im thinking that its not possible and i did do some research concerning multiple galleries per post. The thing is, i just love the appearance of the gallery plugin you did, its so simple, you see the images.
Bill Erickson says
I typically use this plugin ( https://github.com/billerickson/Gallery-Rotator ) . It uses the new WordPress gallery system and adds a “display as rotator” checkbox. So you click “Add Media”, then “Add Gallery”, add images to the gallery, and then before inserting check “display as rotator”. This adds the gallery shortcode to the post and displays it as a rotator.