如何将自定义 javascript 添加到 WordPress 管理员? [英] How to add custom javascript to WordPress Admin?
问题描述
我想向编辑帖子"页面添加一些自定义 jquery 代码,这非常简单,例如在有人按下发布"时显示一个 div.
I want to add some custom jquery code to the Edit Post page, something really simple like showing a div when someone presses Publish.
唯一的限制是我想通过使用插件来实现这一点,而不是破解管理模板文件.
The only restriction is that I want to achieve this through the use of a plugin, not hacking the admin template files.
我尝试使用一些操作来回显一些脚本标签,但似乎不是这样.
I've tried echoing some script tags using some actions but it doesn't seem to be the way.
推荐答案
使用 admin_enqueue_scripts
操作和 wp_enqueue_script
方法将自定义脚本添加到管理界面.
Use the admin_enqueue_scripts
action and the wp_enqueue_script
method to add custom scripts to the admin interface.
这假设您的插件文件夹中有 myscript.js
.相应地改变.my_custom_script
句柄对于您的模块和脚本应该是唯一的.
This assumes that you have myscript.js
in your plugin folder. Change accordingly. The my_custom_script
handle should be unique for your module and script.
function my_enqueue($hook) {
// Only add to the edit.php admin page.
// See WP docs.
if ('edit.php' !== $hook) {
return;
}
wp_enqueue_script('my_custom_script', plugin_dir_url(__FILE__) . '/myscript.js');
}
add_action('admin_enqueue_scripts', 'my_enqueue');
这篇关于如何将自定义 javascript 添加到 WordPress 管理员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!