如何换行/添加<br>到管理菜单

时间:2021-04-08 作者:jave.web

免责声明:This question is not about "frontend" nav menu

我正在寻找一种方法,如何在WP admin侧边栏菜单中的菜单项标签中换行,次要原因是我注册的自定义帖子类型标签太长,无法容纳1行,我真的不想弄乱admin菜单的宽度。

我试着把两者都加上"\\n"<br> 进入自定义贴子类型标签,但<br> 转义,不转换换行符。

我试着在互联网上查找,但我主要找到了一些关于如何插队的文章;“前端”;菜单项不是我要找的。

是否有某种过滤器或其他方法可以做到这一点?

注意:我也希望避免修补任何东西global 或特殊字符,如不间断连字符,如果可能
Note2: I know there is auto linebreak in place, however I need a manually added line break because the term is in some-thing format so it breaks on dash which is unwanted behavior

编辑

Turns out, my problem is specific to the register_post_type label, 添加菜单项;“手动”;具有add_menu_page() 似乎没有此问题,因此我正在共享注册帖子类型的代码:

请注意,这确实再现了这个问题,我也尝试过label arg在那里并删除labels[\'name\'](因为它覆盖了label) 它产生了同样的问题。

register_post_type("whatever_some", [
    \'labels\'          => [
        \'name\'          => \'Whatever a-something\',
        \'singular_name\' => \'Whatever a-something\',
        \'add_new\'       => \'a-something - new\',
        \'add_new_item\'  => \'a-something - new\',
        \'edit_item\'     => \'Edit whatever a-something\',
        \'all_items\'     => \'All whatever a-something\',
    ],
    \'public\'          => true,
    \'menu_icon\'       => \'dashicons-admin-settings\',
    \'capability_type\' => \'page\',
    \'hierarchical\'    => false,
    \'supports\'        => [\'title\', \'editor\', \'thumbnail\', \'excerpt\', \'author\'],
    \'has_archive\'     => true,
    \'rewrite\'         => [\'slug\' => \'whatever-asomething\'],
]);

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

<br> tags are not the solution to the problem that you were hoping they would be. Menu labels were never intended to contain arbitrary HTML. HTML does slip through in places, but this appears to be a bug and is inconsistent.

For Post Types

No. You can\'t.

    $menu[ $ptype_menu_position ] = array( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->cap->edit_posts, $ptype_file, \'\', $menu_class, $ptype_menu_id, $menu_icon );

https://github.com/WordPress/WordPress/blob/270f2011f8ec7265c3f4ddce39c77ef5b496ed1c/wp-admin/menu.php#L169

Changing this would require changes to WordPress itself, and you\'re likely to face heavy resistance.

You might be able to use CSS to adjust how overflow occurs, but this will depend on the exact label you used, its length, and the types of characters inside it.

Note that adding a <br> to the label would also add it everywhere else that label occurs, causing problems. For example the labels get used in dropdown UI\'s and as headings.

Oddly enough, the sub-menu labels don\'t get escaped, so it is possible to add <br> tags to sub-menus, although it will lead to unintended problems:

enter image description here

For General Admin Menu Pages

You can add <br> tags by using them when adding the menu, there is no escaping that removes them. This is very likely a bug.

enter image description here

add_menu_page( 
    \'Custom Menu Title\',
    \'custom menu<br><br>test<br><img width="120" src="https://one.wordpress.test/wp-content/uploads/2021/04/Screenshot-2021-04-08-at-20.35.02-1024x564.png" />\',
    \'manage_options\',
...

So What Can I Do?

Ignore <br> tags and recognise them as the X Y Problem they were all along. You should have asked how to solve your problem, not how to implement your solution.

By sharing code, an obvious solution appears, use the non-breaking hyphen instead of a normal hyphen:

enter image description here

This may not solve the problem for all cases though, after all super-long-hyphenated-text will still overflow if given no other opportunities. Fundamentally the label you\'ve chosen is too long. Shortening it is the only reliable and futureproof solution.