如何将自定义JavaScript添加到WordPress管理员? [英] How to add custom javascript to WordPress Admin?

查看:70
本文介绍了如何将自定义JavaScript添加到WordPress管理员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Edit Post页面添加一些自定义jquery代码,非常简单,例如当有人按下Publish时显示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屋!

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