简体   繁体   English

将 Javascript 文件与 wordpress 插件链接

[英]Link Javascript file with wordpress plugin

I am struggling with finishing a basic plugin I created.我正在努力完成我创建的基本插件。

I have a plugin folder with the following structure:我有一个具有以下结构的插件文件夹:

Folder_name: Booking
booking.php
js - script.js
css - style.css
js and css are sub folders in the Booking folder. 

I have the following function in the booking.php file:我在booking.php文件中有以下功能:

function sc_booking_process() {
    return 'html content which uses divs which connect to js and css files';
}
 add_shortcode('cinema-booking', 'sc_booking_process');
?>

The booking.php needs to link to the javascript and css files to work properly but no matter what method I do it wont work. booking.php 需要链接到 javascript 和 css 文件才能正常工作,但无论我做什么方法都行不通。

I have tried putting the following before the above function but they still aren't linked to it:我曾尝试将以下内容放在上述功能之前,但它们仍然没有链接到它:

function my_scripts_and_css() {
    wp_enqueue_style( 'my-plugin-css', 'css/style.css'  );
    wp_enqueue_script( 'my-plugin-js', 'js/script.js', array('jquery'), '20200110' );       
}
add_action( 'wp_enqueue_scripts', 'my_scripts_and_css' );

Any advise would be really appreciated.任何建议将不胜感激。

function my_scripts_and_css() {

  wp_enqueue_style( 'my-plugin-css', plugin_dir_url( __FILE__ ).'/css/style.css' );
  wp_enqueue_script( 'my-plugin-js', plugin_dir_url( __FILE__ ).'/js/script.js', array('jquery'), '20200110' );       

}

add_action( 'wp_enqueue_scripts', 'my_scripts_and_css' );

You call wrong dir for css and js, please try this one!你为css和js调用了错误的目录,请试试这个! hope it will work for you希望它对你有用

function my_scripts_and_css() {

  wp_enqueue_style( 'my-plugin-css', plugins_url( '/css/style.css', __FILE__ ) );
  wp_enqueue_script( 'my-plugin-js', plugins_url( '/js/script.js',__FILE__ ), array('jquery'), '20200110' );       

}
add_action( 'wp_enqueue_scripts', 'my_scripts_and_css' );

It should work now!它现在应该可以工作了! Please try with this请试试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM