【问题标题】:hook scripts on specific wordpress admin page特定 wordpress 管理页面上的挂钩脚本
【发布时间】: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 它在类构造中被调用。

标签: php wordpress


【解决方案1】:

请参阅此处的 WP 管理员操作顺序:https://codex.wordpress.org/Plugin_API/Action_Reference/#Actions_Run_During_an_Admin_Page_Request

admin_menuadmin_enqueue_scripts 之前被调用

所以在您的renderMenu 方法中,您可以使用admin_enqueue_scripts,因为它将在执行操作之前注册。

public function renderMenu( $hook )
{
    add_action( 'admin_enqueue_scripts', [ $this, 'loadAdminScripts' ] );

    // 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';
}

public function loadAdminScripts()
{
    // enqueue your scripts
}

【讨论】:

  • 感谢您的帮助,这会仅在我的自定义管理页面上加载脚本吗?
  • 为此,您需要查看使用 $hook 参数 - 请参阅此示例:codex.wordpress.org/Plugin_API/Action_Reference/…
  • 我已经阅读了该页面,但如果我将 $hook 变量传递给函数,它将为空或 null。
【解决方案2】:

您可以在像这样将脚本排入队列之前检查页面。

// add enqueue hook in __construct
public function __construct()
  {
       // admin menu hook
       add_action( 'admin_menu', [$this, 'you_callback'] );

       // page scripts hook
       add_action( 'admin_enqueue_script', [$this, 'admin_page_scripts_function'] )
  }

// hook function where you check menu page before add enqueue scripts
public function admin_page_scripts_function($hook) {

    // Checking the menu page with the menu slug. 
    // You change your-menu-slug-here by the right slug of your menu
    if ( "toplevel_page_your-menu-slug-here" === $hook ) :
        // here you enqueue the scripts
    endif;
}

【讨论】:

  • 如果我在var_dump() 方法或initOptionsMenu() 方法中使用var_dump() 变量,则$hook 变量为空。我会再试一次
  • 很抱歉没有很快注意到您的评论。您必须在脚本挂钩之前调用菜单添加挂钩。之后,如果菜单与页面一起出现,那么它应该可以工作,除非在 admin_menu_page_scripts_function 回调中错误引用了菜单 slug。
  • @abage 我有同样的问题,但在我的情况下,我已经定义了add_action('admin_menu'),如果我尝试var_dump(),$hook 变量将不会显示。有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多