编写一个类来在wordpress中传递add_action参数

时间:2013-10-10 21:30:03

标签: php wordpress class hook parameter-passing

我正在使用写入变量user_register的钩子$user_id。我想在函数内部调用此钩子,并将变量$pass从父函数传递给函数register_pass。在那里,$passuser_id将根据其协议发送给第三方软件。

关键目标是$pass$user_id同时放入通过SOAP发送的同一个数组中。

function random_pass_save($pass){

    add_action( 'user_register', 'regiser_pass')
    function register_pass($user_id){
        .
        .
        .
        code that processes both the $user_id, AND $pass and sends it to another piece of software
        .
        .
        .
    }

}

现在我已经对此进行了一些阅读,并且我一直建议人们使用类将变量从add_action调用传递给父函数等等。原则上这是有道理的;据我所知,你几乎可以在任何地方访问这个类(有点像全局变量,但不那么不稳定)。但是,我只是不明白如何将我见过的例子应用到我的情况中。我之前没有使用过课程,所以我想知道是否有人能够引导我完成这个特定的应用程序。

2 个答案:

答案 0 :(得分:2)

您不需要函数内的函数;正如其他人所说,你可以通过课程来实现这一目标。

我建议阅读Singleton模式。基本上,您需要一个保证只存在一个自身实例的对象。因此,假设您用于存储新用户信息的类称为UserCredentials。一旦它被实现为Singleton,以及适当的用户/传递的getter / setter,你可以这样做:

function random_pass_save($pass) {
    UserCredentials::getSingleton()->savePass($pass);
}

add_action( 'user_register', 'regiser_pass')
function register_pass($user_id){
    $pass = UserCredentials::getSingleton()->getPass();
    // $user_id and $pass are now both in scope
}

正如您所推断的那样,通过存储到$ GLOBALS ['whatever']可以实现同样的目的,但这更清晰。

答案 1 :(得分:2)

如果我理解这一点,您只需在创建用户时调用自定义函数。这些函数不必嵌套,如果这是一个类也没关系。

添加钩子并在其回调中,调用函数传递数据:

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
}

这个问题可以通过check_passwords之前发生的钩子user_register解决,它包含用户名和密码。

但好吧,让我们在一个班级里做,它基于this skeleton that I always use。所有解释都在代码注释中。

<?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();
    }
}
相关问题