forked from DevinWalker/custom-functionality-plugin-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
security-customizations
183 lines (147 loc) · 4.64 KB
/
security-customizations
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
<?php
/**
* Security Customizations
*
* @description: All security functions go here
*/
/*************
*
* Update URL on search. Example: /search/keywords+here
* https://managewp.com/enhanced-performance-code-snippets
*
*******************/
function fb_change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'fb_change_search_url_rewrite' );
/************
* Automatic change the permalink
* http://wordpress.stackexchange.com/questions/31207/how-to-set-permalink-structure-via-functions-php
*
******************/
add_action( 'init', function() {
global $wp_rewrite;
$wp_rewrite->set_permalink_structure( '%postname%/' );
} );
/***********************
*
* Force all scripts into wp_footer
* https://managewp.com/enhanced-performance-code-snippets
*
***********************/
if(!is_admin()){
remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_footer', 'wp_print_scripts', 5);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
add_action('wp_footer', 'wp_print_head_scripts', 5);
}
/****************************
*
* Hide update warning for every user but admin
* http://www.1stwebdesigner.com/wordpress/12-wordpress-coding-snippets-hacks/
*****************************/
function wp_hide_update() {
global $current_user;
get_currentuserinfo();
if ($current_user->ID != 1) { // only admin will see it
remove_action( 'admin_notices', 'update_nag', 3 );
}
}
add_action('admin_menu','wp_hide_update');
/*************************
*
* Email Address Encoder plugin by Til Kruss
* http://wordpress.org/support/plugin/email-address-encoder
*
**************************/
/**
* Define plugin constants that can be overridden, generally in wp-config.php.
*/
if (!defined('EAE_FILTER_PRIORITY'))
define('EAE_FILTER_PRIORITY', 1000);
/**
* Register filters to encode exposed email addresses in
* posts, pages, excerpts, comments and widgets.
*/
foreach (array('the_content', 'the_excerpt', 'widget_text', 'comment_text', 'comment_excerpt') as $filter) {
add_filter($filter, 'eae_encode_emails', EAE_FILTER_PRIORITY);
}
/**
* Searches for plain email addresses in given $string and
* encodes them (by default) with the help of eae_encode_str().
*
* Regular expression is based on based on John Gruber's Markdown.
* http://daringfireball.net/projects/markdown/
*
* @param string $string Text with email addresses to encode
* @return string $string Given text with encoded email addresses
*/
function eae_encode_emails($string) {
// abort if $string doesn't contain a @-sign
if (apply_filters('eae_at_sign_check', true)) {
if (strpos($string, '@') === false) return $string;
}
// override encoding function with the 'eae_method' filter
$method = apply_filters('eae_method', 'eae_encode_str');
// override regex pattern with the 'eae_regexp' filter
$regexp = apply_filters(
'eae_regexp',
'{
(?:mailto:)?
(?:
[-!#$%&*+/=?^_`.{|}~\w\x80-\xFF]+
|
".*?"
)
\@
(?:
[-a-z0-9\x80-\xFF]+(\.[-a-z0-9\x80-\xFF]+)*\.[a-z]+
|
\[[\d.a-fA-F:]+\]
)
}xi'
);
return preg_replace_callback(
$regexp,
create_function(
'$matches',
'return '.$method.'($matches[0]);'
),
$string
);
}
/**
* Encodes each character of the given string as either a decimal
* or hexadecimal entity, in the hopes of foiling most email address
* harvesting bots.
*
* Based on Michel Fortin's PHP Markdown:
* http://michelf.com/projects/php-markdown/
* Which is based on John Gruber's original Markdown:
* http://daringfireball.net/projects/markdown/
* Whose code is based on a filter by Matthew Wickline, posted to
* the BBEdit-Talk with some optimizations by Milian Wolff.
*
* @param string $string Text with email addresses to encode
* @return string $string Given text with encoded email addresses
*/
function eae_encode_str($string) {
$chars = str_split($string);
$seed = mt_rand(0, (int) abs(crc32($string) / strlen($string)));
foreach ($chars as $key => $char) {
$ord = ord($char);
if ($ord < 128) { // ignore non-ascii chars
$r = ($seed * (1 + $key)) % 100; // pseudo "random function"
if ($r > 60 && $char != '@') ; // plain character (not encoded), if not @-sign
else if ($r < 45) $chars[$key] = '&#x'.dechex($ord).';'; // hexadecimal
else $chars[$key] = '&#'.$ord.';'; // decimal (ascii)
}
}
return implode('', $chars);
}
/**********************/