0

Disclaimer: This question is not about "frontend" nav menu

I am looking for a way how to break line in a menu item label in WP admin sidebar menu, sidereason is that the custom post type label I am registering is too long for 1 line and I don't really want to mess with the width of the admin menu.

I tried adding both "\n" and <br> into the custom post type label, but <br> gets escaped and line breaks are not converted.

I tried to look on the Internet, but I mainly found articles how to line break in "frontend" menu items which is not what I am looking for.

Is there some sort of filter or other way to do this?

Note: I would also like to avoid tinkering with anything global or special characters like nonbreaking hyphen, if possible
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

EDIT

Turns out, my problem is specific to the register_post_type label, adding menu item "manually" with add_menu_page() does not seem to have this problem, so I am sharing my code for registering the post type:

Please note that, this does reproduce the issue and I also did try putting label arg there and removing labels['name'](since it overrides label) and it produced the same issue.

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'],
]);
10
  • What is the label? Note that the admin nav menu wasn't built and styled for this, and in the future it's likely to be replaced by a React/JS based UI. Hints of what that will look like are already showing up in Gutenberg experiments, and an early version of it was adapted for the wordpress.com UI – Tom J Nowell Apr 8 at 11:10
  • @TomJNowell developer.wordpress.org/reference/functions/register_post_type -label this is what I am referring to. What you are saying about react/js based UI implies even more that there probably should be some sort of filter for this which would work regardless of how the UI is implemented :) – jave.web Apr 8 at 11:32
  • I'm suggesting that this will get harder not easier in the future, and that any filter you did find will probably stop working in the near-mid future. My question wasn't where is the label coming from though, but a literal what is the value of the label? E.g. "Acme corps example post type label". It's difficult if not impossible to explore CSS and JS based solutions without knowing this as there are lots of edge cases and caveats – Tom J Nowell Apr 8 at 11:33
  • It's something like "Whatever a-something" , but "a-something" belongs together as one name and must be placed on a single line. I know a lot of tweaks how to do this, but I was looking for more of an official way. BTW: Very purpose of filters is they're independent of the source code, it would be a real shame if WP core team would abandon that when they already messed up Gutenberg. – jave.web Apr 8 at 19:02
  • what's the actual label though? Not a generic example, I understood that it ended in a word that had a hyphen from your previous comments. – Tom J Nowell Apr 8 at 19:18
1

<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. Fundamentally the label you've chosen is too long. Shortening it is the only reliable and futureproof solution.

9
  • But how come they get escaped when adding through register_post_type? O.o – jave.web 2 days ago
  • you never mentioned register_post_type in your question, that is super important information you should have shared. Share your CPT registration code in your question – Tom J Nowell 2 days ago
  • I did mention sidereason is that the custom post type label I am registering which I thought was implying it, sorry if that wasn't clear – jave.web 2 days ago
  • so to make things clearer I've shared register_post_type code in EDIT replicating the same issue, thank you for your patience :-) – jave.web 2 days ago
  • @jave.web I've updated the answer, it's not good news. <br> tags are not the solution you were hoping for. – Tom J Nowell 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.