繁体   English   中英

如何将自定义 javascript 添加到 WordPress 管理员?

[英]How to add custom javascript to WordPress Admin?

我想向编辑帖子页面添加一些自定义 jquery 代码,这非常简单,例如当有人按下发布时显示一个 div。

唯一的限制是我想通过使用插件来实现这一点,而不是破解管理模板文件。

我尝试过使用一些操作来回显一些脚本标签,但似乎不是这样。

使用admin_enqueue_scripts操作和wp_enqueue_script方法将自定义脚本添加到管理界面。

这假设您的插件文件夹中有myscript.js 相应地改变。 my_custom_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');

你的functions.php文件有一个片段:

function custom_admin_js() {
    $url = get_bloginfo('template_directory') . '/js/wp-admin.js';
    echo '"<script type="text/javascript" src="'. $url . '"></script>"';
}
add_action('admin_footer', 'custom_admin_js');

在 Wordpress 3.2.1 上运行良好。

<?php
function add_jquery_data() {
    global $parent_file;

    if ( isset( $_GET['action'] ) && $_GET['action'] == 'edit' && isset( $_GET['post'] ) && $parent_file == 'edit.php') {
    // Do some stuff.
    }
}

add_filter('admin_head', 'add_jquery_data');

?>

admin_enqueue_scriptswp_enqueue_script是将 javascript 文件添加到仪表板的首选方式。

// I'm using an anonymous function for brevity.
add_action( 'admin_enqueue_scripts', function() {
    wp_enqueue_script( 'handle', plugin_dir_url( __FILE__ ) . '/script.js' );
} );

但是,如果您想使用 PHP 函数输出 javascript,则wp_add_inline_script似乎不起作用。 相反,您可以使用admin_print_scripts直接回显脚本,包括脚本标签本身。 只需确保将优先级设置为高,以便在任何必需的库(例如jQuery之后加载。

add_action( 'admin_print_scripts', function() {
    // I'm using NOWDOC notation to allow line breaks and unescaped quotation marks.
    echo <<<'EOT'
<script type="text/javascript">
jQuery(function($){
    // Do stuff here.
});
</script>
EOT;
}, PHP_INT_MAX );

如果您想花哨并过滤要加载文件的位置,最好使用get_current_screen

function myproject_admin_enqueue() {

    $screen = get_current_screen();

    // load on the NEW and EDIT screens of all post types
    if ( 'post' === $screen->base ) {
        wp_enqueue_script('my_custom_script', plugin_dir_url( __FILE__ ) . 'all-admin.js');
    }

    // load on the NEW and EDIT screens of one post type
    if ( 'post' === $screen->base && 'myposttype' === $screen->post_type ) {
        wp_enqueue_script('my_custom_script', plugin_dir_url( __FILE__ ) . 'mypostype-admin.js');
    }

    // load only when adding a NEW post, not when editing an existing one.
    if ( 'post' === $screen->base && 'add' === $screen->action ) {
        wp_enqueue_script('my_custom_script', plugin_dir_url( __FILE__ ) . 'new-admin.js');
    }

}
add_action('admin_enqueue_scripts', 'myproject_admin_enqueue');

直接将wp_enqueue_script添加到您的代码中不会在新版本的 Wordpress(5.0 以上)中包含该脚本。 更好的方法是首先使用wp_register_script注册脚本以创建一个句柄,然后将该句柄入队。

function custom_script_in_admin($hook) {
    if ('edit.php' !== $hook) {
        return;
    }
    wp_register_script( 'custom_handle_name',plugin_dir_url( __FILE__ ) . '/script.js', '',true );
    wp_enqueue_script('custom_handle_name');
}

add_action('admin_enqueue_scripts', 'custom_script_in_admin');

不知何故,接受的答案对我不起作用。 这是我在答案中没有找到的另一个解决方案,这就是为我所做的,WP 5.x,为我的后端添加了一些 css 和 js...

function suedsicht_theme_add_editor_assets() {
  wp_enqueue_style( 'custom-gutenberg-stylesheet', get_template_directory_uri() . '/css/custom-editor-style.css', array(), wp_get_theme()->get( 'Version' ), 'all' );
  wp_enqueue_script('suedsicht-admin-js', get_template_directory_uri() . '/js/admin.js');
}
add_action( 'enqueue_block_editor_assets', 'suedsicht_theme_add_editor_assets' );

通过使用admin enqueue 脚本挂钩,您可以轻松地为管理面板添加脚本文件。

function custom_admin_js() {

wp_enqueue_script( 'custom_wp_admin_js', get_template_directory_uri() . '/new-assets/js/admin_section.js', false, '1.0.0' );
wp_enqueue_script( 'custom_wp_admin_js' );
}

add_action('admin_enqueue_scripts', 'custom_admin_js');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM