将参数从短码传递到入队脚本

时间:2018-08-08 作者:Antonio

我需要将第二个脚本中的参数传递给wp\\u enqueue\\u脚本中的第一个脚本

/*FIRST SCRIPT*/
function add_script() {
    wp_enqueue_script(\'kross-js\', \'https://\'EDITOR-ID\'kross1.com/widget/6.js?lang=it&be_id=\'USER-ID\', array(), \'\' , false);
}

add_action( \'wp_enqueue_scripts\', \'add_script\' );

/*SECOND SCRIPT*/

function kross_shortcode( $atts ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            \'USER-ID\' => \'\',
            \'EDITOR-ID\' => \'\',
        ),
        $atts
    );

return \'<div id="kross-widget">  </div> \';

}

add_shortcode( \'kross\', \'kross_shortcode\' );

1 个回复
SO网友:Jeanne Kidaw

您可以将脚本(和样式)排入短代码中,如下所示:

add_shortcode("kross", function ($atts, $content, $tag) {


    $atts = shortcode_atts([
        \'user-id\' => \'\',
        \'editor-id\' => \'\',
    ], $atts);



    wp_enqueue_script(
          "kross-js"
        , "https://{$atts["editor-id"]}kross1.com/widget/6.js?lang=it&be_id={$atts["user-id"]}"
    );


    $result = "kross result";

    return $result;

});

结束