如何将自定义 HTML 类名称添加到管理屏幕子菜单项? [英] How to add a custom HTML class name to admin screen submenu items?

查看:20
本文介绍了如何将自定义 HTML 类名称添加到管理屏幕子菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是帖子菜单下的这些子菜单项

Here are these submenu items under the Posts menu

我检查了代码,发现它的标记是这样的

I inspected the code and found out that the markup of it is this

<ul class="wp-submenu wp-submenu-wrap">
  <li class="wp-submenu-head" aria-hidden="true">Posts</li>
  <li class="wp-first-item current"><a href="edit.php" class="wp-first-item current">All Posts</a></li>
  <li><a href="post-new.php">Add New</a></li>
  <li><a href="edit-tags.php?taxonomy=category">Categories</a></li>
  <li><a href="edit-tags.php?taxonomy=post_tag">Tags</a></li>
</ul>

我想要做的是在 <li> 标签上添加一个自定义类 my-custom-class(在服务器端处理),这样会变成这样

What I would want to do is add a custom class my-custom-class on the <li> tags (processed on the server-side) such that it would become like this

<ul class="wp-submenu wp-submenu-wrap">
  <li class="wp-submenu-head" aria-hidden="true">Posts</li>
  <li class="wp-first-item current my-custom-class"><a href="edit.php" class="wp-first-item current">All Posts</a></li>
  <li class="my-custom-class"><a href="post-new.php">Add New</a></li>
  <li class="my-custom-class"><a href="edit-tags.php?taxonomy=category">Categories</a></li>
  <li class="my-custom-class"><a href="edit-tags.php?taxonomy=post_tag">Tags</a></li>
</ul>

有没有办法将自定义 HTML 类名称添加到管理屏幕子菜单项中?

Is there a way to add a custom HTML class name to admin screen submenu items?

推荐答案

您可以对 html 执行 str_replace 这将在页面加载之前发生:

You can do a str_replace on the html which will happen before the page has loaded:

只需要计算出选择器或者用 DOMDocument 解析它

Just need to work out the selectors or then parse it with DOMDocument

function callback($buffer) {

    $buffer = str_replace('wp-first-item', 'wp-first-item my-custom-class', $buffer);

    return $buffer;
}

function buffer_start() { ob_start("callback"); }

function buffer_end() { ob_end_flush(); }

add_action('admin_head', 'buffer_start');
add_action('admin_footer', 'buffer_end');

这篇关于如何将自定义 HTML 类名称添加到管理屏幕子菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆