【问题标题】:Writing a class to pass add_action arguments in wordpress编写一个类以在 wordpress 中传递 add_action 参数
【发布时间】:2013-10-10 21:30:02
【问题描述】:

我正在使用写入变量$user_id 的钩子user_register。我想在函数内部调用这个钩子并将变量$pass从父函数传递给函数register_pass。在那里,$pass 和 user_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 调用传递到父函数等等。原则上这是有道理的;据我了解,您几乎可以在任何地方访问该类(有点像全局变量,但不那么不稳定)。但是,我只是不明白如何将我看到的示例应用于我的情况。我以前没有使用过课程,所以我想知道是否有人愿意引导我完成这个特定的应用程序。

【问题讨论】:

  • 您在此处和WordPress Development 中交叉发布相同的问题。你已经有反馈了。 Is cross-posting a question on multiple Stack Exchange sites permitted if the question is on-topic for each site?。答:没有。
  • @brasofilo 如果我的帖子违反了标准礼仪,我深表歉意。我认为这种情况是合适的,因为在我关于 Wordpress Answers 的问题中,我问的是一个广泛的“什么是完成 X 的技术”问题。在谷歌上搜索了一些之后,我在 Stackoverflow 上发现了类可以解决问题的信息,但不确定如何将它们应用于手头的情况。鉴于我在 stackoverflow 上找到了线索,我认为在那里问会很好。但是,作为这两个站点上的资深成员,您可能对此有更好的直觉;我以后会注意的
  • 好的,我看到了区别,关闭投票撤回。但是您的示例代码似乎无效,为什么您的函数中有一个函数?进一步阅读,你想要完成什么?为什么需要存储新注册用户的ID?
  • @brasofilo 哇,感谢您重新考虑;不过,我确实看到了您关于交叉发布的观点,并且对于像我这样的新手来说,记住这一点是有好处的。无论如何,对于技术方面的东西:我正在将 openmeetings(开源网络会议)与 wordpress 集成在一起,这样每个 wordpress 用户都被记录为 openmeetings 系统中的外部用户。函数中的函数允许我使用钩子来获取自动生成密码的纯文本,然后使用第二个钩子从用户的实际生成中捕获数据。清理完代码后,我重写了上面的问题。

标签: php wordpress class hook parameter-passing


【解决方案1】:

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

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

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。所有的解释都在代码cmets中。

<?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();
    }
}

【讨论】:

  • 非常感谢您这样拼写。我是 Web 开发领域的新手(如果你不知道的话),所以从长远来看,看到一个新概念特别有助于我摆脱困境。由于我为新用户使用随机生成的密码,我不得不添加另一个功能,但无论如何,它工作得很好。对于第一次尝试单例的其他人,我发现this youtube tutorial 是对上述信息的一个非常有用的补充。再次感谢!
【解决方案2】:

你不需要函数中的函数;正如其他人所说,您可以通过类来完成此操作。

我建议阅读单例模式。基本上,您需要一个能够保证仅存在一个自身实例的对象。因此,假设您用来存储新用户信息的类称为 UserCredentials。一旦它被实现为单例,以及适当的用户/通行证的 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'] 来完成,但是这样更简洁。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-23
    • 2012-09-17
    • 2014-02-03
    • 2015-07-17
    • 2015-02-11
    • 2011-02-20
    • 1970-01-01
    • 2011-04-05
    相关资源
    最近更新 更多