-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmb2-radio-image.php
82 lines (67 loc) · 2.69 KB
/
cmb2-radio-image.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/*
Plugin Name: CMB2 Radio Image
Description: https://github.com/satwinderrathore/CMB2-Radio-Image/
Version: 0.1
Author: Satwinder Rathore
Author URI: http://satwinderrathore.wordpress.com
License: GPL-2.0+
*/
// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;
if( !class_exists( 'CMB2_Radio_Image' ) ) {
/**
* Class CMB2_Radio_Image
*/
class CMB2_Radio_Image {
public function __construct() {
add_action( 'cmb2_render_radio_image', array( $this, 'callback' ), 10, 5 );
add_filter( 'cmb2_list_input_attributes', array( $this, 'attributes' ), 10, 4 );
add_action( 'admin_head', array( $this, 'admin_head' ) );
}
public function callback($field, $escaped_value, $object_id, $object_type, $field_type_object) {
echo $field_type_object->radio();
}
public function attributes($args, $defaults, $field, $cmb) {
if ($field->args['type'] == 'radio_image' && isset($field->args['images'])) {
foreach ($field->args['images'] as $field_id => $image) {
if ($field_id == $args['value']) {
$image = trailingslashit($field->args['images_path']) . $image;
$args['label'] = '<img src="' . $image . '" alt="' . $args['value'] . '" title="' . $args['label'] . '" />';
}
}
}
return $args;
}
public function admin_head() {
?>
<style>
.cmb-type-radio-image .cmb2-radio-list {
display: block;
clear: both;
overflow: hidden;
}
.cmb-type-radio-image .cmb2-radio-list input[type="radio"] {
display: none;
}
.cmb-type-radio-image .cmb2-radio-list li {
display: inline-block;
margin-bottom: 0;
}
.cmb-type-radio-image .cmb2-radio-list input[type="radio"] + label {
border: 3px solid #eee;
display: block;
}
.cmb-type-radio-image .cmb2-radio-list input[type="radio"] + label:hover,
.cmb-type-radio-image .cmb2-radio-list input[type="radio"]:checked + label {
border-color: #ccc;
}
.cmb-type-radio-image .cmb2-radio-list li label img {
display: block;
}
</style>
<?php
}
}
$cmb2_radio_image = new CMB2_Radio_Image();
}