1

Below is a trimmed down version of my plugin, to keep it simple. My setup function is being called, but when I go to the login page, it's not dying like it should. I had the plugin working earlier, however I'm trying to re-write it into a class structure to make it more self-contained.

if ( ! function_exists('add_action') ) {
   header('Status: 403 Forbidden');
   header('HTTP/1.1 403 Forbidden');
}

add_action('plugins_loaded', array('My_Authentication', 'setup') );
if ( ! class_exists('My_Authentication') ) {
    class My_Authentication {
        public function setup() {
            add_action('wp_login', array(&$this, 'no_auth_allowed') );
            add_action('wp_logout', array(&$this, 'no_auth_allowed') );
        }
        public function no_auth_allowed() {
            wp_die('no authentication for you!');
        }
    }
}

To clarify on what my question is, why isn't the no_auth_allowed() function being called when I go to the wp-login.php page?

  • If the syntax error on the add_action('wp_logout', line is corrected does it work? – s_ha_dum Oct 30 '15 at 20:05
  • See the two class examples on add_action. You can't call a class method like that. – Milo Oct 30 '15 at 20:05
  • I thought so too @Milo, but I pasted it into a mu-plugin file and the setup() method fires. – s_ha_dum Oct 30 '15 at 20:12
  • @s_ha_dum thanks, I fixed the typo, but the problem still persists. – Brady Pacha Oct 30 '15 at 20:17
  • I started by looking at this to try and understand how to properly do the add_actions. – Brady Pacha Oct 30 '15 at 20:19
2

I created a class instance and referenced that in add_action, which is moved below where the instance is created:

if ( ! function_exists('add_action') ) {
   header('Status: 403 Forbidden');
   header('HTTP/1.1 403 Forbidden');
}

if ( ! class_exists('My_Authentication') ) {
    class My_Authentication {
        public function setup() {
            add_action('wp_login', array(&$this, 'no_auth_allowed') );
            add_action('wp_logout', array(&$this, 'no_auth_allowed') );
        }
        public function no_auth_allowed() {
            wp_die('no authentication for you!');
        }
    }
    $my_auth = new My_Authentication;
}
add_action('plugins_loaded', array($my_auth, 'setup') );
  • It's still doing the same thing for me. This is causing your wp-login.php page to die? – Brady Pacha Oct 30 '15 at 21:03
  • you have to try to log in, the wp_login action fires when in the process of logging in, not just visiting the login page. – Milo Oct 30 '15 at 21:08
  • weird, I could've sworn it triggered when the page loaded. I'll just have to find a new hook for my needs. Thanks for the help! – Brady Pacha Oct 30 '15 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.