-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
143 lines (116 loc) · 3.03 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
<?php
if ( ! function_exists( 'blank_slate_bootstrap' ) ) {
/**
* Initialize the plugin.
*/
function blank_slate_bootstrap() {
load_plugin_textdomain( 'blank-slate', false, __DIR__ . '/languages' );
// Register the blank slate template
blank_slate_add_template(
'blank-slate-template.php',
esc_html__( 'Blank Slate', 'blank-slate' )
);
// Add our template(s) to the dropdown in the admin
add_filter(
'theme_page_templates',
function ( array $templates ) {
return array_merge( $templates, blank_slate_get_templates() );
}
);
// Ensure our template is loaded on the front end
add_filter(
'template_include',
function ( $template ) {
if ( is_singular() ) {
$assigned_template = get_post_meta( get_the_ID(), '_wp_page_template', true );
if ( blank_slate_get_template( $assigned_template ) ) {
if ( file_exists( $assigned_template ) ) {
return $assigned_template;
}
// Allow themes to override plugin templates
$file = locate_template( wp_normalize_path( '/blank-slate/' . $assigned_template ) );
if ( ! empty( $file ) ) {
return $file;
}
// Fetch template from plugin directory
$file = wp_normalize_path( plugin_dir_path( __FILE__ ) . '/templates/' . $assigned_template );
if ( file_exists( $file ) ) {
return $file;
}
}
}
return $template;
}
);
}
}
if ( ! function_exists( 'blank_slate_get_templates' ) ) {
/**
* Get all registered templates.
*
* @return array
*/
function blank_slate_get_templates() {
return (array) apply_filters( 'blank_slate_templates', array() );
}
}
if ( ! function_exists( 'blank_slate_get_template' ) ) {
/**
* Get a registered template.
*
* @param string $file Template file/path
*
* @return string|null
*/
function blank_slate_get_template( $file ) {
$templates = blank_slate_get_templates();
return isset( $templates[ $file ] ) ? $templates[ $file ] : null;
}
}
if ( ! function_exists( 'blank_slate_add_template' ) ) {
/**
* Register a new template.
*
* @param string $file Template file/path
* @param string $label Label for the template
*/
function blank_slate_add_template( $file, $label ) {
add_filter(
'blank_slate_templates',
function ( array $templates ) use ( $file, $label ) {
$templates[ $file ] = $label;
return $templates;
}
);
}
}
if ( ! function_exists( 'blank_slate_register_admin_page' ) ) {
/**
* Register the admin page.
*/
function blank_slate_register_admin_page() {
add_menu_page(
esc_html__( 'Blank Slate', 'blank-slate' ),
esc_html__( 'Blank Slate', 'blank-slate' ),
'edit_posts',
'blank-slate',
function () {
require __DIR__ . '/pages/admin.php';
},
'dashicons-media-default'
);
}
}
if ( ! function_exists( 'wp_body_open' ) ) {
/**
* Add wp_body_open() template tag if it doesn't exist (WP versions less than 5.2).
*/
function wp_body_open() {
/**
* Triggered after the opening body tag.
*
* @since 5.2.0
*/
do_action( 'wp_body_open' );
}
}