简体   繁体   English

如何将自己的javascript / css文件添加到wordpress插件菜单页面?

[英]How to add own javascript/css file to wordpress plugin menu page?

I want to write my first WordPress plugin and could need some help. 我想编写我的第一个WordPress插件,可能需要一些帮助。 I don't know how to add my javascript file to the dashboard area head when clicking on the menu item of my plugin. 单击插件的菜单项时,我不知道如何将我的JavaScript文件添加到仪表板区域的头部。 This is what I've done so far: 到目前为止,这是我所做的:

1st step: created a directory and a php-file in the wp-content/plugins directory, added the head of the plugin like this: 第一步:在wp-content/plugins目录中创建一个目录和一个php文件,并添加如下所示的插件头:

/* Plugin Name: Test Plugin
Plugin URI: http://my-plugin.com/
Description: test test test
Version: 1.0
Author: me
Author URI: http://my-plugin.com/
License: GPLv2 or later
*/

2nd step: With this function I add my plugins name (top level menu item name) at the bottom of the dashboard menu. 第二步:使用此功能,我在仪表板菜单的底部添加我的插件名称(顶级菜单项名称)。 I dont even know why the name and the slug name are listed twice. 我什至不知道为什么名字和子弹名字被列出两次。 I got that code from: here . 我得到的代码: 这里 I also dont know what "mt-top-level-handle" does. 我也不知道“ mt-top-level-handle”的作用。 There is no function for it and it still works. 它没有任何功能,但仍然有效。 "manage_options" is the capability variable which defines the user permissions on this page. “ manage_options”是功能变量,用于定义此页面上的用户权限。 (Anyone knows how to make this page accessable for admins,(authors) only? ) (任何人都知道如何使管理员只能访问此页面,(作者)?)

add_action( 'admin_menu', 'register_my_menu_item' );
function register_my_menu_item() {
     add_menu_page(__('top level menu item name','slug-name'), __('top level menu item name','slug-name'), 'manage_options', 'mt-top-level-handle', 'my_plugins_page' );
}

3rd step: This simple function displays "Hello World" on the plugins page. 第三步:此简单功能在插件页面上显示“ Hello World”。

function edit_table_page() {
?>
    <h2>Hello World</h2>
<?php
}

Now I'd like to know how to use my javascript file(s) on that plugins page. 现在,我想知道如何在该插件页面上使用我的JavaScript文件。 I need to add it after the jquery file is loaded. 我需要在加载jquery文件后添加它。

All I found about adding scripts was this: 我发现有关添加脚本的全部内容是:

add_action('wp_enqueue_scripts', 'my_scripts');
function my_scripts() {
   wp_enqueue_script('jquery'); 
   wp_register_script('my_script', plugins_url('js/my_script.js', __FILE__),array("jquery")); 
   wp_enqueue_script('my_script');
}

That didn't work. 那没用。 So how to do it right? 那么怎么做对呢? :) :)

add_menu_page() returns the hookname on success. add_menu_page()成功返回钩子名称。 You can leverage that to enqueue your script on the dynamic load-{$hookname} hook: 您可以利用它来使脚本进入动态load-{$hookname}挂钩:

add_action( 'admin_menu', 'register_my_menu_item' );
function register_my_menu_item()
{
    $my_plugins_page = add_menu_page(
        __( 'top level menu item name','my_plugin_textdomain' ),
        __( 'top level menu item name', 'my_plugin_textdomain' ),
        'manage_options',
        'mt-top-level-handle',
        'my_plugins_page'
    );
    add_action( 'load-' . $my_plugins_page, 'so20659191_enqueue' );
}

function so20659191_enqueue()
{
    wp_enqueue_script( 'my_script', plugins_url( 'js/my_script.js', __FILE__ ), array( 'jquery' ), null, true );
}

function my_plugins_page() {
?>
    <h2>Hello World</h2>
<?php
}

Found a solution! 找到了解决方案! adding js-files: 添加js文件:

add_action('admin_enqueue_scripts', 'my_scripts'); // add scripts to dashboard head
function my_scripts() {
    wp_enqueue_script('jquery'); 
    wp_register_script('my_script', plugins_url('js/my_script.js', __FILE__),array("jquery")); 
    wp_enqueue_script('my_script');    
}

adding CSS File: 添加CSS文件:

add_action('admin_enqueue_scripts', 'my_styles');
function my_styles() {
    wp_register_style( 'custom_wp_admin_css', plugins_url('my-plugin/css/style.css'));
    wp_enqueue_style( 'custom_wp_admin_css' );
}

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

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