2013-10-10 42 views
1

我正在使用鉤子user_register寫入變量$user_id。我想在函數內部調用這個鉤子,並將父函數變量$pass傳遞給函數register_pass。一旦出現,$passuser_id將按照其協議發送給第三方軟件。編寫一個類,在wordpress中傳遞add_action參數

關鍵目標是$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身邊召喚變量傳給父母等功能。原則上這是有道理的;據我所知,你幾乎可以在任何地方訪問這個類(有點像全局變量,但不那麼危險)。但是,我只是不理解如何將我見過的例子應用於我的情況。我之前沒有使用過課程,所以我想知道是否有人願意帶我走過這個特定的應用程序。

+0

您在此處和[wordpress.se]中交叉發佈相同的問題。你已經在那裏得到反饋。 [如果問題是針對每個網站的主題,是否允許在多個Stack Exchange站點上交叉發佈問題?](http://meta.stackexchange.com/q/64068/185667)。答:不。 – brasofilo

+0

@brasofilo如果我的帖子違反標準禮節,我很抱歉。我認爲這種情況是合適的,因爲在我關於Wordpress Answers的問題中,我提出了一個廣泛的「什麼是技術來完成X」的問題。谷歌搜索後,我發現在Stackoverflow的信息,類可以做的伎倆,但不知道如何將它們應用到手頭的情況。考慮到我在stackoverflow上找到了這些線索,我認爲在那裏問問題會很好。但是,作爲這兩個網站上比我更高級的成員,你可能對此有更好的直覺;我會在將來留意 – neanderslob

+0

好吧,我看到了差異,收到了近距離投票。但是你的示例代碼似乎是無效的,爲什麼你有一個函數內的函數?並進一步閱讀,你想完成什麼?爲什麼你需要存儲新註冊用戶的ID? – brasofilo

回答

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_passwordsuser_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(); 
    } 
} 
+0

非常感謝這樣拼寫出來。我是網絡開發領域的新手(如果你不能說的話),所以看到一個新的概念能夠幫助我從長遠來看幫助我。由於我爲新用戶使用隨機生成的密碼,因此我不得不添加另一個函數,它工作得很好。對於其他人第一次在單身人士身上採取了一些措施,我發現[此YouTube教程](http://www.youtube.com/watch?v=UPfdb5y2SOI)對上述信息非常有幫助。再次感謝! – neanderslob

2

你不需要函數內的函數;正如其他人所說的,你可以通過課程來完成。

我建議閱讀Singleton模式。基本上,你需要一個保證只存在一個自身實例的對象。因此,假設您用來存儲新用戶信息的類稱爲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完成[「什麼」] ,但這是更清潔。

相關問題