Wait a result before enqueue

时间:2016-04-27 作者:Nicolas Frbezar

假设我们有:

class AwesomeClass{
    
    private storedResult;

    private __construct(){
        add_action( \'set_logged_in_cookie\', array($this, \'a\'),10,1);
        add_action(\'wp_enqueue_scripts\', array($this, \'b\')); 
    }

    Function a(p1){
        //some interesting process with p1
        // UPDATE $this->storedResult
    }

    Function b(){
        //some interesting process
        // USAGE OF $this->storedResult
    }
}
如何确保在函数a完成时执行函数b(从而更新storedResult)?

编辑,我已经尝试过了:

class IC_Chat {

public $token = null;

public function __construct()
{
    add_action( \'set_logged_in_cookie\', array($this, \'ic_askForToken\'),10,1);
}

// Triggered when the cookie for intrachat is set or if there were an authentification
public function ic_load_scripts() {     
    wp_register_script( \'socket.IO-client\', plugins_url(\'../js/socket.io.js\', __FILE__), array( \'jquery\' ), \'\', true ); // Enqueue thescript in the footer of the page
    wp_register_script( \'ic-client-script\', plugins_url(\'../js/ic-client-script.js\', __FILE__), array( \'jquery\', \'socket.IO-client\' ), \'\', true ); // Enqueue thescript in the footer of the page
    wp_register_style(\'ic_css\', plugins_url(\'../css/style.css\', __FILE__));
    
    wp_enqueue_script(\'jquery\');
    wp_enqueue_style(\'ic_css\');
    wp_enqueue_script(\'socket.IO-client\');
    wp_enqueue_script(\'ic-client-script\');
    wp_localize_script(\'ic-client-script\', \'wp_include_vars\', array(
            \'token\' => $this->token //$_COOKIE[$this->cookieIcLoggedInName]
        )
    );
}

// Triggered only when the user connect
public function ic_askForToken($cookie){

    //HTTP post request who work
    
    if ( is_wp_error($resp) ) {
        $error_message = $resp->get_error_message();
        echo $resp->get_error_code().\' \'.$error_message;
    } else {
        switch($resp[\'response\'][\'code\'] ){
            case 200: {
                $token = json_decode( $resp[\'body\'], true );
                setcookie($this->cookieIcLoggedInName, $token["token"], 0, SITECOOKIEPATH, COOKIE_DOMAIN, 1, $secure);
                $this->token = $token[\'token\'];

                //Launch the script
                add_action(\'wp_enqueue_scripts\', array($this,\'ic_load_scripts\')); // chargement du script
                break;
            }
            .........
            ........
            ....
        }
    }
}   
}

但它不会工作,我的令牌值仍然是null,而不是http请求获得的值。

事实上,通过这样做,我的脚本将不会加载。

1 个回复
SO网友:Nabeel Khan

有多种方法可以做到这一点,例如,将方法b的add\\u操作移动到

class AwesomeClass{

    private storedResult;

    private __construct(){
        add_action( \'set_logged_in_cookie\', array($this, \'a\'),10,1);
    }

    Function a(p1){
        //some interesting process with p1
        // UPDATE $this->storedResult
        add_action(\'wp_enqueue_scripts\', array($this, \'b\')); 
    }

    Function b(){
        //some interesting process
        // USAGE OF $this->storedResult
    }
}

相关推荐

Use Post ID in functions.php

我的functions.php. 它工作得很好:当预订时,它会给我发电子邮件。易于理解的然而,我希望它能给我发送该预订的post ID,而不仅仅是“你好,世界!”消息帮助function add_to_system() { // get post meta // $email = get_post_meta(\'\',$post_id) // $post_id = get_queried_object_id(); mail(\'michael.mc