forked from mustardBees/cmb-field-gallery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmb-field-gallery.php
63 lines (46 loc) · 1.83 KB
/
cmb-field-gallery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/*
Plugin Name: CMB Field Type: Gallery
Plugin URI: https://github.com/mustardBees/cmb-field-gallery
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>.
Version: 2.0.2
Author: Phil Wylie
Author URI: http://www.philwylie.co.uk/
License: GPLv2+
*/
// Useful global constants
define( 'PW_GALLERY_URL', plugin_dir_url( __FILE__ ) );
/**
* Render field
*/
function pw_gallery_field( $field, $meta ) {
wp_enqueue_script( 'pw_gallery_init', PW_GALLERY_URL . 'js/script.js', array( 'jquery' ), null );
if ( ! empty( $meta ) ) {
$meta = implode( ',', $meta );
}
$img_size = $field->args( 'preview_size' ) ? $field->args( 'preview_size' ) : 150;
echo '<div class="pw-gallery">';
echo ' <input type="hidden" id="' . $field->args( 'id' ) . '" name="' . $field->args( 'id' ) . '" value="' . $meta . '" />';
echo ' <input type="button" class="button" value="' . ( $field->args( 'button' ) ? $field->args( 'button' ) : 'Manage gallery' ) . '" style="margin-left: 0;" />';
echo '</div>';
$desc = $field->args( 'desc' );
if ( ! empty( $desc ) ) echo '<p class="cmb2-metabox-description">' . $desc . '</p>';
echo '<ul id="'.$field->args( 'id' ).'-status" class="cmb2-media-status">';
$ids = explode(',',$meta);
foreach($ids as $id) {
echo '<li class="img-status">'. wp_get_attachment_image( $id, $img_size ) .'</li>';
}
echo '</ul>';
}
add_filter( 'cmb2_render_pw_gallery', 'pw_gallery_field', 10, 2 );
/**
* Split CSV string into an array of values
*/
function pw_gallery_field_sanitise( $meta_value, $field ) {
if ( empty( $meta_value ) ) {
$meta_value = '';
} else {
$meta_value = explode( ',', $meta_value );
}
return $meta_value;
}