-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.php
248 lines (201 loc) · 7.17 KB
/
frontend.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
<?php
use zz\Html\HTMLMinify;
/**
* Frontend usefull hooks.
*
* @package CleanWordPress\Frontend
* @since Clean WordPress 1.1.0
*/
if (!defined('ABSPATH')) {
die('You are not authorized to directly access to this page');
}
//-Admin bar--------------------------------------------------------//
//Remove admin bar since v3.1
add_filter('show_admin_bar', '__return_false');
remove_action('init', 'wp_admin_bar_init');
//-Feeds--------------------------------------------------------//
//Remove automatic feed links since WP v.3.x
remove_theme_support('automatic-feed-links');
//Remove feed links
//<link rel="alternate" type="application/rss+xml" title="__SITE_NAME__ » Comments Feed" href="__SITE_URL__/comments/feed/" />
remove_action('wp_head', 'feed_links', 2);
//Remove extra feed links, such as categories, tags, etc.
//<link rel="alternate" type="application/rss+xml" title="__SITE_NAME__ » Comments Feed" href="__SITE_URL__/category/__CAT__/feed/" />
remove_action('wp_head', 'feed_links_extra', 3);
//-Generator--------------------------------------------------------//
//Remove WORDPRESS version.
//<meta name="generator" content="WordPress __WP_VERSION__" />
remove_action('wp_head', 'wp_generator');
/**
* Remove WORDPRESS generator info.
*
* @since Clean WordPress 1.1.0
*/
add_filter('the_generator', '_cw_remove_version');
function _cw_remove_version()
{
return '';
}
//-Links--------------------------------------------------------//
//Link to adjacent posts.
//<link rel="prev" title="adjacent_posts_rel_link" href="__SITE_URL__" />
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
//Link to blog index.
//<link rel="index" title="__SITE_NAME__" href="__SITE_URL__" />
remove_action('wp_head', 'index_rel_link');
//Really Simple Discovery support.
//<link rel="EditURI" type="application/rsd+xml" title="RSD" href="__SITE_URL__" />
remove_action('wp_head', 'rsd_link');
//Remove Shortlink Link Rel Hook
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
//Windows Live Writter support.
//<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="__SITE_URL__" />
remove_action('wp_head', 'wlwmanifest_link');
//Others
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
//-i18n--------------------------------------------------------//
//Remove i18n styles
remove_action('wp_head', 'wp_dlmp_l10n_style');
//-Frontend hooks--------------------------------------------------------//
/**
* Remove unecessary details in body_class.
*
* @param string $classes Default classes for `body_class()`
* @param string $class Other classes for `body_class()`
* @uses get_query_var() Get a special value from query.
* @uses get_user_by() Get user data for an unknown user.
* @uses get_userdata() Get user data for a known user.
* @uses is_author() To know if current page is an author's page.
* @uses sanitize_html_class()
* @uses get_queried_object()
* @uses get_queried_object_id()
*
* @since Clean WordPress 1.3.1
*/
add_filter('body_class', '_cw_clean_body_class', 10, 2);
function _cw_clean_body_class($classes, $class)
{
//Remove unecessary CSS classes generated by WORDPRESS on authors pages
if (isset($classes['author'])) {
//Get author
if (!$name = get_query_var('author_name')) {
$author = get_userdata(get_query_var('author'));
}
else {
$author = get_user_by('slug', $name);
}
//Get data to remove
$removed[] = 'author-'.$author->ID;
$removed[] = 'author-'.$author->user_nicename;
//Remove classes
$classes = array_diff($classes, $removed);
}
//Remove unecessary CSS classes generated by WORDPRESS on single pages
if (isset($classes['single'])) {
//Get post details
$post = get_queried_object();
$pid = get_queried_object_id();
//Check post type
if (isset($post->post_type)) {
$removed[] = 'single-'.sanitize_html_class($post->post_type, $pid);
$removed[] = 'postid-'.$pid;
//Remove classes
$classes = array_diff($classes, $removed);
}
}
//Remove unecessary CSS classes generated by WORDPRESS on categories / tags / tax
if (isset($classes['category']) || isset($classes['tag']) || isset($classes['tax'])) {
//Get object details
$obj = get_queried_object();
//Check object ID
if (isset($obj->term_id)) {
//Get class
$obj_class = sanitize_html_class($obj->slug, $obj->term_id);
$obj_class = is_numeric($obj_class) || !trim($obj_class, '-') ?
$obj->term_id :
$obj_class;
//Remove details
if (isset($classes['category'])) {
$removed[] = 'category-'.$obj_class;
$removed[] = 'category-'.$obj->term_id;
}
else if (isset($classes['tag'])) {
$removed[] = 'tag-'.$obj_class;
$removed[] = 'tag-'.$obj->term_id;
}
else if (isset($classes['tax'])) {
$removed[] = 'tax-'.sanitize_html_class($obj->taxonomy);
$removed[] = 'term-'.$obj_class;
$removed[] = 'term-'.$obj->term_id;
}
//Remove classes
$classes = array_diff($classes, $removed);
}
}
//Return all classes
return array_merge($classes, (array)$class);
}
/**
* Delete Wordpress auto-formatting.
*
* @uses wptexturize()
* @uses wpautop() To format content
*
* @since Clean WordPress 1.3.1
*/
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', '_cw_shortcode_formatter', 99);
function _cw_shortcode_formatter($content)
{
$new_content = '';
$pattern_full = '{(\[raw\].*?\[/raw\])}is';
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
//Make the magic
foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
/**
* Disable emojicons introduced with WP 4.2 in frontend panel.
*
* @uses remove_action()
* @uses add_filter()
*
* @since Clean WordPress 1.3.2
*/
add_action('init', '_cw_disable_wp_emojicons');
function _cw_disable_wp_emojicons()
{
//All actions related to emojis
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
}
/**
* Minify HTML code.
*
* @uses remove_action()
* @uses add_filter()
*
* @since Clean WordPress 1.3.6
*/
add_action('get_header', 'start_ob');
function start_ob()
{
ob_start('minify_html');
}
function minify_html($output)
{
$minify = new HTMLMinify($output, array('optimizationLevel' => HTMLMinify::OPTIMIZATION_ADVANCED));
return $minify->process();
}