-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
291 lines (227 loc) · 11 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
<?php
define('THEME_NAME', 'wpboiler');
// If there are problems with caching,
// change this version number
define('CACHE_VERSION', '1.0.0');
// require get_template_directory() . '/classes/class-wpboiler-svg-icons.php';
require get_template_directory() . '/inc/template-functions.php';
require get_template_directory() . '/inc/template-tags.php';
require get_template_directory() . '/inc/allowed-blocks.php';
require get_template_directory() . '/inc/acf-functions.php';
require get_template_directory() . '/inc/custom-hooks.php';
require get_template_directory() . '/inc/post-types.php';
require get_template_directory() . '/inc/custom-taxonomies.php';
require get_template_directory() . '/inc/meta-boxes.php';
if(!function_exists('wpboiler_init')) {
function wpboiler_init() {
// Remove everything to do with emojis
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
add_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 );
/**
* Filter function used to remove the TinyMCE emoji plugin
*
* @param array $plugins
* @return array Difference between the two arrays
*/
function disable_emojis_tinymce( $plugins ) {
if( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
}
return array();
}
/**
* Remove emoji CDN hostname from DNS prefetching hints
*
* @param array $urls URLs to print for resource hints
* @param string $relation_type The relation type the URLs are printed for
* @return array Difference between the two arrays
*/
function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
if( 'dns-prefetch' == $relation_type ) {
/** This filter is documented in wp-includes/formatting.php */
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
$urls = array_diff( $urls, array( $emoji_svg_url ) );
}
return $urls;
}
// Remove version number from header
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'wlwmanifest_link');
remove_action( 'wp_head', 'rsd_link' );
// Removes shortlink
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
// Removes feed links
remove_action( 'wp_head', 'feed_links', 2 );
// Removes comments feed
remove_action( 'wp_head', 'feed_links_extra', 3 );
}
}
add_action('init', 'wpboiler_init');
if(!function_exists('wpboiler_setup')) {
function wpboiler_setup() {
/**
* CUSTOM IMAGE SIZES
*/
add_image_size( 'promo', 600 );
add_image_size( 'cta', 2000 );
add_image_size( 'post-item', 1000 );
/**
* ADD THEME SUPPORT
*/
add_theme_support( 'post-thumbnails', array('post', 'page') );
add_theme_support( 'title-tag' );
add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption', 'style', 'script' ) );
add_theme_support( 'responsive-embeds' );
add_theme_support( 'editor-styles' );
add_post_type_support( 'page', 'excerpt' );
// add_editor_style( './css/style-editor.css' );
add_editor_style( './dist/editorStyle.css' );
remove_theme_support( 'core-block-patterns' );
// NAV MENU REGISTRATION
register_nav_menus(
array(
'primary' => esc_html__( 'Primary menu', THEME_NAME ),
'footer' => __( 'Secondary menu', THEME_NAME ),
)
);
}
}
add_action('after_setup_theme', 'wpboiler_setup');
/**
* Script and styles enqueues
*
*/
function wpboiler_scripts_init() {
// wp_enqueue_style( 'wpboiler-styles', get_template_directory_uri() . '/dist/style.css' );
// wp_enqueue_style( 'wpboiler-app-styles', get_template_directory_uri() . '/dist/app.css' );
// wp_enqueue_script( 'wpboiler-app-js', get_template_directory_uri() . '/dist/app.js', array(), CACHE_VERSION, true );
// Remove basic block styles for WP core blocks
wp_dequeue_style( 'wp-block-library' );
}
add_action( 'wp_enqueue_scripts', 'wpboiler_scripts_init' );
/**
* Load Google Fonts
*
*/
function wpboiler_load_google_fonts() {
?>
<link rel="dns-prefetch" href="https://fonts.gstatic.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin="anonymous">
<link rel="preload" href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,300;0,400;0,700;1,300&display=swap" as="fetch" crossorigin="anonymous">
<?php
}
add_action( 'wp_head', 'wpboiler_load_google_fonts' );
/**
* Custom scripts for the block editor
* Used to add new options in the sidebar
* Also removes basic functionality added by WP
*
*/
function wpboiler_block_additional_styles_enqueue() {
wp_enqueue_script(
'wpboiler-block-additonal-styles-script', get_template_directory_uri() . '/js/wpboiler-block-additional-styles.js', array('wp-blocks', 'wp-dom-ready', 'wp-edit-post')
);
}
add_action( 'enqueue_block_editor_assets', 'wpboiler_block_additional_styles_enqueue' );
function wpboiler_the_html_classes() {
/**
* Filters the classes for the main <html> element.
*
*/
$classes = apply_filters( 'wpboiler_html_classes', '' );
if( !$classes ) {
return;
}
echo 'class="' . esc_attr( $classes ) . '"';
}
// This scripts loader is used to load the scripts generated by HtmlWebpackPlugin
// This is used to load the scripts with the correct hash
function wpboiler_scripts_loader() {
// if(is_front_page()) {
$html = file_get_contents(__DIR__ . '/dist/index.html'); // get the contents of the generated index.html file using HtmlWebpackPlugin
// get all the script tags
// we want to inject the script tags in the footer using the file HtmlWebpackPlugin generates
// this will supply the correct hash for the file
preg_match_all('/<script.*?\s+src="([^"]+)"><\/script>/', $html, $matches);
foreach($matches[1] as $script) {
$file_path = '/wp-content/themes/wp-boilerplate/dist/' . $script; // get the file path
// $file_url = get_template_directory_uri() . $file_path; // get the file url
$file_url = $script; // get the file url
wp_enqueue_script(basename($script, '.js'), $file_url, array(), null, true); // enqueue the script
}
// }
}
add_action('wp_enqueue_scripts', 'wpboiler_scripts_loader');
function wpboiler_styles_loader() {
/**
* @TODO: If builds and devs work ok, remove the commented code below
*/
// if(WP_ENV === 'development') {
// wp_enqueue_style('wp-boilerplate-main', 'http://localhost:8080/wp-content/themes/wp-boilerplate/dist/app.css', [], null);
// wp_enqueue_script('wp-boilerplate-main', 'http://localhost:8080/wp-content/themes/wp-boilerplate/dist/app.js', [], null, true);
// } else {
// if(is_front_page()) {
$html = file_get_contents(__DIR__ . '/dist/index.html'); // get the contents of the generated index.html file using HtmlWebpackPlugin
// get all the link tags
// we want to inject the link tags in the header using the file HtmlWebpackPlugin generates
// this will supply the correct hash for the file
preg_match_all('/<link.*?\s+href="([^"]+\.css)".*?>/i', $html, $matches);
foreach($matches[1] as $style) {
$file_path = '/wp-content/themes/wp-boilerplate/dist/' . $style; // get the file path
// $file_url = get_template_directory_uri() . $file_path; // get the file url
$file_url = $style; // get the file url
var_dump(basename($style, '.css'));
wp_enqueue_style(basename($style, '.css'), $file_url, array(), false, 'all'); // enqueue the styles
}
// }
// }
}
add_action('wp_enqueue_scripts', 'wpboiler_styles_loader');
function wpboiler_editor_scripts_loader() {
// if(is_front_page()) {
$html = file_get_contents(__DIR__ . '/dist/index.html'); // get the contents of the generated index.html file using HtmlWebpackPlugin
// get all the script tags
// we want to inject the script tags in the footer using the file HtmlWebpackPlugin generates
// this will supply the correct hash for the file
preg_match_all('/<script.*?\s+src="([^"]+)"><\/script>/', $html, $matches);
foreach($matches[1] as $script) {
$file_path = '/wp-content/themes/wp-boilerplate/dist/' . $script; // get the file path
// $file_url = get_template_directory_uri() . $file_path; // get the file url
$file_url = $script; // get the file url
wp_enqueue_script(basename($script, '.js'), $file_url, array(), null, true); // enqueue the script
}
// }
}
add_action('admin_enqueue_scripts', 'wpboiler_editor_scripts_loader');
function wpboiler_editor_styles_loader() {
/**
* @TODO: If builds and devs work ok, remove the commented code below
*/
// if(WP_ENV === 'development') {
// wp_enqueue_style('wp-boilerplate-main', 'http://localhost:8080/wp-content/themes/wp-boilerplate/dist/app.css', [], null);
// wp_enqueue_script('wp-boilerplate-main', 'http://localhost:8080/wp-content/themes/wp-boilerplate/dist/app.js', [], null, true);
// } else {
// if(is_front_page()) {
$html = file_get_contents(__DIR__ . '/dist/index.html'); // get the contents of the generated index.html file using HtmlWebpackPlugin
// get all the link tags
// we want to inject the link tags in the header using the file HtmlWebpackPlugin generates
// this will supply the correct hash for the file
preg_match_all('/<link.*?\s+href="([^"]+\.css)".*?>/i', $html, $matches);
foreach($matches[1] as $style) {
$file_path = '/wp-content/themes/wp-boilerplate/dist/' . $style; // get the file path
// $file_url = get_template_directory_uri() . $file_path; // get the file url
$file_url = $style; // get the file url
var_dump(basename($style, '.css'));
wp_enqueue_style(basename($style, '.css'), $file_url, array(), false, 'all'); // enqueue the styles
}
// }
// }
}
add_action('admin_enqueue_scripts', 'wpboiler_editor_styles_loader');