Преглед изворни кода

feat: building-on cmb fields | feat: working custom fields for hero videos

tags/0.9.0
J пре 6 година
родитељ
комит
5f1a4dfd01
2 измењених фајлова са 67 додато и 3 уклоњено
  1. 63
    0
      plugins/cia-post-types/cia-post-types.php
  2. 4
    3
      vue-theme/src/components/gallery.vue

+ 63
- 0
plugins/cia-post-types/cia-post-types.php Прегледај датотеку

@@ -16,6 +16,8 @@
16 16
 // If this file is called directly, abort.
17 17
 if ( ! defined( 'WPINC' ) ) { die; }
18 18
 
19
+require_once 'includes/cmb2/init.php';
20
+
19 21
 include('includes/custom-types.php');
20 22
 include('includes/p2p-mappings.php');
21 23
 
@@ -64,4 +66,65 @@ $custom_types = get_all_custom_types();
64 66
 foreach ($custom_types as $type):
65 67
     $icon = get_icon($type);
66 68
     add_action( 'init', [ new PostType($type, $icon), 'register_post_type' ] );
69
+
70
+    // Register custom meta-boxes
71
+    // register_custom_meta_box( 'hero_box' 'Custom Hero Section', 'hero', $type );
67 72
 endforeach;
73
+
74
+add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
75
+
76
+function sanitize_hero_urls( $value, $field_args, $field ) {
77
+    $encoded = wp_json_encode( array( 'url' => $value ), true );
78
+    return $encoded;
79
+}
80
+
81
+function cmb_hero_render_row_cb( $field_args, $field ) {
82
+    $id          = $field->args( 'id' );
83
+    $label       = $field->args( 'name' );
84
+    $name        = $field->args( '_name' );
85
+    $value       = $field->escaped_value();
86
+    $description = $field->args( 'description' );
87
+    // !: Impossible to fine " conversion
88
+    $decoded     = json_decode( str_replace('"', '"', $value), true );
89
+    ?>
90
+    <div class="custom-field-row">
91
+        <p>
92
+            <label for="<?php echo $id; ?>"><?php echo $label; ?></label>
93
+            <input style="width: 100%;" id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>" 
94
+                value="<?php echo $decoded['url']; ?>"/>
95
+        </p>
96
+        <p class="description"><?php echo $description; ?></p>
97
+    </div>
98
+    <?php
99
+}
100
+
101
+/**
102
+ * Define the metabox and field configurations.
103
+ */
104
+function cmb2_sample_metaboxes() {
105
+
106
+    /**
107
+     * Initiate the metabox
108
+     */
109
+    $cmb = new_cmb2_box( array(
110
+        'id'            => 'hero_metabox',
111
+        'title'         => __( 'Hero', 'cmb2' ),
112
+        'object_types'  => array( 'artist', ), // Post type
113
+        'context'       => 'normal',
114
+        'priority'      => 'high',
115
+        'show_names'    => true, // Show field names on the left
116
+        'show_in_rest'  => WP_REST_Server::READABLE
117
+        // 'cmb_styles' => false, // false to disable the CMB stylesheet
118
+        // 'closed'     => true, // Keep the metabox closed by default
119
+    ) );
120
+
121
+    // URL text field
122
+    $cmb->add_field( array(
123
+        'name'             => __( 'YouTube URL', 'cmb2' ),
124
+        'desc'             => __( 'Video for the hero section to display', 'cmb2' ),
125
+        'id'               => 'hero_header',
126
+        'type'             => 'text',
127
+        'sanitization_cb'  => 'sanitize_hero_urls',
128
+        'render_row_cb' => 'cmb_hero_render_row_cb'
129
+    ) );
130
+}

+ 4
- 3
vue-theme/src/components/gallery.vue Прегледај датотеку

@@ -47,14 +47,15 @@ export default {
47 47
         }
48 48
     },
49 49
     watch: {
50
-        fullscreengallery(newVal, oldVal) { 
50
+        fullscreengallery(newVal, oldVal) {
51
+
51 52
             this.selected = newVal
52 53
         }
53 54
     },
54 55
     mounted() {
55 56
         // Set the first selection
56 57
         this.selected = this.fullscreengallery
57
-        
58
+
58 59
         window.addEventListener('keydown', this.interpretKeypress)
59 60
     },
60 61
     destroyed() {
@@ -66,7 +67,7 @@ export default {
66 67
 <style lang="postcss">
67 68
 .gallery
68 69
     position: fixed
69
-    top: 0
70
+    top: 0q
70 71
     left: 0
71 72
     width: 100%
72 73
     height: 100%

Loading…
Откажи
Сачувај