如何在我的WordPress插件中包含CSS和jQuery?

推荐答案

For styles wp_register_style( 'namespace', 'http://locationofcss.com/mycss.css' );

然后在希望加载CSS的任何位置使用:wp_enqueue_style('namespace');.

脚本与上面相同,但是加载jQuery的更快方法是只使用init中加载的enqueue来加载您想要加载的页面:wp_enqueue_script('jquery');

Unless of course you want to use the google repository for jquery.

You can also conditionally load the jquery library that your script is dependent on:

wp_enqueue_script('namespaceformyscript', 'http://locationofscript.com/myscript.js', array('jquery'));

Update Sept. 2017

I wrote this answer a while ago. I should clarify that the best place to enqueue your scripts and styles is within the wp_enqueue_scripts hook. So for example:

add_action('wp_enqueue_scripts', 'callback_for_setting_up_scripts');
function callback_for_setting_up_scripts() {
    wp_register_style( 'namespace', 'http://locationofcss.com/mycss.css' );
    wp_enqueue_style( 'namespace' );
    wp_enqueue_script( 'namespaceformyscript', 'http://locationofscript.com/myscript.js', array( 'jquery' ) );
}

The wp_enqueue_scripts action will set things up for the "frontend". You can use the admin_enqueue_scripts action for the backend (anywhere within wp-admin) and the login_enqueue_scripts action for the login page.

Jquery相关问答推荐

如何根据另一个 radio Select 并切换?

在 Mastodon 中将 jQuery 添加到 Rails 6

为什么我不能在 window.onload 事件的处理程序中将 $ jQuery 对象作为参数传递?

使用淡入淡出和追加

jQuery - 获取 ajax POST 的表单值

如何使用 jQuery 重置 jquery Select 的 Select 选项?

JQUERY UI Accordion 开始折叠

如何使用 jQuery 进行带命名空间的 XML 解析

jQuery .load() 调用不会在加载的 HTML 文件中执行 JavaScript

Jquery .show() 不显示隐藏可见性的 div

用 Javascript 加载 jQuery 并使用 jQuery

如何使用 JQuery 创建一个新的 img 标签,其中包含来自 JavaScript 对象的 src 和 id?

如何在页面加载时启动 jQuery Fancybox?

如何通过jQuery函数仅获取直接子元素

如何使用jQuery触发类更改事件?

图片转Base64

通过 :not 在 jQuery Select 器中隐藏除 $(this) 之外的所有内容

如何删除集中的 contenteditable pre 周围的边框?

jQuery向左滑动并显示

获取对象属性中的最小值/最大值的快速方法