Wordpress仅从公共函数enqueue_scripts(){加载一个脚本

时间:2017-11-24 15:15:41

标签: php wordpress

我正在使用WordPress-Plugin-Boilerplate

在插件class-my-plugin-public.php中我有enqueue_scripts()函数。

ajax和面料加载就好了。但是,如果我加载其他内容,它将不会插入页面。我有什么想法吗?

/**
 * Register the JavaScript for the public-facing side of the site.
 *
 * @since    1.0.0
 */
public function enqueue_scripts() {


    wp_localize_script( $this->plugin_name, 'canvas-draw' , array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

    wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/fabric.min.js', array( 'jquery' ), $this->version, false );

    // This doesnt load
    wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/scroll-behaviour.js', array( 'jquery' ), $this->version, true );
    // This doesnt load either
    wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/drag-drop-index.min.js', array( 'jquery' ), $this->version, true );



}

2 个答案:

答案 0 :(得分:1)

您必须为每个排队的脚本提供唯一的 $ handle 名称。

wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )
  

$处理   (字符串)(必需)脚本的名称。应该是独一无二的。

您可以这样做:

/**
 * Register the JavaScript for the public-facing side of the site.
 *
 * @since    1.0.0
 */
public function enqueue_scripts() {


    wp_localize_script( $this->plugin_name, 'canvas-draw' , array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

    wp_enqueue_script( $this->plugin_name . '_fabric', plugin_dir_url( __FILE__ ) . 'js/fabric.min.js', array( 'jquery' ), $this->version, false );

    wp_enqueue_script( $this->plugin_name . '_scroll-behaviour', plugin_dir_url( __FILE__ ) . 'js/scroll-behaviour.js', array( 'jquery' ), $this->version, true );

    wp_enqueue_script( $this->plugin_name . '_drag-drop-index', plugin_dir_url( __FILE__ ) . 'js/drag-drop-index.min.js', array( 'jquery' ), $this->version, true );

}

答案 1 :(得分:0)

你是否有一个结束体$str = '{ "firstName": "Jhon", "lastName": "Doe", "username": "jhon", "avatar": "localhost/uploads/avatars/default.jpg", "language": "ar", "birth_date": "2017-11-22 00:00:00", "weight_chart": [], "health_status": { "id": 130, "user_id": 258, "weight": 95, "height": 171 } }'; $obj = json_decode($str); echo $obj->firstName.' - '; echo $obj->health_status->id.' - '; echo $obj->health_status->weight; 它可能是问题,因为wp在它之前附加脚本