0

I have a clean installation of Wordpress, without any extra plugins installed (only for testing purpose).

I have created a test-plugin and declared the following class:

<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
class Test_Plugin
{
    public function __construct()
    {
        add_action('show_user_profile', array($this,'add_user_profile_inputs'));
        add_action('edit_user_profile', array($this,'add_user_profile_inputs'));
    }

    public function add_user_profile_inputs( $user )
    {
?>
        <h3>New input fields area</h3>
<?php
    }
}
?>

The problem with this code is that the hooks will not fire. When I navigate to the Edit User or Your Profile pages, there is nothing changed.

If I write the code outside the class, the hooks will fire and the Edit User and Your Profile pages are changed with the addition of the new text:

<?php    
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    add_action('show_user_profile', 'add_user_profile_inputs');
    add_action('edit_user_profile', 'add_user_profile_inputs');

    function add_user_profile_inputs( $user )
    {
        ?>
        <h3>New input fields area</h3>
        <?php
    }
?>

I don't understand why, if someone can please explain this. I would like to keep everything inside the Class. Is this possible? and how? Does this behavior apply to other hooks?

Thank you.

3

Where are you calling the class? A class by itself will do nothing unless it's used.

So, add at the very end $activate = new Test_Plugin();

  • Indeed. Rookie mistake! – Alex Alex Jan 26 '18 at 12:45

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.