如果我理解這個權利,您只需在創建用戶時調用自定義函數。這些函數不一定是嵌套的,如果這是一個類,也沒關係。
添加鉤子和其回調裏面,你叫你的函數傳遞數據:
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
之前發生,同時包含用戶名和亟待解決密碼。
但是好的,我們來做一個Class,它基於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();
}
}
您在此處和[wordpress.se]中交叉發佈相同的問題。你已經在那裏得到反饋。 [如果問題是針對每個網站的主題,是否允許在多個Stack Exchange站點上交叉發佈問題?](http://meta.stackexchange.com/q/64068/185667)。答:不。 – brasofilo
@brasofilo如果我的帖子違反標準禮節,我很抱歉。我認爲這種情況是合適的,因爲在我關於Wordpress Answers的問題中,我提出了一個廣泛的「什麼是技術來完成X」的問題。谷歌搜索後,我發現在Stackoverflow的信息,類可以做的伎倆,但不知道如何將它們應用到手頭的情況。考慮到我在stackoverflow上找到了這些線索,我認爲在那裏問問題會很好。但是,作爲這兩個網站上比我更高級的成員,你可能對此有更好的直覺;我會在將來留意 – neanderslob
好吧,我看到了差異,收到了近距離投票。但是你的示例代碼似乎是無效的,爲什麼你有一個函數內的函數?並進一步閱讀,你想完成什麼?爲什麼你需要存儲新註冊用戶的ID? – brasofilo