如何将 jquery 添加到 Wordpress 管理页面?

How to add jquery to Wordpress admin pages?

我想通过插件向 wordpress 管理页面添加 jquery 功能,但它们需要在头脑中才能工作(至少这是我能够获得它们的唯一方法在职的)。

我正在使用 admin_head 挂钩,但我不确定这是正确的方法。 admin_enqueue_scripts 似乎是首选方法,但它对我不起作用,因为它会在页脚中加载脚本。

有没有办法让 admin_enqueue_scripts 在 header 中加载?有没有理由不使用 admin_head

admin_enqueue_scripts 不在页脚加载脚本。

您可以将wp_register_script()的第五个参数设置为false。

第五个参数:

$in_footer (bool) (Optional) Whether to enqueue the script before instead of in the .

wp_register_script( $handle, $src, $deps = array(), $ver, $in_footer);

function yournamespace_enqueue_scripts( $hook ) {

    if( !in_array( $hook, array( 'post.php', 'post-new.php' ) ) )
        return;

    wp_enqueue_script( 
        'your_script_handle',                         // Handle
        plugins_url( '/yourfilename.js', __FILE__ ),  // Path to file
        array( 'jquery' )                             // Dependancies
    );
}
add_action( 'admin_enqueue_scripts', 'yournamespace_enqueue_scripts', 2000 );