Version Description
- Fixed redirection bug of password protected pages
Download this release
Release Info
Developer | SomeWebMedia |
Plugin | HC Custom WP-Admin URL |
Version | 1.3 |
Comparing to | |
See all releases |
Code changes from version 1.2 to 1.3
- hc-custom-wp-admin-url.php +151 -143
- readme.txt +5 -2
hc-custom-wp-admin-url.php
CHANGED
@@ -1,159 +1,167 @@
|
|
1 |
<?php
|
2 |
/*
|
3 |
Plugin Name: HC Custom WP-Admin URL
|
4 |
-
Version: 1.
|
5 |
-
Plugin URI: http://wordpress.org/
|
6 |
Description: Small and simple plugin that allows you to change url of wp-admin
|
7 |
Author: Some Web Media
|
8 |
Author URI: http://someweblog.com/
|
9 |
*/
|
10 |
|
11 |
-
if (!class_exists('
|
12 |
-
|
13 |
-
class hc_custom_wpadmin_url {
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
if (isset($_POST['custom_wpadmin_slug'])) {
|
24 |
-
|
25 |
-
// sanitize input
|
26 |
-
$wpadmin_slug = trim(sanitize_key(wp_strip_all_tags($_POST['custom_wpadmin_slug'])));
|
27 |
-
$home_path = get_home_path();
|
28 |
-
|
29 |
-
// check if permalinks are turned off, if so force push rules to .htaccess
|
30 |
-
if (isset($_POST['selection']) && $_POST['selection'] == '' && $wpadmin_slug != '') {
|
31 |
-
// check if .htaccess is writable
|
32 |
-
if ((!file_exists($home_path . '.htaccess') && is_writable($home_path)) || is_writable($home_path . '.htaccess')) {
|
33 |
-
|
34 |
-
// taken from wp-includes/rewrite.php
|
35 |
-
$home_root = parse_url(home_url());
|
36 |
-
if (isset($home_root['path']))
|
37 |
-
$home_root = trailingslashit($home_root['path']);
|
38 |
-
else
|
39 |
-
$home_root = '/';
|
40 |
-
// create rules
|
41 |
-
$rules = "<IfModule mod_rewrite.c>\n";
|
42 |
-
$rules .= "RewriteEngine On\n";
|
43 |
-
$rules .= "RewriteRule ^$wpadmin_slug/?$ ".$home_root."wp-login.php [QSA,L]\n";
|
44 |
-
$rules .= "</IfModule>";
|
45 |
-
// write to .htaccess
|
46 |
-
insert_with_markers($home_path . '.htaccess', 'WPAdminURL', explode("\n", $rules));
|
47 |
-
}
|
48 |
-
} else if (isset($_POST['selection']) || (isset($_POST['selection']) && $_POST['selection'] == '' && $wpadmin_slug == '')) {
|
49 |
-
// remove rules if permalinks were enabled
|
50 |
-
$markerdata = explode("\n", implode('', file($home_path . '.htaccess')));
|
51 |
-
$found = false;
|
52 |
-
$newdata = '';
|
53 |
-
foreach ($markerdata as $line) {
|
54 |
-
if ($line == '# BEGIN WPAdminURL')
|
55 |
-
$found = true;
|
56 |
-
if (!$found)
|
57 |
-
$newdata .= "$line\n";
|
58 |
-
if ($line == '# END WPAdminURL')
|
59 |
-
$found = false;
|
60 |
-
}
|
61 |
-
// write back
|
62 |
-
$f = @fopen($home_path . '.htaccess', 'w');
|
63 |
-
fwrite($f, $newdata);
|
64 |
}
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
}
|
75 |
-
}
|
76 |
-
add_settings_field('custom_wpadmin_slug', 'WP-Admin slug', array($this, 'options_page'), 'permalink', 'optional', array('label_for' => 'custom_wpadmin_slug'));
|
77 |
-
register_setting('permalink', 'custom_wpadmin_slug', 'strval');
|
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 |
-
if ($found)
|
147 |
-
$this->custom_login();
|
148 |
-
}
|
149 |
}
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
}
|
1 |
<?php
|
2 |
/*
|
3 |
Plugin Name: HC Custom WP-Admin URL
|
4 |
+
Version: 1.3
|
5 |
+
Plugin URI: http://wordpress.org/plugins/hc-custom-wp-admin-url/
|
6 |
Description: Small and simple plugin that allows you to change url of wp-admin
|
7 |
Author: Some Web Media
|
8 |
Author URI: http://someweblog.com/
|
9 |
*/
|
10 |
|
11 |
+
if (!class_exists('HC_CustomWPAdminURL')) {
|
|
|
|
|
12 |
|
13 |
+
class HC_CustomWPAdminURL {
|
14 |
+
|
15 |
+
function rewrite_admin_url($wp_rewrite) {
|
16 |
+
// be sure rules are written every time permalinks are updated
|
17 |
+
$rule = get_option('custom_wpadmin_slug');
|
18 |
+
if ($rule != '') {
|
19 |
+
add_rewrite_rule($rule.'/?$', 'wp-login.php', 'top');
|
20 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
}
|
22 |
+
|
23 |
+
function custom_admin_url() {
|
24 |
+
if (isset($_POST['custom_wpadmin_slug'])) {
|
25 |
+
|
26 |
+
// sanitize input
|
27 |
+
$wpadmin_slug = trim(sanitize_key(wp_strip_all_tags($_POST['custom_wpadmin_slug'])));
|
28 |
+
$home_path = get_home_path();
|
29 |
+
|
30 |
+
// check if permalinks are turned off, if so force push rules to .htaccess
|
31 |
+
if (isset($_POST['selection']) && $_POST['selection'] == '' && $wpadmin_slug != '') {
|
32 |
+
// check if .htaccess is writable
|
33 |
+
if ((!file_exists($home_path . '.htaccess') && is_writable($home_path)) || is_writable($home_path . '.htaccess')) {
|
34 |
+
|
35 |
+
// taken from wp-includes/rewrite.php
|
36 |
+
$home_root = parse_url(home_url());
|
37 |
+
if (isset($home_root['path'])) {
|
38 |
+
$home_root = trailingslashit($home_root['path']);
|
39 |
+
} else {
|
40 |
+
$home_root = '/';
|
41 |
+
}
|
42 |
+
// create rules
|
43 |
+
$rules = "<IfModule mod_rewrite.c>\n";
|
44 |
+
$rules .= "RewriteEngine On\n";
|
45 |
+
$rules .= "RewriteRule ^$wpadmin_slug/?$ ".$home_root."wp-login.php [QSA,L]\n";
|
46 |
+
$rules .= "</IfModule>";
|
47 |
+
// write to .htaccess
|
48 |
+
insert_with_markers($home_path . '.htaccess', 'WPAdminURL', explode("\n", $rules));
|
49 |
+
}
|
50 |
+
} else if (isset($_POST['selection']) || (isset($_POST['selection']) && $_POST['selection'] == '' && $wpadmin_slug == '')) {
|
51 |
+
// remove rules if permalinks were enabled
|
52 |
+
$markerdata = explode("\n", implode('', file($home_path . '.htaccess')));
|
53 |
+
$found = false;
|
54 |
+
$newdata = '';
|
55 |
+
foreach ($markerdata as $line) {
|
56 |
+
if ($line == '# BEGIN WPAdminURL') {
|
57 |
+
$found = true;
|
58 |
+
}
|
59 |
+
if (!$found) {
|
60 |
+
$newdata .= "$line\n";
|
61 |
+
}
|
62 |
+
if ($line == '# END WPAdminURL') {
|
63 |
+
$found = false;
|
64 |
+
}
|
65 |
+
}
|
66 |
+
// write back
|
67 |
+
$f = @fopen($home_path . '.htaccess', 'w');
|
68 |
+
fwrite($f, $newdata);
|
69 |
+
}
|
70 |
+
|
71 |
+
// save to db
|
72 |
+
update_option('custom_wpadmin_slug', $wpadmin_slug);
|
73 |
+
|
74 |
+
// write rewrite rules right away
|
75 |
+
if ($wpadmin_slug != '') {
|
76 |
+
add_rewrite_rule($wpadmin_slug.'/?$', 'wp-login.php', 'top');
|
77 |
+
} else {
|
78 |
+
flush_rewrite_rules();
|
79 |
+
}
|
80 |
+
}
|
81 |
+
add_settings_field('custom_wpadmin_slug', 'WP-Admin slug', array($this, 'options_page'), 'permalink', 'optional', array('label_for' => 'custom_wpadmin_slug'));
|
82 |
+
register_setting('permalink', 'custom_wpadmin_slug', 'strval');
|
83 |
}
|
|
|
|
|
|
|
|
|
84 |
|
85 |
+
function options_page() {
|
86 |
+
?>
|
87 |
+
<input id="custom_wpadmin_slug" name="custom_wpadmin_slug" type="text" class="regular-text code" value="<?php echo get_option('custom_wpadmin_slug'); ?>">
|
88 |
+
<p class="howto">Allowed characters are a-z, 0-9, - and _</p>
|
89 |
+
<?php
|
90 |
+
}
|
91 |
|
92 |
+
// custom login url
|
93 |
+
function custom_login() {
|
94 |
+
// start session if doesn't exist
|
95 |
+
if (!session_id()) session_start();
|
96 |
+
$url = $this->get_url();
|
97 |
+
// see referal url by the web client
|
98 |
+
list($file, $arguments) = explode("?", $_SERVER['REQUEST_URI']);
|
99 |
+
// on localhost remove subdir
|
100 |
+
$file = ($url['rewrite_base']) ? implode("", explode("/" . $url['rewrite_base'], $file)) : $file;
|
101 |
+
|
102 |
+
if ("/wp-login.php?loggedout=true" == $file . "?" . $arguments) {
|
103 |
+
session_destroy();
|
104 |
+
header("location: " . $url['scheme'] . "://" . $url['domain'] . "/" . $url['rewrite_base']);
|
105 |
+
exit();
|
106 |
+
} else if ("action=logout" == substr($arguments, 0, 13)) {
|
107 |
+
unset($_SESSION['valid_login']);
|
108 |
+
} else if ('action=lostpassword' == $url['query'] || 'action=postpass' == $url['query']) {
|
109 |
+
// let user to this pages
|
110 |
+
} else if ($file == "/" . get_option('custom_wpadmin_slug') || $file == "/" . get_option('custom_wpadmin_slug') . "/") {
|
111 |
+
$_SESSION['valid_login'] = true;
|
112 |
+
} else if (isset($_SESSION['valid_login'])) {
|
113 |
+
// let them pass
|
114 |
+
} else {
|
115 |
+
header("location: " . $url['scheme'] . "://" . $url['domain'] . "/" . $url['rewrite_base']);
|
116 |
+
exit();
|
117 |
+
}
|
118 |
+
}
|
119 |
+
|
120 |
+
// return parsed url
|
121 |
+
function get_url() {
|
122 |
+
$url = array();
|
123 |
+
$url['scheme'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off") ? "https" : "http");
|
124 |
+
$url['domain'] = $_SERVER['HTTP_HOST'];
|
125 |
+
$url['port'] = (isset($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"]) ? $_SERVER["SERVER_PORT"] : "";
|
126 |
+
$url['rewrite_base'] = ($host = explode( (($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'], get_bloginfo('url') ) ) ? preg_replace("/^\//", "", implode("", $host)) : "";
|
127 |
+
$url['path'] = ($url['rewrite_base']) ? implode("", explode("/" . $url['rewrite_base'], $_SERVER["SCRIPT_NAME"])) : $_SERVER["SCRIPT_NAME"];
|
128 |
+
$url['query'] = $_SERVER['QUERY_STRING'];
|
129 |
+
return $url;
|
130 |
+
}
|
131 |
+
|
132 |
+
function check_login() {
|
133 |
+
// just chek if we are on the right place
|
134 |
+
if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php')) && (get_option('custom_wpadmin_slug') != false && get_option('custom_wpadmin_slug') != '')) {
|
135 |
+
|
136 |
+
// inlcude file.php
|
137 |
+
$DS = DIRECTORY_SEPARATOR;
|
138 |
+
if (file_exists(dirname(__FILE__) . $DS . '..' . $DS . '..' . $DS . '..' . $DS . 'wp-admin' . $DS . 'includes' . $DS . 'file.php')) {
|
139 |
+
include(dirname(__FILE__) . $DS . '..' . $DS . '..' . $DS . '..' . $DS . 'wp-admin' . $DS . 'includes' . $DS . 'file.php');
|
140 |
+
} else {
|
141 |
+
return; // if hierarchy is messed up, don't go further
|
142 |
+
}
|
143 |
+
|
144 |
+
// check if our plugin have written necesery line to .htaccess, sometimes WP doesn't write correctly so we don't want to disable login in that case
|
145 |
+
$markerdata = explode("\n", implode('', file(get_home_path() . '.htaccess')));
|
146 |
+
$found = false;
|
147 |
+
$url = $this->get_url();
|
148 |
+
foreach ($markerdata as $line) {
|
149 |
+
if (trim($line) == 'RewriteRule ^' . get_option('custom_wpadmin_slug') . '/?$ ' . ($url['rewrite_base'] ? '/'.$url['rewrite_base'] : '') . '/wp-login.php [QSA,L]') {
|
150 |
+
$found = true;
|
151 |
+
}
|
152 |
+
}
|
153 |
+
if ($found) {
|
154 |
+
$this->custom_login();
|
155 |
+
}
|
156 |
+
}
|
157 |
}
|
|
|
|
|
|
|
158 |
}
|
159 |
+
|
160 |
+
$hc_custom_wpadmin_url = new HC_CustomWPAdminURL();
|
161 |
+
|
162 |
+
// add hooks
|
163 |
+
add_filter('generate_rewrite_rules', array($hc_custom_wpadmin_url, 'rewrite_admin_url'));
|
164 |
+
add_action('admin_init', array($hc_custom_wpadmin_url, 'custom_admin_url'));
|
165 |
+
add_action('login_init', array($hc_custom_wpadmin_url, 'check_login'));
|
166 |
+
|
|
|
167 |
}
|
readme.txt
CHANGED
@@ -3,7 +3,7 @@ Contributors: SomeWebMedia
|
|
3 |
Tags: plugin, administration, admin, custom url, login, security, wp-admin, wp-login
|
4 |
Requires at least: 3.2
|
5 |
Tested up to: 3.5.1
|
6 |
-
Stable tag: 1.
|
7 |
License: GPLv2 or later
|
8 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
9 |
|
@@ -29,7 +29,7 @@ New field will be added to **Settings** -> **Permalinks** section called **WP-Ad
|
|
29 |
== Frequently Asked Questions ==
|
30 |
|
31 |
= Does this work with Multisite =
|
32 |
-
|
33 |
|
34 |
= What about the default Admin URLs? =
|
35 |
|
@@ -45,6 +45,9 @@ Be careful if using this plugin with other plugins which modify .htaccess file.
|
|
45 |
|
46 |
== Changelog ==
|
47 |
|
|
|
|
|
|
|
48 |
= 1.2 =
|
49 |
* If WordPress for some reason haven't applied rewrite rules to .htaccess, default wp-admin url will work
|
50 |
|
3 |
Tags: plugin, administration, admin, custom url, login, security, wp-admin, wp-login
|
4 |
Requires at least: 3.2
|
5 |
Tested up to: 3.5.1
|
6 |
+
Stable tag: 1.3
|
7 |
License: GPLv2 or later
|
8 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
9 |
|
29 |
== Frequently Asked Questions ==
|
30 |
|
31 |
= Does this work with Multisite =
|
32 |
+
We don't know, but we'll look into it soon!
|
33 |
|
34 |
= What about the default Admin URLs? =
|
35 |
|
45 |
|
46 |
== Changelog ==
|
47 |
|
48 |
+
= 1.3 =
|
49 |
+
* Fixed redirection bug of password protected pages
|
50 |
+
|
51 |
= 1.2 =
|
52 |
* If WordPress for some reason haven't applied rewrite rules to .htaccess, default wp-admin url will work
|
53 |
|