-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
660 lines (577 loc) · 17.9 KB
/
functions.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
<?php
/**
* Template functions
*
* @package WordPress
* @subpackage arnexyz
* @since arnexyz 1.0.0
* @author Arne Kamola <arne@arne.xyz>
*/
/**
* Activate the classic link manager
*
* @see https://blog.templatetoaster.com/wordpress-links-manager/
* @since arnexyz 1.0.0
*/
add_filter( 'pre_option_link_manager_enabled', '__return_true' );
/**
* Check if it's CSS Naked Day: April 9th
* The day is changeable via parameters for debugging purposes.
*
* @see https://css-naked-day.github.io
* @since arnexyz 1.2.0
* @param int $d Day of the CSS Naked Day
* @param int $m Month of the CSS Naked Day
*/
function is_css_naked_day( $d = 9, $m = 4 ) {
$start = date( 'U', mktime( -12, 0, 0, $m, $d, date('Y') ) );
$end = date( 'U', mktime( 36, 0, 0, $m, $d, date('Y') ) );
$z = date( 'Z' ) * -1;
$now = time() + $z;
if ( $now >= $start && $now <= $end ) {
return true;
}
return false;
}
/**
* Check whether a page is a subpage or not
*
* @see https://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages
* @since arnexyz 1.0.0
* @return False or the parent page object
*/
function is_subpage() {
global $post;
if ( is_page() && $post->post_parent ) {
return $post->post_parent;
} else {
return false;
}
}
/**
* Show a breadcrumb navigation
*
* @see http://dimox.net/wordpress-breadcrumbs-without-a-plugin/
* @since arnexyz 1.0.0
*/
function arnexyz_breadcrumb() {
// Settings
$sep = '<span class="sep">›</span>';
$home = get_bloginfo('name');
$before = '<span class="current-page">';
$after = '</span>';
// Generate breadcrumb navigation
if ( !is_home() && !is_front_page() || is_paged() ) {
echo '<p>' . __( 'You are here:' ) . ' ';
global $post;
$home_link = get_bloginfo('url');
echo '<a href="' . $home_link . '">' . $home . '</a> ' . $sep . ' ';
if ( is_category() ) {
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$this_cat = $cat_obj->term_id;
$this_cat = get_category( $this_cat );
$parentCat = get_category( $this_cat->parent );
if ( $this_cat->parent != 0 ) {
echo get_category_parents( $parentCat, $link = true, ' ' . $sep . ' ' );
}
echo $before . single_cat_title( '', $print = false ) . $after;
} elseif ( is_day() ) {
echo '<a href="' . get_year_link( get_the_time('Y') ) . '">' . get_the_time('Y') . '</a> ' . $sep . ' ';
echo '<a href="' . get_month_link( get_the_time('Y'), get_the_time('m') ) . '">' . get_the_time('F') . '</a> ' . $sep . ' ';
echo $before . get_the_time('d') . $after;
} elseif ( is_month() ) {
echo '<a href="' . get_year_link( get_the_time('Y') ) . '">' . get_the_time('Y') . '</a> ' . $sep . ' ';
echo $before . get_the_time('F') . $after;
} elseif ( is_year() ) {
echo $before . get_the_time('Y') . $after;
} elseif ( is_single() && !is_attachment() ) {
if ( get_post_type() != 'post' ) {
$post_type = get_post_type_object( get_post_type() );
$slug = $post_type->rewrite;
echo '<a href="' . $home_link . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a> ' . $sep . ' ';
echo $before . get_the_title() . $after;
} else {
$post_type = get_post_type_object( get_post_type() );
$slug = $post_type->rewrite;
//if ( is_front_page )
echo '<a href="' . get_permalink( get_option('page_for_posts') ) . '">' . __( 'Blog', 'arnexyz' ) . '</a> ' . $sep . ' ';
//endif;
echo $before . get_the_title() . $after;
}
} elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
$post_type = get_post_type_object( get_post_type() );
echo $before . $post_type->labels->singular_name . $after;
} elseif ( is_attachment() ) {
$parent = get_post( $post->post_parent );
$cat = get_the_category( $parent->ID );
$cat = $cat[0];
echo get_category_parents( $cat, $link = true, ' ' . $sep . ' ' );
echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> ' . $sep . ' ';
echo $before . get_the_title() . $after;
} elseif ( is_page() && !$post->post_parent ) {
echo $before . get_the_title() . $after;
} elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ( $parent_id ) {
$page = get_page( $parent_id );
$breadcrumbs[] = '<a href="' . get_permalink( $page->ID ) . '">' . get_the_title( $page->ID ) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse( $breadcrumbs );
foreach ( $breadcrumbs as $crumb ) {
echo $crumb . ' ' . $sep . ' ';
}
echo $before . get_the_title() . $after;
} elseif ( is_search() ) {
echo $before . __( 'Search results for: "', 'arnexyz' ) . get_search_query() . '"' . $after;
} elseif ( is_tag() ) {
echo $before . __( 'Posts with the tag "', 'arnexyz' ) . single_tag_title( '', false ) . '"' . $after;
} elseif ( is_404() ) {
echo $before . __( 'Error 404', 'arnexyz' ) . $after;
} elseif ( is_home() ) {
echo $before . __( 'Blog', 'arnexyz' ) . $after;
}
if ( get_query_var('paged') ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) {
echo ' (';
}
echo ': ' . __( 'Page', 'arnexyz' ) . ' ' . get_query_var('paged');
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) {
echo ')';
}
}
echo '</p>';
}
}
/**
* Add custom styles to the classic editor
*
* @since 2.1.0
* @see https://themeisle.com/blog/wordpress-editor-emulate-website/
*/
function arnexyz_editor_style() {
add_editor_style( 'editor-style.css' );
}
add_action( 'admin_init', 'arnexyz_editor_style' );
/**
* Remove the brackets around the categories post count
*
* @since arnexyz 1.2.0
* @return string
*/
function arnexyz_categories_postcount_filter( $count ) {
$count = str_replace('(', '<span class="post_count">', $count);
$count = str_replace(')', '</span>', $count);
return $count;
}
add_filter( 'wp_list_categories', 'arnexyz_categories_postcount_filter' );
/**
* Add custom post status for "retired" posts
*
* @see https://wordpress.org/support/article/post-status/#custom-status
* @since arnexyz 1.2.0
*/
function arnexyz_post_status() {
register_post_status( 'retired', array(
'label' => _x( 'Retired', 'Status General Name', 'arnexyz' ),
'label_count' => _n_noop( 'Retired <span class="count">(%s)</span>', 'Retired <span class="count">(%s)</span>', 'arnexyz' ),
// 'public' => false,
'protected' => true,
// 'show_in_admin_all_list' => true,
// 'show_in_admin_status_list' => true,
// 'exclude_from_search' => false,
));
}
add_action( 'init', 'arnexyz_post_status', 0 );
/**
* Add custom post status to the "Quick Edit" dropdown
*
* @see arnexyz_post_status()
* @see https://rudrastyh.com/wordpress/custom-post-status-quick-edit.html
* @since arnexyz 1.2.0
*/
function arnexyz_status_into_inline_edit() { // ultra-simple example
echo '<script>
jQuery(document).ready( function($) {
$( \'select[name="_status"]\' ).append( \'<option value="retired">' . __( 'Retired', 'arnexyz' ) . '</option>\' );
});
</script>';
}
add_action( 'admin_footer-edit.php', 'arnexyz_status_into_inline_edit' );
/**
* Add custom post type to the post status dropdown
*
* @see arnexyz_post_status()
* @since arnexyz 1.2.0
*/
function arnexyz_display_custom_post_status_option() {
global $post;
$complete = '';
$label = '';
if ( isset( $post ) && $post->post_type == 'post' ) {
if( $post->post_status == 'retired' ) {
$selected = 'selected';
}
echo '<script>
jQuery(document).ready( function($) {
$( \'select#post_status\' ).append( \'<option value="retired" ' . $selected . '>' . __( 'Retried', 'arnexyz' ) . '</option>\' );
$( \'#post-status-display\' ).text(' . __( 'Retired', 'arnexyz' ) . ');
});
</script>';
}
}
add_action( 'admin_footer', 'arnexyz_display_custom_post_status_option' );
/**
* Show custom post status label in the post overview
*
* @see https://rudrastyh.com/wordpress/custom-post-status-quick-edit.html
* @since arnexyz 1.2.0
* @return array
*/
function arnexyz_display_status_label( $statuses ) {
global $post; // we need it to check current post status
if ( isset( $post ) && get_query_var( 'post_status' ) != 'retired' ) { // not for pages with all posts of this status
if ( $post->post_status == 'retired' ) {
return array( __( 'Retired', 'arnexyz' ) ); // returning our status label
}
}
return $statuses; // returning the array with default statuses
}
add_filter( 'display_post_states', 'arnexyz_display_status_label' );
/**
* Get date/year of the first post
* @see https://alex.leonard.ie/2010/07/27/wordpress-tip-get-the-date-of-your-first-post/
*
* @since arne.xyz 2.2.0
* @return string Year of the first post
*/
function arnexyz_get_first_post_date( $format = 'Y' ) {
$args = array(
'numberposts' => -1,
'post_status' => 'publish',
'order' => 'ASC'
);
// Get all posts in order of first to last
$all_posts = get_posts( $args );
// Extract first post from array
$first_post = $all_posts[0];
// Assign first post date to var
$first_post_date = $first_post->post_date;
// Return date in required format
$output = date( $format, strtotime( $first_post_date ) );
return $output;
}
/**
* Add custom JavaScript that depends on jQuery.
*
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
* @since arnexyz 1.0.0
*/
function arnexyz_js() {
wp_enqueue_script(
$handle = 'theme-script',
$src = get_template_directory_uri() . '/app.js',
array( 'jquery' ),
$ver = false,
$in_footer = true
);
}
add_action( 'wp_enqueue_scripts', 'arnexyz_js' );
/**
* WordPress theme features
*
* @see https://developer.wordpress.org/reference/functions/add_theme_support/
* @since arnexyz 1.0.3
*/
function arnexyz_support() {
// Features
// @see https://developer.wordpress.org/reference/functions/add_theme_support/#features
// HTML5
// @see https://developer.wordpress.org/reference/functions/add_theme_support/#html5
add_theme_support( 'html5', array(
'caption',
'comment-list',
'comment-form',
'gallery',
'search-form'
));
// Post Formats
// @see https://codex.wordpress.org/Post_Formats
add_theme_support( 'post-formats', array(
'aside',
'gallery',
'link',
'image',
'quote',
'status',
'video',
'audio',
'chat'
));
}
add_action( 'after_setup_theme', 'arnexyz_support' );
/**
* Add theme menus for the WordPress menu feature
*
* @see https://codex.wordpress.org/Function_Reference/register_nav_menu
* @since arnexyz 1.0.0
*/
function arnexyz_register_menus() {
if ( function_exists('register_nav_menus') ) {
register_nav_menus( array(
'primary' => __('Main Navigation', 'arnexyz')
));
}
}
add_action( 'after_setup_theme', 'arnexyz_register_menus' );
/**
* Add theme sidebars for the WordPress widget feature
*
* @see https://codex.wordpress.org/Function_Reference/register_sidebars
* @since arnexyz 1.0.0
*/
function arnexyz_register_sidebars() {
// Primary
register_sidebar( array(
'name' => __('Sidebar', 'arnexyz'),
'id' => 'sidebar-1',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>'
));
// Footer
register_sidebar( array(
'name' => __('Footer', 'arnexyz'),
'id' => 'sidebar-footer',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>'
));
}
add_action( 'widgets_init', 'arnexyz_register_sidebars' );
/**
* Remove the WordPress emoji embed
*
* @see https://www.denisbouquet.com/remove-wordpress-emoji-code/
* @since arnexyz 1.0.0
*/
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
/**
* Remove DNS prefetch
*
* @see https://fransdejonge.com/2017/11/remove-dns-prefetch-from-wordpress-4-9/
* @since arnexyz 1.1.0
*/
remove_action( 'wp_head', 'wp_resource_hints', 2 );
/**
* Remove the generator meta tag with the WordPress version from the HTML for
* better security.
*
* @see https://developer.wordpress.org/reference/hooks/the_generator/
* @since arnexyz 1.1.0
* @return string silence is golden ...
*/
function arnexyz_remove_version() {
return '';
}
add_filter( 'the_generator', 'arnexyz_remove_version' );
/**
* Replace the default login error message to hide any information that could
* maybe used for cracking into the system.
*
* @see https://codex.wordpress.org/Plugin_API/Filter_Reference/login_errors
* @since arnexyz 1.0.2
* @return string Login error message
*/
function arnexyz_wrong_login() {
return __( 'Wrong username or password.', 'arnexyz' );
}
add_filter( 'login_errors', 'arnexyz_wrong_login' );
/**
* Don't store IP of commentors
*
* @see https://www.wpbeginner.com/wp-tutorials/how-to-stop-storing-ip-address-in-wordpress-comments/
* @since arnexyz 1.0.0
* @return string Silence is golden ...
*/
function arnexyz_remove_comments_ip( $comment_author_ip ) {
return '';
}
add_filter( 'pre_comment_user_ip', 'arnexyz_remove_comments_ip' );
/**
* Don't store the user agent of commentors
*
* @see wp_filter_comment()
* @since arnexyz 1.0.0
* @return string Silence is golden ...
*/
function arnexyz_remove_comments_useragent( $comment_author_agent ) {
return '';
}
add_filter( 'pre_comment_user_agent', 'arnexyz_remove_comments_useragent' );
//
// Theme options page
//
/**
* Setup the theme options page
*
* @since arnexyz 2.3.0
* @return void
*/
function arnexyz_theme_options_init() {
// Register options
register_setting(
$option_group = 'arnexyz_options',
$option_name = 'arnexyz_theme_options'
);
//
// Sections
//
// Footer Options
// Section
add_settings_section(
$id = 'arnexyz_options_footer',
$title = __( 'Footer', 'arnexyz' ),
$callback = 'arnexyz_theme_options_section_footer_description',
$page = 'arnexyz_options'
);
// Fields
add_settings_field(
$id = 'show_cc_license',
$title = __( 'Show Creative Commons License', 'arnexyz' ),
$callback = 'arnexyz_theme_options_field_cc_license',
$page = 'arnexyz_options',
$section = 'arnexyz_options_footer',
$args = array(
'label_for' => 'show_cc_license'
)
);
add_settings_field(
$id = 'show_rss_link',
$title = __( 'Show link to RSS feed', 'arnexyz' ),
$callback = 'arnexyz_theme_options_field_rss_link',
$page = 'arnexyz_options',
$section = 'arnexyz_options_footer',
$args = array(
'label_for' => 'show_rss_link'
)
);
add_settings_field(
$id = 'show_html_validation_link',
$title = __( 'Show HTML validation link', 'arnexyz' ),
$callback = 'arnexyz_theme_options_field_html_validation_link',
$page = 'arnexyz_options',
$section = 'arnexyz_options_footer',
$args = array(
'label_for' => 'show_html_validation_link'
)
);
add_settings_field(
$id = 'show_css_validation_link',
$title = __( 'Show CSS validation link', 'arnexyz' ),
$callback = 'arnexyz_theme_options_field_css_validation_link',
$page = 'arnexyz_options',
$section = 'arnexyz_options_footer',
$args = array(
'label_for' => 'show_css_validation_link'
)
);
}
add_action( 'admin_init', 'arnexyz_theme_options_init' );
/**
* Add menu item to the admin menu
*
* @since arnexyz 2.3.0
* @return void
*/
function arnexyz_theme_options_menuitem() {
add_theme_page(
__( 'Options', 'arnexyz' ),
__( 'Options', 'arnexyz' ),
'edit_theme_options',
'theme-options',
'arnexyz_theme_options_page'
);
}
add_action( 'admin_menu', 'arnexyz_theme_options_menuitem' );
/**
* The theme options page
*
* @since arnexyz 2.3.0
* @return void
*/
function arnexyz_theme_options_page() {
?>
<div class="wrap">
<h1>arnexyz › <?php _e( 'Theme Options', 'arnexyz' ); ?></h1>
<form action="options.php" method="post">
<?php
settings_fields( 'arnexyz_options' );
do_settings_sections( 'arnexyz_options' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Print the description of the theme option section "Footer"
*
* @since arnexyz 2.3.0
* @return void
*/
function arnexyz_theme_options_section_footer_description() {
_e( 'Modify the footer', 'arnexyz' );
}
/**
* Print the HTML for the theme options for "Show Creative Commons license"
*
* @since arnexyz 2.3.0
* @return void
*/
function arnexyz_theme_options_field_cc_license() {
$options = get_option( 'arnexyz_theme_options' );
?>
<input type="checkbox" name="arnexyz_theme_options[show_cc_license]" value="1"<?php if ( isset( $options['show_cc_license'] ) ) { checked( $options['show_cc_license'], 1 ); } ?>>
<p class="description" id="show_cc_license-description">Requires the WordPress plugin for <a href="https://de.wordpress.org/plugins/creative-commons/" target="_blank">Creative Commons</a>.</p>
<?php
}
/**
* Print the HTML for the theme option "Show CSS validation link"
*
* @since arnexyz 2.3.0
* @return void
*/
function arnexyz_theme_options_field_css_validation_link() {
$options = get_option( 'arnexyz_theme_options' );
?>
<input type="checkbox" name="arnexyz_theme_options[show_css_validation_link]" value="1"<?php if ( isset( $options['show_css_validation_link'] ) ) { checked( $options['show_css_validation_link'], 1 ); } ?>>
<?php
}
/**
* Print the HTML for the theme option "Show HTML validation link"
*
* @since arnexyz 2.3.0
* @return void
*/
function arnexyz_theme_options_field_html_validation_link() {
$options = get_option( 'arnexyz_theme_options' );
?>
<input type="checkbox" name="arnexyz_theme_options[show_html_validation_link]" value="1"<?php if ( isset( $options['show_html_validation_link'] ) ) { checked( $options['show_html_validation_link'], 1 ); } ?>>
<?php
}
/**
* Print the HTML for the theme option "Show link to RSS feed"
*
* @since arnexyz 2.3.0
* @return void
*/
function arnexyz_theme_options_field_rss_link() {
$options = get_option( 'arnexyz_theme_options' );
?>
<input type="checkbox" name="arnexyz_theme_options[show_rss_link]" value="1"<?php if ( isset( $options['show_rss_link'] ) ) { checked( $options['show_rss_link'], 1 ); } ?>>
<?php
}