【发布时间】:2019-10-03 11:57:10
【问题描述】:
我是新来的。我有一些作为 wordpress 插件一部分的类代码。我需要在现有代码中添加一个管理页面,然后按照我通过谷歌搜索找到的法典和一篇文章,我成功地添加了该页面。我只想在此管理页面上加载一些脚本,但我不知道如何实现这一点,我发现的每个示例都使用程序样式,我需要将其集成到一个类中。
我创建了两个方法来保存页面创建,一个是调用add_menu_page() 函数,第二个是负责呈现页面内容的回调函数。我无法弄清楚需要在哪里调用 add_action( 'admin_enqueue_script' ) 挂钩。这是我的代码,任何帮助将不胜感激。
// class constuct
public function __construct()
{
add_action( 'admin_menu', array( $this, 'initOptionsMenu' ) );
}
// class method to add the menu page
public function initOptionsMenu()
{
$page_title = 'test page';
$menu_title = 'B Page';
$capability = 'edit_posts'; // is there any reference for this param?
$menu_slug = 'test-page';
$function = array( $this, 'renderMenu' );
$icon_url = '';
$position = 26;
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
}
// class method to render the menu page content
public function renderMenu( $hook )
{
// the $hook variable will be empty if I do a var_dump()
// here I want to enqueue the scripts, is this possible?
require_once 'test-options.php';
}
【问题讨论】:
-
你是如何调用
initOptionsMenu方法的?那是使用 WordPress 钩子吗? -
@PeterMellett 它在类构造中被调用。