Javascript wordpress将属性从短代码传递到java脚本
我创建了一个插件,它从短代码中获取属性,并将它们传递给java脚本,它工作正常,但我认为它写错了。请告诉我如何正确地编写代码 短码 [yoolek-googlemap-v1 lat=51 lng=17 zoom=15] 在上一个版本中,我在 添加_操作“wp_排队_脚本”,数组$this,“cssjs” 在函数构造中,但我无法将变量从run_yoolek传递到wp_localize_脚本。 这是我的非工作代码:Javascript wordpress将属性从短代码传递到java脚本,javascript,wordpress,plugins,shortcode,Javascript,Wordpress,Plugins,Shortcode,我创建了一个插件,它从短代码中获取属性,并将它们传递给java脚本,它工作正常,但我认为它写错了。请告诉我如何正确地编写代码 短码 [yoolek-googlemap-v1 lat=51 lng=17 zoom=15] 在上一个版本中,我在 添加_操作“wp_排队_脚本”,数组$this,“cssjs” 在函数构造中,但我无法将变量从run_yoolek传递到wp_localize_脚本。 这是我的非工作代码: public function __construct() {
public function __construct()
{
add_action( 'wp_enqueue_scripts', array( $this, 'cssjs' ) );
add_shortcode( $this->tag, array($this, 'run_yoolek' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
public function run_yoolek($atts)
{
$atts = shortcode_atts(
array(
'lat' => '51',
'lng' => '0',
'zoom' => '9',
), $atts, $this->tag );
$this->admin_lat = $atts['lat'];
$this->admin_lng = $atts['lng'];
$this->admin_zoom = $atts['zoom'];
ob_start();
echo $this->admin_lat.",";
echo $this->admin_lng.",";
echo $this->admin_zoom;
echo "<div id='".$this->tag."-map-canvas'></div>";
return ob_get_clean();
}
/*ADMIN,PLUGIN HOOKS AND FUNCTIONS*/
public function cssjs() {
wp_enqueue_style( $this->tag.'-css',plugin_dir_url( __FILE__ ).'style.css');
wp_enqueue_script($this->tag.'-google-maps', 'https://...');
wp_enqueue_script($this->tag.'-js', plugin_dir_url( __FILE__ ).'script.js');
$params = array(
'lat' => $this->admin_lat,
'lng' => $this->admin_lng,
'zoom' => $this->admin_zoom,
);
wp_localize_script($this->tag.'-js', 'MyScriptParams', $params );
}
public function __construct()
{
add_action( 'wp_enqueue_scripts', array( $this, 'cssjs' ) );
add_shortcode( $this->tag, array($this, 'run_yoolek' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
public function run_yoolek($atts)
{
$atts = shortcode_atts(
array(
'lat' => '51',
'lng' => '0',
'zoom' => '9',
), $atts, $this->tag );
$this->admin_lat = $atts['lat'];
$this->admin_lng = $atts['lng'];
$this->admin_zoom = $atts['zoom'];
ob_start();
echo $this->admin_lat.",";
echo $this->admin_lng.",";
echo $this->admin_zoom;
echo "<div id='".$this->tag."-map-canvas'></div>";
return ob_get_clean();
}
/*ADMIN,PLUGIN HOOKS AND FUNCTIONS*/
public function cssjs() {
wp_enqueue_style( $this->tag.'-css',plugin_dir_url( __FILE__ ).'style.css');
wp_enqueue_script($this->tag.'-google-maps', 'https://...');
wp_enqueue_script($this->tag.'-js', plugin_dir_url( __FILE__ ).'script.js');
$params = array(
'lat' => $this->admin_lat,
'lng' => $this->admin_lng,
'zoom' => $this->admin_zoom,
);
wp_localize_script($this->tag.'-js', 'MyScriptParams', $params );
}