/
has-single.php
110 lines (90 loc) · 2.88 KB
/
has-single.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
<?php
namespace Sleek\PostTypes;
################################################
# Add support for has_single in post type config
add_filter('template_redirect', function () {
global $wp_query;
$postTypes = get_post_types(['public' => true], 'objects');
foreach ($postTypes as $postType) {
if (isset($postType->has_single) and $postType->has_single === false and is_singular($postType->name)) {
status_header(404); # Sets 404 header
$wp_query->set_404(); # Shows 404 template
}
}
});
# Remove permalink below title and preview button
add_filter('admin_head', function ($return) {
$currentScreen = get_current_screen();
if (isset($currentScreen->post_type)) {
$ptObj = get_post_type_object($currentScreen->post_type);
if (isset($ptObj->has_single) and $ptObj->has_single === false) {
?>
<style>
#edit-slug-box,
#minor-publishing-actions #preview-action {
display: none;
}
</style>
<?php
}
}
});
# Remove slug metabox
add_action('add_meta_boxes', function () {
$postTypes = get_post_types(['public' => true], 'objects');
foreach ($postTypes as $postType) {
if (isset($postType->has_single) and $postType->has_single === false) {
remove_meta_box('slugdiv', $postType->name, 'normal');
}
}
});
# Remove "View post" from admin-bar
add_action('admin_bar_menu', function ($adminBar) {
if (is_admin()) {
$currentScreen = get_current_screen();
if (isset($currentScreen->post_type)) {
$ptObj = get_post_type_object($currentScreen->post_type);
if (isset($ptObj->has_single) and $ptObj->has_single === false) {
$adminBar->remove_node('view');
}
}
}
}, 99);
# Remove "View" link from admin archive
add_filter('post_row_actions', function ($actions) {
$currentScreen = get_current_screen();
if (isset($currentScreen->post_type)) {
$ptObj = get_post_type_object($currentScreen->post_type);
if (isset($ptObj->has_single) and $ptObj->has_single === false) {
unset($actions['view']);
}
}
return $actions;
});
#######################################
# Remove !has_single from Yoast Sitemap
# NOTE: Using wpseo_sitemap_exclude_post_type excludes the archive pages too
add_filter('wpseo_exclude_from_sitemap_by_post_ids', function () {
$postTypes = get_post_types(['public' => true], 'objects');
$ids = [];
foreach ($postTypes as $postType) {
if (isset($postType->has_single) and $postType->has_single === false) {
$ids = array_merge($ids, get_posts([
'post_type' => $postType->name,
'numberposts' => -1,
'fields' => 'ids'
]));
}
}
return $ids;
}, 99);
########################################
# Remove Yoast meta box from !has_single
add_action('add_meta_boxes', function () {
$postTypes = get_post_types(['public' => true], 'objects');
foreach ($postTypes as $postType) {
if (isset($postType->has_single) and $postType->has_single === false) {
remove_meta_box('wpseo_meta', $postType->name, 'normal');
}
}
}, 100);