Writing a class to pass add_action arguments in wordpress

Solution:1

If I’m understanding this right, you simply need to call a custom function when a user is created. The functions don’t have to be nested and it doesn’t matter if this is a class or not.

Add the hook and inside its callback, you call your function passing the data:

add_action( 'user_register', 'register_pass' );

function register_pass( $user_id )
{
    $password = get_the_password_somehow_with( $user_id ); // <--- problem
    random_pass_save( $user_id, $password );
}

function random_pass_save( $user_id, $password )
{
    // do your thing
}

That problem can be solved with the hook check_passwords, that happens before user_register and that contains both the user name and the password.

But ok, let’s do it in a Class, it’s based in this skeleton that I always use. All the explanation is in the code comments.

<?php
/**
 * Plugin Name: Testing a Singleton
 * Plugin URI:  http://stackoverflow.com/questions/19306582
 * Version:     0.1
 * Author:      brasofilo 
 * Author URI:  http://wordpress.stackexchange.com/users/12615/brasofilo
 */

# Start the plugin in a safe point
add_action( 
    'plugins_loaded',
    array( B5F_Demo_SO_19306582::get_instance(), 'plugin_setup' )
    );

class B5F_Demo_SO_19306582 
{
    # Singleton
    protected static $instance = NULL;  

    # Store new user data (id, name, password)
    private $user_data;  

    # Leave empty
    public function __construct() {}

    # Singleton
    public static function get_instance() 
    {
        NULL === self::$instance and self::$instance = new self;
        return self::$instance;
    }

    # Start our action hooks
    public function plugin_setup() 
    {
        add_action( 'check_passwords', array( $this, 'check_pass' ), 10, 3 );
        add_action( 'user_register',  array( $this, 'user_register_action' ) );
    }

    # Confirms that passwords match when registering a new user
    public function check_pass( $user, $pass1, $pass2 ) 
    {   
        // They match, let's add data to our private property
        if( $pass1 == $pass2 )
            $this->user_data = array( 'user' => $user, 'pass' => $pass1 );
    }

    # Add the ID of a newly registered user to our private property
    # Now we have all the data and can call our final method
    public function user_register_action( $user_id )
    {
        $this->user_data['user_id'] = $user_id;
        $this->external_save();
    }

    # In this method we do our thing with all the information 
    # previously stored in the private property
    public function external_save()
    {
        var_dump( $this->user_data );
        die();
    }
}

Solution:2

The parameter needs to be passed to the callback function in the wp_schedule_single_event function, not the add_action function.

Try this:

add_action('z_purge_products_cache', array($this, 'purge_products_cache'));

Then schedule the event, putting the parameter in an array:

wp_schedule_single_event(time() + 20, 'z_purge_products_cache', array($asins_r));

Your purge_products_cache function will be called with parameter $asins_r.