Skip to content

Commit 87c07ca

Browse files
committed
Initial commit
0 parents  commit 87c07ca

6 files changed

+121
-0
lines changed

.gitignore

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# OS or IDE folders/files to ignore
2+
*~
3+
._*
4+
*.lock
5+
*.DS_Store
6+
*.swp
7+
*.out
8+
.cache
9+
.project
10+
.settings
11+
nbproject
12+
thumb.db
13+
Thumbs.db
14+
15+
16+
# Project specific files to ignore
17+
*.class
18+
*.pyc
19+
*.pyo

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# CMB Field Type: Gallery
2+
3+
## Description
4+
5+
Gallery field type for [Custom Metaboxes and Fields for WordPress](https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress).
6+
7+
## Usage
8+
9+
`pw_gallery` - Save a list of attachment IDs. Example:
10+
11+
```php
12+
array(
13+
'name' => 'Gallery Images',
14+
'desc' => 'Upload and manage gallery images',
15+
'button' => 'Manage gallery', // Optionally set button label
16+
'id' => $prefix . 'gallery_images',
17+
'type' => 'pw_gallery',
18+
),
19+
```
20+
21+
## Screenshot
22+
23+
![Image](screenshot-1.png?raw=true)
24+
25+
![Image](screenshot-2.png?raw=true)
26+
27+
## To-do
28+
* Add a clear gallery button
29+
30+
## Example front-end output
31+
32+
```php
33+
<?php $gallery_images = get_post_meta( get_the_ID(), '_cmb_gallery_images', true ); ?>
34+
<?php if ( ! empty( $gallery_images ) ) : ?>
35+
<ul>
36+
<?php foreach ( $gallery_images as $gallery_image ) : ?>
37+
<li><?php echo wp_get_attachment_image( $gallery_image, 'thumbnail' ); ?></li>
38+
<?php endforeach; ?>
39+
</ul>
40+
<?php endif; ?>
41+
```

cmb-field-gallery.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/*
3+
Plugin Name: CMB Field Type: Gallery
4+
Plugin URI: https://github.com/mustardBees/cmb-field-gallery
5+
Description: Gallery field type for Custom Metaboxes and Fields for WordPress. Thanks to <a href="http://www.purewebsolutions.nl/">Roel Obdam</a> for the hard work <a href="http://goo.gl/RYj2w">figuring out the media library</a>.
6+
Version: 1.0
7+
Author: Phil Wylie
8+
Author URI: http://www.philwylie.co.uk/
9+
License: GPLv2+
10+
*/
11+
12+
// Useful global constants
13+
define( 'PW_GALLERY_URL', plugin_dir_url( __FILE__ ) );
14+
15+
/**
16+
* Render field
17+
*/
18+
function pw_gallery_field( $field, $meta ) {
19+
wp_enqueue_script( 'pw_gallery_init', PW_GALLERY_URL . 'js/script.js', array( 'jquery' ), null );
20+
21+
if ( ! empty( $meta ) ) {
22+
$meta = implode( ',', $meta );
23+
}
24+
25+
echo '<div class="pw-gallery">';
26+
echo ' <input type="hidden" id="' . $field['id'] . '" name="' . $field['id'] . '" value="' . $meta . '" />';
27+
echo ' <input type="button" class="button" value="' . ( ! empty( $field['button'] ) ? $field['button'] : 'Manage gallery' ) . '" />';
28+
echo '</div>';
29+
30+
if ( ! empty( $field['desc'] ) ) echo '<p class="cmb_metabox_description">' . $field['desc'] . '</p>';
31+
}
32+
add_filter( 'cmb_render_pw_gallery', 'pw_gallery_field', 10, 2 );
33+
34+
35+
/**
36+
* Split CSV string into an array of values
37+
*/
38+
function pw_gallery_field_validation( $new ) {
39+
if ( empty( $new ) ) {
40+
$new = '';
41+
} else {
42+
$new = explode( ',', $new );
43+
}
44+
45+
return $new;
46+
}
47+
add_filter( 'cmb_validate_pw_gallery', 'pw_gallery_field_validation', 10, 3 );

js/script.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(function ($) {
2+
$('.pw-gallery').each(function() {
3+
var instance = this;
4+
5+
$('input[type=button]', instance).click(function() {
6+
var gallerysc = '[gallery ids="' + $('input[type=hidden]', instance).val() + '"]';
7+
wp.media.gallery.edit(gallerysc).on('update', function(g) {
8+
var id_array = [];
9+
$.each(g.models, function(id, img) { id_array.push(img.id); });
10+
$('input[type=hidden]', instance).val(id_array.join(","));
11+
});
12+
});
13+
});
14+
}(jQuery));

screenshot-1.png

4.36 KB
Loading

screenshot-2.png

62.7 KB
Loading

0 commit comments

Comments
 (0)