title: Issues with PHP classes and WordPress
body: <p>I am currently <em>trying</em> to add new options to the <a href="https://wordpress.org/plugins/rename-wp-login/" rel="nofollow">Rename wp-login.php</a> because it is barely being updated. The owner also allowed me to add new options to it.</p>
  
  <p>The plugin functionality allows you to 'rename' <code>/wp-admin/</code> and <code>wp-login.php</code> to a different location. I barely have any knowledge about PHP classes and, you guessed it, the whole plugin is basically created using PHP classes.</p>
  
  <p>What I am trying to do:</p>
  
  <ol>
  <li><p>When the field is empty, revert everything to <code>/wp-admin/</code>. Basically a disable option; I've got that, but when the field is left blank and you'll save it, it saves the value which was entered before. That's a problem; It should be empty when you save it blank.</p></li>
  <li><p>Currently the field is in the "Permalinks" section of the settings menu, but I want to put the option in a custom page.</p></li>
  </ol>
  
  <p>Also, how does this plugin saves the value of the field? I can't find anything, not even a <code>register_setting</code> function.</p>
  
  <p>When I say 'the field', I'm talking about this field: <code>&lt;input id="rwl-page-input" type="text" name="rwl_page" value="' . $this-&gt;new_login_slug()  . '"&gt;</code></p>
  
  <hr>
  
  <p>Code is as follows:</p>
  
  <pre><code>&lt;?php
  if ( defined( 'ABSPATH' ) &amp;&amp; ! class_exists( 'Rename_WP_Login' ) ) {
  
      class Rename_WP_Login {
          private $wp_login_php;
  
          private function basename() {
              return plugin_basename( __FILE__ );
          }
  
          private function path() {
              return trailingslashit( dirname( __FILE__ ) );
          }
  
          private function use_trailing_slashes() {
              return '/' === substr( get_option( 'permalink_structure' ), -1, 1 );
          }
  
          private function user_trailingslashit( $string ) {
              return $this-&gt;use_trailing_slashes() ? trailingslashit( $string ) : untrailingslashit( $string );
          }
  
          private function wp_template_loader() {
              global $pagenow;
  
              $pagenow = 'index.php';
  
              if ( ! defined( 'WP_USE_THEMES' ) ) {
                  define( 'WP_USE_THEMES', true );
              }
  
              wp();
  
              if ( $_SERVER['REQUEST_URI'] === $this-&gt;user_trailingslashit( str_repeat( '-/', 10 ) ) ) {
                  $_SERVER['REQUEST_URI'] = $this-&gt;user_trailingslashit( '/wp-login-php/' );
              }
  
              require_once ABSPATH . WPINC . '/template-loader.php';
  
              die;
          }
  
          private function new_login_slug() {
              if (
                  ( $slug = get_option( 'rwl_page' ) ) || (
                      is_multisite() &amp;&amp;
                      is_plugin_active_for_network( $this-&gt;basename() ) &amp;&amp;
                      ( $slug = get_site_option( 'rwl_page', 'login' ) )
                  ) ||
                  ( $slug = 'login' )
              ) {
                  return $slug;
              }
          }
  
          public function new_login_url( $scheme = null ) {
              if ( get_option( 'permalink_structure' ) ) {
                  return $this-&gt;user_trailingslashit( home_url( '/', $scheme ) . $this-&gt;new_login_slug() );
              } else {
                  return home_url( '/', $scheme ) . '?' . $this-&gt;new_login_slug();
              }
          }
  
          public function __construct() {
              register_activation_hook( $this-&gt;basename(), array( $this, 'activate' ) );
              register_uninstall_hook( $this-&gt;basename(), array( 'Rename_WP_Login', 'uninstall' ) );
  
              add_action( 'admin_init', array( $this, 'admin_init' ) );
              add_action( 'admin_notices', array( $this, 'admin_notices' ) );
              add_action( 'network_admin_notices', array( $this, 'admin_notices' ) );
  
              if ( is_multisite() &amp;&amp; ! function_exists( 'is_plugin_active_for_network' ) ) {
                  require_once ABSPATH . '/wp-admin/includes/plugin.php';
              }
  
              add_filter( 'plugin_action_links_' . $this-&gt;basename(), array( $this, 'plugin_action_links' ) );
  
              if ( is_multisite() &amp;&amp; is_plugin_active_for_network( $this-&gt;basename() ) ) {
                  add_filter( 'network_admin_plugin_action_links_' . $this-&gt;basename(), array( $this, 'plugin_action_links' ) );
  
                  add_action( 'wpmu_options', array( $this, 'wpmu_options' ) );
                  add_action( 'update_wpmu_options', array( $this, 'update_wpmu_options' ) );
              }
  
              add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 1 );
              add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
  
              add_filter( 'site_url', array( $this, 'site_url' ), 10, 4 );
              add_filter( 'network_site_url', array( $this, 'network_site_url' ), 10, 3 );
              add_filter( 'wp_redirect', array( $this, 'wp_redirect' ), 10, 2 );
  
              add_filter( 'site_option_welcome_email', array( $this, 'welcome_email' ) );
  
              remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
          }
  
          public function activate() {
              add_option( 'rwl_redirect', '1' );
          }
  
          public static function uninstall() {
              global $wpdb;
  
              if ( is_multisite() ) {
                  $blogs = $wpdb-&gt;get_col( "SELECT blog_id FROM {$wpdb-&gt;blogs}" );
  
                  if ( $blogs ) {
                      foreach ( $blogs as $blog ) {
                          switch_to_blog( $blog );
                          delete_option( 'rwl_page' );
                      }
  
                      restore_current_blog();
                  }
  
                  delete_site_option( 'rwl_page' );
              } else {
                  delete_option( 'rwl_page' );
              }
          }
  
          public function wpmu_options() {
              echo (
                  '&lt;h3&gt;' .
                      _x( 'Rename wp-login.php', 'Text string for settings page', 'rename-wp-login' ) .
                  '&lt;/h3&gt;' .
                  '&lt;p&gt;' .
                      __( 'This option allows you to set a networkwide default, which can be overridden by individual sites. Simply go to to the site’s permalink settings to change the url.', 'rename-wp-login' ) .
                  '&lt;/p&gt;' .
                  '&lt;table class="form-table"&gt;' .
                      '&lt;tr valign="top"&gt;' .
                          '&lt;th scope="row"&gt;' .
                              __( 'Networkwide default', 'rename-wp-login' ) .
                          '&lt;/th&gt;' .
                          '&lt;td&gt;' .
                              '&lt;input id="rwl-page-input" type="text" name="rwl_page" value="' . get_site_option( 'rwl_page', 'login' )  . '"&gt;' .
                          '&lt;/td&gt;' .
                      '&lt;/tr&gt;' .
                  '&lt;/table&gt;'
              );
          }
  
          public function update_wpmu_options() {
              if (
                  ( $rwl_page = sanitize_title_with_dashes( $_POST['rwl_page'] ) ) &amp;&amp;
                  strpos( $rwl_page, 'wp-login' ) === false &amp;&amp;
                  ! in_array( $rwl_page, $this-&gt;forbidden_slugs() )
              ) {
                  update_site_option( 'rwl_page', $rwl_page );
              }
          }
  
          public function admin_init() {
              global $pagenow;
  
              add_settings_section(
                  'rename-wp-login-section',
                  _x( 'Rename wp-login.php', 'Text string for settings page', 'rename-wp-login' ),
                  array( $this, 'rwl_section_desc' ),
                  'permalink'
              );
  
              add_settings_field(
                  'rwl-page',
                  '&lt;label for="rwl-page"&gt;' . __( 'Login url', 'rename-wp-login' ) . '&lt;/label&gt;',
                  array( $this, 'rwl_page_input' ),
                  'permalink',
                  'rename-wp-login-section'
              );
  
              if ( isset( $_POST['rwl_page'] ) &amp;&amp; $pagenow === 'options-permalink.php' ) {
                  if (
                      ( $rwl_page = sanitize_title_with_dashes( $_POST['rwl_page'] ) ) &amp;&amp;
                      strpos( $rwl_page, 'wp-login' ) === false &amp;&amp;
                      ! in_array( $rwl_page, $this-&gt;forbidden_slugs() )
                  ) {
                      if ( is_multisite() &amp;&amp; $rwl_page === get_site_option( 'rwl_page', 'login' ) ) {
                          delete_option( 'rwl_page' );
                      } else {
                          update_option( 'rwl_page', $rwl_page );
                      }
                  }
              }
  
              if ( get_option( 'rwl_redirect' ) ) {
                  delete_option( 'rwl_redirect' );
  
                  if ( is_multisite() &amp;&amp; is_super_admin() &amp;&amp; is_plugin_active_for_network( $this-&gt;basename() ) ) {
                      $redirect = network_admin_url( 'settings.php#rwl-page-input' );
                  } else {
                      $redirect = admin_url( 'options-permalink.php#rwl-page-input' );
                  }
  
                  wp_safe_redirect( $redirect );
  
                  die;
              }
          }
  
          public function rwl_section_desc() {
              if ( is_multisite() &amp;&amp; is_super_admin() &amp;&amp; is_plugin_active_for_network( $this-&gt;basename() ) ) {
                  echo (
                      '&lt;p&gt;' .
                          sprintf(
                              __( 'To set a networkwide default, go to %s.', 'rename-wp-login' ),
                              '&lt;a href="' . esc_url( network_admin_url( 'settings.php#rwl-page-input' ) ) . '"&gt;' .
                                  __( 'Network Settings', 'rename-wp-login' ) .
                              '&lt;/a&gt;'
                          ) .
                      '&lt;/p&gt;'
                  );
              }
          }
  
          public function rwl_page_input() {
              if ( get_option( 'permalink_structure' ) ) {
                  echo '&lt;code&gt;' . trailingslashit( home_url() ) . '&lt;/code&gt; &lt;input id="rwl-page-input" type="text" name="rwl_page" value="' . $this-&gt;new_login_slug()  . '"&gt;' . ( $this-&gt;use_trailing_slashes() ? ' &lt;code&gt;/&lt;/code&gt;' : '' );
              } else {
                  echo '&lt;code&gt;' . trailingslashit( home_url() ) . '?&lt;/code&gt; &lt;input id="rwl-page-input" type="text" name="rwl_page" value="' . $this-&gt;new_login_slug()  . '"&gt;';
              }
          }
  
          public function admin_notices() {
              global $pagenow;
  
              if ( ! is_network_admin() &amp;&amp; $pagenow === 'options-permalink.php' &amp;&amp; isset( $_GET['settings-updated'] ) ) {
                  echo '&lt;div class="notice notice-success is-dismissible"&gt;&lt;p&gt;' . sprintf( __( 'Your login page is now here: %s. Bookmark this page!', 'rename-wp-login' ), '&lt;strong&gt;&lt;a href="' . $this-&gt;new_login_url() . '"&gt;' . $this-&gt;new_login_url() . '&lt;/a&gt;&lt;/strong&gt;' ) . '&lt;/p&gt;&lt;/div&gt;';
              }
          }
  
          public function plugin_action_links( $links ) {
              if ( is_network_admin() &amp;&amp; is_plugin_active_for_network( $this-&gt;basename() ) ) {
                  array_unshift( $links,
                      '&lt;a href="' . esc_url( network_admin_url( 'settings.php#rwl-page-input' ) ) . '"&gt;' .
                          __( 'Settings', 'rename-wp-login' ) .
                      '&lt;/a&gt;'
                  );
              } elseif ( ! is_network_admin() ) {
                  array_unshift( $links,
                      '&lt;a href="' . esc_url( admin_url( 'options-permalink.php#rwl-page-input' ) ) . '"&gt;' .
                          __( 'Settings', 'rename-wp-login' ) .
                      '&lt;/a&gt;'
                  );
              }
  
              return $links;
          }
  
          public function plugins_loaded() {
              global $pagenow;
  
              load_plugin_textdomain( 'rename-wp-login' );
  
              if (
                  ! is_multisite() &amp;&amp; (
                      strpos( $_SERVER['REQUEST_URI'], 'wp-signup' ) !== false ||
                      strpos( $_SERVER['REQUEST_URI'], 'wp-activate' ) !== false
                  )
              ) {
                  wp_die( __( 'This feature is not enabled.', 'rename-wp-login' ), '', array( 'response' =&gt; 403 ) );
              }
  
              $request = parse_url( $_SERVER['REQUEST_URI'] );
  
              if ( (
                      strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ||
                      untrailingslashit( $request['path'] ) === site_url( 'wp-login', 'relative' )
                  ) &amp;&amp;
                  ! is_admin()
              ) {
                  $this-&gt;wp_login_php = true;
                  $_SERVER['REQUEST_URI'] = $this-&gt;user_trailingslashit( '/' . str_repeat( '-/', 10 ) );
                  $pagenow = 'index.php';
              } elseif (
                  untrailingslashit( $request['path'] ) === home_url( $this-&gt;new_login_slug(), 'relative' ) || (
                      ! get_option( 'permalink_structure' ) &amp;&amp;
                      isset( $_GET[$this-&gt;new_login_slug()] ) &amp;&amp;
                      empty( $_GET[$this-&gt;new_login_slug()] )
              ) ) {
                  $pagenow = 'wp-login.php';
              }
          }
  
          public function wp_loaded() {
              global $pagenow;
  
              if ( is_admin() &amp;&amp; ! is_user_logged_in() &amp;&amp; ! defined( 'DOING_AJAX' ) ) {
                  wp_die( __( 'You must log in to access the admin area.', 'rename-wp-login' ), '', array( 'response' =&gt; 403 ) );
              }
  
              $request = parse_url( $_SERVER['REQUEST_URI'] );
  
              if (
                  $pagenow === 'wp-login.php' &amp;&amp;
                  $request['path'] !== $this-&gt;user_trailingslashit( $request['path'] ) &amp;&amp;
                  get_option( 'permalink_structure' )
              ) {
                  wp_safe_redirect( $this-&gt;user_trailingslashit( $this-&gt;new_login_url() ) . ( ! empty( $_SERVER['QUERY_STRING'] ) ? '?' . $_SERVER['QUERY_STRING'] : '' ) );
                  die;
              } elseif ( $this-&gt;wp_login_php ) {
                  if (
                      ( $referer = wp_get_referer() ) &amp;&amp;
                      strpos( $referer, 'wp-activate.php' ) !== false &amp;&amp;
                      ( $referer = parse_url( $referer ) ) &amp;&amp;
                      ! empty( $referer['query'] )
                  ) {
                      parse_str( $referer['query'], $referer );
  
                      if (
                          ! empty( $referer['key'] ) &amp;&amp;
                          ( $result = wpmu_activate_signup( $referer['key'] ) ) &amp;&amp;
                          is_wp_error( $result ) &amp;&amp; (
                              $result-&gt;get_error_code() === 'already_active' ||
                              $result-&gt;get_error_code() === 'blog_taken'
                      ) ) {
                          wp_safe_redirect( $this-&gt;new_login_url() . ( ! empty( $_SERVER['QUERY_STRING'] ) ? '?' . $_SERVER['QUERY_STRING'] : '' ) );
                          die;
                      }
                  }
  
                  $this-&gt;wp_template_loader();
              } elseif ( $pagenow === 'wp-login.php' ) {
                  global $error, $interim_login, $action, $user_login;
  
                  @require_once ABSPATH . 'wp-login.php';
  
                  die;
              }
          }
  
          public function site_url( $url, $path, $scheme, $blog_id ) {
              return $this-&gt;filter_wp_login_php( $url, $scheme );
          }
  
          public function network_site_url( $url, $path, $scheme ) {
              return $this-&gt;filter_wp_login_php( $url, $scheme );
          }
  
          public function wp_redirect( $location, $status ) {
              return $this-&gt;filter_wp_login_php( $location );
          }
  
          public function filter_wp_login_php( $url, $scheme = null ) {
              if ( strpos( $url, 'wp-login.php' ) !== false ) {
                  if ( is_ssl() ) {
                      $scheme = 'https';
                  }
  
                  $args = explode( '?', $url );
  
                  if ( isset( $args[1] ) ) {
                      parse_str( $args[1], $args );
                      $url = add_query_arg( $args, $this-&gt;new_login_url( $scheme ) );
                  } else {
                      $url = $this-&gt;new_login_url( $scheme );
                  }
              }
  
              return $url;
          }
  
          public function welcome_email( $value ) {
              return str_replace( 'wp-login.php', trailingslashit( get_site_option( 'rwl_page', 'login' ) ), $value );
          }
  
          public function forbidden_slugs() {
              $wp = new WP;
              return array_merge( $wp-&gt;public_query_vars, $wp-&gt;private_query_vars );
          }
      }
  
      new Rename_WP_Login;
  }
  </code></pre>
  
  <p>I would really, really appreciate it if anyone can help me out with this!</p>