-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-redirect-rules.php
155 lines (134 loc) · 4.93 KB
/
wp-redirect-rules.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
<?php
/*
Plugin Name: WP Redirect Rules
Description: A WordPress plugin for .htacccess management of HTTP 301 redirects. Go to Admin Dashboard > Tools > Redirect Rules.
Version: 1
Author: ara303
*/
function redirect_rules_menu() {
add_submenu_page(
'tools.php',
'Redirect Rules',
'Redirect Rules',
'manage_options',
'redirect-rules',
'redirect_rules_page'
);
}
add_action( 'admin_menu', 'redirect_rules_menu' );
function redirect_rules_page() {
if( ! current_user_can( 'manage_options' ) ){
wp_die( "Access denied." );
}
if( isset( $_POST['submit'] ) ){
$from = sanitize_text_field( $_POST['from_url'] );
$to = sanitize_text_field( $_POST['to_url'] );
add_redirect_rule( $from, $to );
}
if( isset( $_POST['delete'] ) ){
$rule_id = intval( $_POST['rule_id'] );
delete_redirect_rule( $rule_id );
}
$rules = get_redirect_rules();
?>
<div class="wrap">
<style>
.wprr-form-wrapper {
margin: 2em 0 2.5em;
display: flex;
gap: 1em;
}
.wprr-form-input {
font-size: 13px;
flex-grow: 1;
}
</style>
<h1>WP Redirect Rules</h1>
<h2>New rule</h2>
<form method="post" action="" class="wprr-form-wrapper">
<input class="wprr-form-input" type="text" name="from_url" placeholder="From URL" required>
<input class="wprr-form-input" type="text" name="to_url" placeholder="To URL" required>
<input type="submit" name="submit" value="Add new" class="button button-primary">
</form>
<h2>Rules</h2>
<table class="widefat striped">
<thead>
<tr>
<th>From</th>
<th>To</th>
<th style="width: 53px;"></th>
</tr>
</thead>
<tbody>
<?php foreach( $rules as $id => $rule ): ?>
<tr>
<td><?php echo esc_html($rule['from']); ?></td>
<td><?php echo esc_html($rule['to']); ?></td>
<td>
<form method="post" action="">
<input type="hidden" name="rule_id" value="<?php echo $id; ?>">
<input type="submit" name="delete" value="Delete" class="button button-secondary button-small" style="color: #a00; border-color: #a00;">
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
function get_redirect_rules() {
$rules = get_option( 'redirect_rules', [] );
return $rules;
}
function add_redirect_rule($from, $to) {
$rules = get_redirect_rules();;
$rules[] = [ 'from' => $from, 'to' => $to ];
update_option( 'redirect_rules', $rules );
update_htaccess();
}
function delete_redirect_rule( $id ){
$rules = get_redirect_rules();
if( isset( $rules[$id] ) ){
unset( $rules[$id] );
update_option( 'redirect_rules', $rules );
update_htaccess();
}
}
function update_htaccess(){
$htaccess_path = ABSPATH . '.htaccess';
$htaccess = file_get_contents( $htaccess_path );
$rules = get_redirect_rules();
$redirect_block = "\n\n# START WP-Redirect-Rules\n";
foreach( $rules as $rule ) {
$redirect_block .= "Redirect 301 {$rule['from']} {$rule['to']}\n";
}
$redirect_block .= "# CLOSE WP-Redirect-Rules\n";
$regex = '/\n\n# START WP-Redirect-Rules\n.*# CLOSE WP-Redirect-Rules\n/s';
if( preg_match( $regex, $htaccess ) ){
$new_block = preg_replace( $regex, $redirect_block, $htaccess );
} else {
$new_block = $htaccess . $redirect_block;
}
file_put_contents( $htaccess_path, $new_block );
}
/**
* Create an empty rules block in .htaccess upon plugin activation, unless one already exists.
*/
function wprr_activate(){
$htaccess = file_get_contents( ABSPATH . '.htaccess' );
if( ! preg_match( '/\n\n# START WP-Redirect-Rules\n.*# CLOSE WP-Redirect-Rules\n/s', $htaccess) ) {
$rules_block = $htaccess . "\n\n# START WP-Redirect-Rules\n# CLOSE WP-Redirect-Rules\n";
file_put_contents( $htaccess, $rules_block );
}
}
register_activation_hook( __FILE__, 'wprr_activate' );
/**
* Remove the start/close comments, along with rules in between, upon plugin deactivation.
*/
function wprr_deactivate() {
$htaccess = file_get_contents( ABSPATH . '.htaccess' );
$remove_blocks = preg_replace( '/\n\n# START WP-Redirect-Rules\n.*# CLOSE WP-Redirect-Rules\n/s', '', $htaccess );
file_put_contents( $htaccess, $remove_blocks );
}
register_deactivation_hook(__FILE__, 'wprr_deactivate');