-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippet.php
executable file
·319 lines (260 loc) · 7.81 KB
/
snippet.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/*
Plugin Name: Snippets
Plugin URI: https://github.com/wp-pure/snippets
Description: Snippets of reusable html that can easily be included in pages, posts, or even templates using shortcodes.
Version: 0.0.4
Author: James Pederson
Author URI: https://jpederson.com
License: GPL2
*/
// snippet custom post type function
function snippet_post_type() {
// register our custom post type
register_post_type( 'snippet',
// custom post type information
array(
'labels' => array(
'name' => __( 'Snippets', 'ptheme' ), /* This is the Title of the Group */
'singular_name' => __( 'Snippet', 'ptheme' ), /* This is the individual type */
'all_items' => __( 'All Snippets', 'ptheme' ), /* the all items menu item */
'add_new' => __( 'Add New', 'ptheme' ), /* The add new menu item */
'add_new_item' => __( 'Add New Snippet', 'ptheme' ), /* Add New Display Title */
'edit' => __( 'Edit', 'ptheme' ), /* Edit Dialog */
'edit_item' => __( 'Edit Snippets', 'ptheme' ), /* Edit Display Title */
'new_item' => __( 'New Snippet', 'ptheme' ), /* New Display Title */
'view_item' => __( 'View Snippet', 'ptheme' ), /* View Display Title */
'search_items' => __( 'Search Snippets', 'ptheme' ), /* Search Custom Type Title */
'not_found' => __( 'Nothing found in the database.', 'ptheme' ), /* This displays if there are no entries yet */
'not_found_in_trash' => __( 'Nothing found in Trash', 'ptheme' ), /* This displays if there is nothing in the trash */
'parent_item_colon' => ''
),
'description' => __( 'Manage Snippets.', 'ptheme' ), /* Custom Type Description */
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => true,
'show_ui' => true,
'query_var' => true,
'menu_position' => 25, /* this is what order you want it to appear in on the left hand side menu */
'menu_icon' => 'dashicons-media-code', /* the icon for the custom post type menu */
'has_archive' => false, /* you can rename the slug here */
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array( 'title', 'revisions' )
)
);
}
add_action( 'init', 'snippet_post_type');
// function to handle the snippet shortcode
function snippet_shortcode( $atts ) {
// if we have a slug specified
if ( isset( $atts['slug'] ) ) {
// let's set the args for our select
$args = array(
'name' => $atts['slug'],
'post_type' => 'snippet',
'numberposts' => 1
);
// get the snippet
$snippets = get_posts( $args );
// check to make sure it's not empty
if ( is_object( $snippets[0] ) ) {
// get the snippet code
return get_field( 'content', $snippets[0]->ID );
} else {
// return nothing if the snippet doesn't exist
return '';
}
}
}
add_shortcode( 'snippet', 'snippet_shortcode' );
// function to handle the snippet shortcode
function get_snippet( $slug, $filter_content = true ) {
// if we have a slug specified
if ( isset( $slug ) ) {
// let's set the args for our select
$args = array(
'name' => $slug,
'post_type' => 'snippet',
'numberposts' => 1
);
// get the snippet
$snippets = get_posts( $args );
// check to make sure it's not empty
if ( isset( $snippets[0] ) ) {
// get the snippet code
return get_field( 'content', $snippets[0]->ID );
} else {
// return nothing if the snippet doesn't exist
return '';
}
}
}
// custom admin listing column to output the shortcode for each snipper
function snippet_columns( $columns ) {
// don't include the data column (it's really not necessary)
unset($columns['date']);
// add a shortcode column
$columns['snippet_shortcode'] = 'Shortcode';
// return the columns list
return $columns;
}
add_filter( 'manage_snippet_posts_columns', 'snippet_columns' );
// populate the new column with the shortcode so users can easily copy it
function snippet_columns_content( $column, $post_id ) {
// only affect the appropriate column
if ( $column === 'snippet_shortcode' ) {
// get the slug
$slug = get_post_field( 'post_name', $post_id );
// output the shortcode for easy copying
echo '[snippet slug="' . $slug . '"]';
}
}
add_action( 'manage_posts_custom_column' , 'snippet_columns_content', 10, 2 );
// remove data filtering from snippets admin listing
function snippet_remove_date_filter( $months ) {
// get post type
global $typenow;
// only remove this for snippet post type
if ( $typenow == 'snippet' ) {
return array();
}
// otherwise keep date filter
return $months;
}
add_filter( 'months_dropdown_results', 'snippet_remove_date_filter' );
// sort snippets by title in the dashboard listing.
function snippet_order( $query ) {
// get post type
global $typenow;
// only remove this for snippet post type
if ( $typenow == 'snippet' ) {
$query->set( 'order' , 'asc' );
$query->set( 'orderby', 'title');
return;
}
}
add_action( 'pre_get_posts', 'snippet_order', 1 );
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_673a7168dcc79',
'title' => 'Snippet Fields',
'fields' => array(
array(
'key' => 'field_673a7168fa84c',
'label' => 'Mode',
'name' => 'mode',
'aria-label' => '',
'type' => 'radio',
'instructions' => 'Choose the mode for this snippet. \'Code\' style snippets preserve all the special formatting and characters that are necessary for code to work correctly (no encoding), where the visual editor will give you a more traditional editing experience for bits of content that you just want to be able to include in multiple places. Beware when switching between the two modes on existing snippets (save first).',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '20',
'class' => '',
'id' => '',
),
'choices' => array(
'visual' => 'Visual Editor',
'code' => 'Code Editor',
),
'default_value' => 'visual',
'return_format' => 'value',
'allow_null' => 0,
'other_choice' => 0,
'allow_in_bindings' => 0,
'layout' => 'vertical',
'save_other_choice' => 0,
),
array(
'key' => 'field_673a7203fa84d',
'label' => 'Content',
'name' => 'content',
'aria-label' => '',
'type' => 'acfe_code_editor',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_673a7168fa84c',
'operator' => '==',
'value' => 'code',
),
),
),
'wrapper' => array(
'width' => '80',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'mode' => 'text/html',
'lines' => 1,
'indent_unit' => 4,
'maxlength' => '',
'rows' => 20,
'max_rows' => '',
'return_format' => array(
),
'allow_in_bindings' => 0,
),
array(
'key' => 'field_673a7218fa84e',
'label' => 'Content',
'name' => 'content',
'aria-label' => '',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_673a7168fa84c',
'operator' => '==',
'value' => 'visual',
),
),
),
'wrapper' => array(
'width' => '80',
'class' => '',
'id' => '',
),
'default_value' => '',
'allow_in_bindings' => 0,
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'snippet',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'seamless',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => 0,
'acfe_display_title' => '',
'acfe_autosync' => array(
0 => 'php',
),
'acfe_form' => 0,
'acfe_meta' => '',
'acfe_note' => '',
'modified' => 1731884705,
));
endif;