HC Custom WP-Admin URL - Version 1.3

Version Description

  • Fixed redirection bug of password protected pages
Download this release

Release Info

Developer SomeWebMedia
Plugin Icon 128x128 HC Custom WP-Admin URL
Version 1.3
Comparing to
See all releases

Code changes from version 1.2 to 1.3

Files changed (2) hide show
  1. hc-custom-wp-admin-url.php +151 -143
  2. 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.2
5
- Plugin URI: http://wordpress.org/extend/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_custom_wpadmin_url')) {
12
-
13
- class hc_custom_wpadmin_url {
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
- function custom_admin_url() {
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
- // save to db
67
- update_option('custom_wpadmin_slug', $wpadmin_slug);
68
-
69
- // write rewrite rules right away
70
- if ($wpadmin_slug != '') {
71
- add_rewrite_rule($wpadmin_slug.'/?$', 'wp-login.php', 'top');
72
- } else {
73
- flush_rewrite_rules();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- function options_page() {
81
- ?>
82
- <input id="custom_wpadmin_slug" name="custom_wpadmin_slug" type="text" class="regular-text code" value="<?php echo get_option('custom_wpadmin_slug'); ?>" />
83
- <p class="howto">Allowed characters are a-z, 0-9, - and _</p>
84
- <?php
85
- }
86
 
87
- // custom login url
88
- function custom_login() {
89
- // start session if doesn't exist
90
- if (!session_id()) session_start();
91
- $url = $this->get_url();
92
- // see referal url by the web client
93
- list($file, $arguments) = explode("?", $_SERVER['REQUEST_URI']);
94
- // on localhost remove subdir
95
- $file = ($url['rewrite_base']) ? implode("", explode("/" . $url['rewrite_base'], $file)) : $file;
96
-
97
- if("/wp-login.php?loggedout=true" == $file . "?" . $arguments) {
98
- session_destroy();
99
- header("location: " . $url['scheme'] . "://" . $url['domain'] . "/" . $url['rewrite_base']);
100
- exit();
101
- } else if ("action=logout" == substr($arguments, 0, 13)) {
102
- unset($_SESSION['valid_login']);
103
- } else if ("action=lostpassword" == $url['query']) {
104
- // let user to this page
105
- } else if ($file == "/" . get_option('custom_wpadmin_slug') || $file == "/" . get_option('custom_wpadmin_slug') . "/") {
106
- $_SESSION['valid_login'] = true;
107
- } else if (isset($_SESSION['valid_login'])) {
108
- // let them pass
109
- } else {
110
- header("location: " . $url['scheme'] . "://" . $url['domain'] . "/" . $url['rewrite_base']);
111
- exit();
112
- }
113
- }
114
-
115
- // return parsed url
116
- function get_url() {
117
- $url = array();
118
- $url['scheme'] = (($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http");
119
- $url['domain'] = $_SERVER['HTTP_HOST'];
120
- $url['port'] = ($_SERVER["SERVER_PORT"]) ? $_SERVER["SERVER_PORT"] : "";
121
- $url['rewrite_base'] = ($host = explode( (($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'], get_bloginfo('url') ) ) ? preg_replace("/^\//", "", implode("", $host)) : "";
122
- $url['path'] = ($url['rewrite_base']) ? implode("", explode("/" . $url['rewrite_base'], $_SERVER["SCRIPT_NAME"])) : $_SERVER["SCRIPT_NAME"];
123
- $url['query'] = $_SERVER['QUERY_STRING'];
124
- return $url;
125
- }
126
-
127
- function check_login() {
128
- // just chek if we are on the right place
129
- if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php')) && (get_option('custom_wpadmin_slug') != false && get_option('custom_wpadmin_slug') != '')) {
130
-
131
- // inlcude file.php
132
- $DS = DIRECTORY_SEPARATOR;
133
- if (file_exists(dirname(__FILE__) . $DS . '..' . $DS . '..' . $DS . '..' . $DS . 'wp-admin' . $DS . 'includes' . $DS . 'file.php'))
134
- include(dirname(__FILE__) . $DS . '..' . $DS . '..' . $DS . '..' . $DS . 'wp-admin' . $DS . 'includes' . $DS . 'file.php');
135
- else
136
- return; // if hierarchy is messed up, don't go further
137
-
138
- // 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
139
- $markerdata = explode("\n", implode('', file(get_home_path() . '.htaccess')));
140
- $found = false;
141
- $url = $this->get_url();
142
- foreach ($markerdata as $line) {
143
- if (trim($line) == 'RewriteRule ^' . get_option('custom_wpadmin_slug') . '/?$ ' . ($url['rewrite_base'] ? '/'.$url['rewrite_base'] : '') . '/wp-login.php [QSA,L]')
144
- $found = true;
 
 
 
 
 
 
 
145
  }
146
- if ($found)
147
- $this->custom_login();
148
- }
149
  }
150
- }
151
-
152
- $hc_custom_wpadmin_url = new hc_custom_wpadmin_url();
153
-
154
- // add hooks
155
- add_filter('generate_rewrite_rules', array($hc_custom_wpadmin_url, 'rewrite_admin_url'));
156
- add_action('admin_init', array($hc_custom_wpadmin_url, 'custom_admin_url'));
157
- add_action('login_init', array($hc_custom_wpadmin_url, 'check_login'));
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.2
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
- Not yet. Soon!
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