WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 16 months ago

#32700 new enhancement

Allow media attachments in nav menus

Reported by: bobbingwide 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)

32700.diff (560 bytes) - added by bobbingwide 4 years ago.
Allow attachments to be listed in nav menus
32700.2.diff (3.5 KB) - added by birgire 16 months ago.

Download all attachments as: .zip

Change History (10)

#1 @johnbillion
4 years ago

  • Keywords needs-patch added

#2 @johnbillion
4 years ago

  • Version trunk deleted

@bobbingwide
4 years ago

Allow attachments to be listed in nav menus

#3 follow-up: @bobbingwide
4 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 @welcher
3 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 @Rob_Huska
20 months 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 @Rob_Huska
20 months 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.

@birgire
16 months ago

#7 @birgire
16 months 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.

#8 @birgire
16 months ago

  • Keywords has-unit-tests added; needs-unit-tests removed
Note: See TracTickets for help on using tickets.