如何在我的 WordPress 插件中包含 CSS 和 jQuery? [英] How To Include CSS and jQuery in my WordPress plugin?

查看:22
本文介绍了如何在我的 WordPress 插件中包含 CSS 和 jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How To Include CSS and jQuery in my WordPress plugin ?

推荐答案

对于样式 wp_register_style( 'namespace', 'http://locationofcss.com/mycss.css' );

然后使用:wp_enqueue_style('namespace'); 任何你想要加载 css 的地方.

Then use: wp_enqueue_style('namespace'); wherever you want the css to load.

脚本如上,但加载 jquery 的更快方法是使用在 init 中加载的 enqueue 来加载您希望它加载的页面:wp_enqueue_script('jquery');

Scripts are as above but the quicker way for loading jquery is just to use enqueue loaded in an init for the page you want it to load on: wp_enqueue_script('jquery');

当然,除非您想使用 jquery 的 google 存储库.

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

您还可以有条件地加载您的脚本所依赖的 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'));

2017 年 9 月更新

我不久前写了这个答案.我应该澄清一下,将脚本和样式放入队列的最佳位置是 wp_enqueue_scripts 钩子.例如:

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' ) );
}

wp_enqueue_scripts 操作将为前端"进行设置.您可以将 admin_enqueue_scripts 操作用于后端(在 wp-admin 中的任何位置)和 login_enqueue_scripts 操作用于登录页面.

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.

这篇关于如何在我的 WordPress 插件中包含 CSS 和 jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆