Domanda

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'],
]);
È stato utile?

Soluzione

<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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top