将另一个函数的值赋给同一个php类中的其他函数

时间:2017-11-22 11:28:27

标签: php wordpress visual-composer

我开发了visual composer自定义元素,我想从elementMapping函数赋予shortcode $ atts [" elementcolor"]的值到elementCss函数我该怎么做

请让我知道如何将elementMapping函数的短代码$ atts [" elementcolor"]的值赋给elementCss函数

public static void main(String[] args) {
     printNumber(1);
}

public static void printNumber(int n) {
    if(n!=4) {
         printNumber(n + 1);
         System.out.print(n);
    }
}

1 个答案:

答案 0 :(得分:0)

使用私有变量存储短代码值:

class newelement extends WPBakeryShortCode {
    private $atts; 
    # Construct ----------------
    function __construct() {
        add_action( 'init', [ $this, 'elementMapping' ] );
        add_shortcode( 'element_Shortcode', [ $this, 'elementShortcode' ] );
        add_action( 'wp_enqueue_scripts', [ $this, 'elementCss' ]);
    }
    # Fields -------------------
    public function elementMapping() {
        if ( !defined( 'WPB_VC_VERSION' ) ) {
            return;
        }
        vc_map(
            [
                "base"              => "elementShortcode",
                "params" => [
                    [
                        "type" => "colorpicker",
                        "param_name" => "elementcolor"
                    ],
                ],
            ]
        );
    }
    # Output Code ---------------------------------
    public function elementShortcode () {
        $atts = shortcode_atts([
            "elementcolor" => "",
        ], $atts);
        $this->atts = $atts;
        extract($atts);
        ob_start();
        ?>

        <?php
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    }
    # css ---------------------------------
    public function elementCss() {

        $elementcolor = $this->atts["elementcolor"];

        $css = "
                 .element{ color: {$elementcolor} ;}
        ";
        wp_add_inline_style( 'main', $css);
     }
    }