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

Php Wordpress仅从公用函数enqueue_scripts()加载一个脚本{,php,wordpress,Php,Wordpress,我正在使用 在plugins类-my-plugin-public.php中,我有一个enqueue_scripts()函数 ajax和fabric加载很好。但是如果我加载下面的其他内容,它将不会插入页面。你知道我做错了什么吗 /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() {

我正在使用

在plugins类-my-plugin-public.php中,我有一个enqueue_scripts()函数

ajax和fabric加载很好。但是如果我加载下面的其他内容,它将不会插入页面。你知道我做错了什么吗

/**
 * 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 );



}

您是否有结束正文tage
这可能是个问题,因为wp会在脚本之前追加脚本

您是否有结束正文tage
这可能是个问题,因为wp会在脚本之前追加脚本

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

wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )
$handle 脚本的(字符串)(必需)名称。应是唯一的

您可以这样做:

/**
 * 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 );

}

排队的每个脚本都必须有唯一的$handle名称

wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )
$handle 脚本的(字符串)(必需)名称。应是唯一的

您可以这样做:

/**
 * 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 );

}

您在何处根据指定唯一的$handle?似乎您正在尝试重用
$this->plugin\u name
-这是上面链接中的注释-如果您尝试使用不同的参数注册或排队注册已注册的句柄,新参数将被忽略。相反,请使用wp\u deregister\u script()并使用新参数再次注册脚本。您在何处根据指定唯一的$handle?它看起来像您正在尝试重用
$this->plugin\u name
-这是上面链接中的注释-如果您尝试使用不同的参数注册或排队注册已注册的句柄,新参数将被忽略。相反,使用wp_deregister_script()并使用新参数再次注册脚本。