Opened 10 years ago
Last modified 7 months ago
#32700 new enhancement
Allow media attachments in nav menus
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 4.7.3 |
Component: | Menus | Keywords: | has-patch dev-feedback has-unit-tests |
Focuses: | Cc: |
Description
I have a requirement to include Media items directly into a menu.
Choosing the menu item will display the attachment page.
Currently, this is not possible, for two reasons.
By default post_type attachment
has show_in_nav_menus
set to false.
If set true, then the Media metabox is displayed in wp-admin/nav-menus.php
, but with "No items".
This is due to _wp_nav_menu_meta_box_object() setting the default query to "post_status" = "publish".
Were the default query for attachment set to "post_status" = "inherit" then the Media items would be listed.
This would allow media items to be easily added to menus.
} elseif ( 'attachment' == $object->name ) { // $object->_default_query = array( 'post_status' => 'inherit', );
Current workaround
I can very easily programmatically set show_in_nav_menus
,
I can also implement the nav_menu_meta_box_object
filter
to update the value for "post_status" as above.
But it would be a lot easier if the logic in the filter was in core.
Attachments (2)
Change History (14)
#3
follow-up:
↓ 5
@
10 years ago
I've uploaded a patch file that changes _wp_nav_menu_meta_box_object().
In order to test it you need to set show_in_nav_menus to true against the attachment post type object, in response to "init".
$post_type_object = get_post_type_object( "attachment" ); $post_type_object->show_in_nav_menus = true;
You also need to create some attachments.
I'm not sure where to develop a unit test for this :-)
#4
@
9 years ago
- Keywords has-patch dev-feedback needs-unit-tests added; needs-patch removed
Patch applies cleanly and confirming indicated setup works.
You can create test in the tests/phpunit/tests/menu folder.
#5
in reply to:
↑ 3
@
8 years ago
Replying to bobbingwide:
I've uploaded a patch file that changes _wp_nav_menu_meta_box_object().
In order to test it you need to set show_in_nav_menus to true against the attachment post type object, in response to "init".
$post_type_object = get_post_type_object( "attachment" ); $post_type_object->show_in_nav_menus = true;You also need to create some attachments.
I'm not sure where to develop a unit test for this :-)
Ok, so I have been able to make the Media show on the nav menu page by doing
add_action( 'init', 'add_attachments_to_nav_menu' ); function add_attachments_to_nav_menu() { $post_type_object = get_post_type_object( "attachment" ); $post_type_object->show_in_nav_menus = true; }
But how do I update the _wp_nav_menu_meta_box_object()
for attachments to be post_status->"inherit"
so that media files show up?
#6
@
8 years ago
- Version set to 4.7.3
So adding the code below to _wp_nav_menu_meta_box_object()
function in the /wp-admin/includes/nav-menu.php
, my media files show up! I DO NOT want to do this. Is there any other way to inject this code or do something else to get the same results?
// Attachments have a status of 'inherit' } elseif ( 'attachment' == $object->name ) { $object->_default_query = array( 'post_status' => 'inherit', );
It would be nice if I could filter and even set specific file types... I really only want to have the availability to add PDFs. I know I can go get the URL from the media library and add it as a Custom Link, but this is just so much nicer.
#7
@
7 years ago
In 32700.2.diff three tests are added:
- The nav menu meta box object for attachments should have inherit post status
- The nav menu meta box for attachments should show no items when there are no attachments available
- The nav menu meta box for attachments should show items when there are attachments available
There are no tests available for nav menu meta boxes, so I created a new test class.
Hope this helps the ticket.
#10
@
6 years ago
@bobbingwide I realise this is an old ticket but my own query (#46585) just got marked as a Duplicate thought it is slightly different (the same issue in Appearance -> Customize, NOT Appearance -> Menus)
You previously said you had a workaround. Do you still have a working solution you could share? Thanks.
#12
@
7 months ago
Desperate for this functionality still! Being able to add attachments (especially PDFs) with a click to nav menus without having to go through the "Custom Link" copy/paste url would save a ton of effort and time. Has noone found a way to do this without modifying core?
_wp_nav_menu_meta_box_object()
/wp-admin/includes/nav-menu.php
Allow attachments to be listed in nav menus