将Gridster插入WordPress后端


Insert Gridster into WordPress Backend

我一直试图将Gridster包含在我的WordPress后端中,但它根本不起作用。前端工作得很好,但我不明白为什么,因为文件实际上在以下目录中。

我尝试的第一种方式:

function add_my_scripts() {
  wp_enqueue_script('jquery');
  wp_enqueue_script( "gridster-script", plugins_url( '/js/jquery.gridster.min.js', __FILE__  ), array('jquery') );
  wp_enqueue_script( "gridster-script-extra", plugins_url( '/js/jquery.gridster.with-extras.min.js', __FILE__ ), array('gridster-script'), true );
  wp_enqueue_script( "gridster-script-custom",  plugins_url( '/js/gridster.js', __FILE__ ), array('jquery'), '1.0', true );
 }

第二个:

function add_my_scripts() {
  wp_enqueue_script('jquery');
  wp_enqueue_script('gridster-script', plugin_dir_url(__FILE__) . '/js/jquery.gridster.min.js', array('jquery') );
  wp_enqueue_script('gridster-script-extra', plugin_dir_url(__FILE__) . '/js/jquery.gridster.with-extras.min.js', array('gridster-script') );
  wp_enqueue_script('gridster-script-custom', plugin_dir_url(__FILE__) . '/js/gridster.js', array('jquery') );
 }

第三个也是最后一个:

function add_my_scripts() {
  wp_enqueue_script('jquery');
  wp_enqueue_script( 'gridster-script', get_template_directory_uri() . '/js/jquery.gridster.min.js', array('jquery'), '1.0.0', true );
  wp_enqueue_script( 'gridster-script-extra', get_template_directory_uri() . '/js/jquery.gridster.with-extras.min.js', array('gridster-script'), '1.0.0', true );
  wp_enqueue_script( 'gridster-script-custom', get_template_directory_uri() . '/js/gridster.js', array('jquery'), '1.0.0', true );
 }

您不能随意使用插件url函数,需要为您的用例选择正确的函数。假设__FILE__正确地引用了主插件文件,则需要将该函数挂接到admin_enqueue_scripts操作钩子中:

function add_my_scripts() {
    wp_enqueue_script( "gridster-script", plugins_url( 'js/jquery.gridster.min.js', __FILE__  ), array('jquery') );
    wp_enqueue_script( "gridster-script-extra", plugins_url( 'js/jquery.gridster.with-extras.min.js', __FILE__ ), array('gridster-script'), true );
    wp_enqueue_script( "gridster-script-custom",  plugins_url( 'js/gridster.js', __FILE__ ), array('jquery'), '1.0', true );
}
add_action( 'admin_enqueue_scripts', 'add_my_scripts' );