-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-documentation.php
107 lines (95 loc) · 2.76 KB
/
wp-documentation.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
<?php
/**
* Plugin Name: WP Documentation
* Plugin URI: https://github.com/pressden/wp-documentation
* Description: Adds a documentation custom post type to WordPress.
* Version: 1.0
* Author: D.S. Webster
*
* @package WPDocumentation
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Define the custom post type slug.
define( 'WPD_SLUG', 'documentation' );
/**
* Initialize documentation custom post type
*/
function wpd_initialize() {
$wpd_label = ucfirst( WPD_SLUG );
// Register documentation post type.
$args = array(
'labels' => array(
'name' => $wpd_label,
'singular_name' => $wpd_label,
'add_new_item' => 'Add New ' . $wpd_label,
'edit_item' => 'Edit ' . $wpd_label,
'new_item' => 'Create ' . $wpd_label,
'view_item' => 'View ' . $wpd_label,
'search_items' => 'Search ' . $wpd_label,
'not_found' => 'No ' . strtolower( $wpd_label ) . ' found',
'not_found_in_trash' => 'No ' . strtolower( $wpd_label ) . ' found in trash',
'parent_item_colon' => 'Parent ' . $wpd_label . ':',
),
'capability_type' => 'post',
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'trackbacks',
'custom-fields',
'comments',
'revisions',
'page-attributes',
'post-formats',
),
'public' => true,
'menu_icon' => 'dashicons-book',
'menu_position' => 20,
'rewrite' => array( 'slug' => WPD_SLUG ),
'has_archive' => true,
'show_in_rest' => true,
);
register_post_type( WPD_SLUG, $args );
}
add_action( 'init', 'wpd_initialize' );
/**
* Filter requests for documentation.
*
* @param object $query Instance of WP_Query object.
*/
function wpd_restrict_frontend_access( $query ) {
// Protect the documentation post type.
if (
// This is the main query.
$query->is_main_query() &&
// The post_type query is set.
isset( $query->query['post_type'] ) &&
// This is a documentation post_type query.
'documentation' === $query->query['post_type'] &&
// This user does not have permission to view documentation.
! current_user_can( 'edit_posts' )
) {
$query->set_404();
status_header( 404 );
}
}
add_action('pre_get_posts', 'wpd_restrict_frontend_access');
/**
* Exclude the documentation post type from Yoast SEO Sitemap(s)
*
* @param boolean $excluded Exclusion boolean for the current post type.
* @param string $post_type The current post type.
*/
function wpd_wpseo_exclude_from_sitemap( $excluded, $post_type ) {
$excludedPostTypes = array( WPD_SLUG );
if ( in_array( $post_type, $excludedPostTypes ) ) {
return true;
}
return $excluded;
}
add_filter( 'wpseo_sitemap_exclude_post_type', 'wpd_wpseo_exclude_from_sitemap', 10, 2 );