Skip to content

WordPress: Preserve Hierarchy in Menu Selection META-Box

The metaboxes for the menu item selection are a mess. Pagination is not good, elements are skipped. Ordering feels completely random and the overall user experience is not good. With this script in your child themes function.php file you can easily fix that and have a look like in this screenshot.

class Preserve_Page_and_Taxonomy_Hierarchy {

	function __construct() {
		add_action( 'load-nav-menus.php', array( $this, 'init' ) );
	}

	function init() {
		add_action( 'pre_get_posts',    array( $this, 'disable_paging_for_hierarchical_post_types' ) );
		add_filter( 'get_terms_args',   array( $this, 'remove_limit_for_hierarchical_taxonomies' ), 10, 2 );
		add_filter( 'get_terms_fields', array( $this, 'remove_page_links_for_hierarchical_taxonomies' ), 10, 3 );
	}

	function disable_paging_for_hierarchical_post_types( $query ) {
		if ( ! is_admin() || 'nav-menus' !== get_current_screen()->id ) {
			return;
		}

		if ( ! is_post_type_hierarchical( $query->get( 'post_type' ) ) ) {
			return;
		}

		if ( 50 == $query->get( 'posts_per_page' ) ) {
			$query->set( 'nopaging', true );
		}
	}

	function remove_limit_for_hierarchical_taxonomies( $args, $taxonomies ) {
		if ( ! is_admin() || 'nav-menus' !== get_current_screen()->id ) {
			return $args;
		}

		if ( ! is_taxonomy_hierarchical( reset( $taxonomies ) ) ) {
			return $args;
		}

		if ( 50 == $args['number'] ) {
			$args['number'] = '';
		}

		return $args;
	}

	function remove_page_links_for_hierarchical_taxonomies( $selects, $args, $taxonomies ) {
		if ( ! is_admin() || 'nav-menus' !== get_current_screen()->id ) {
			return $selects;
		}

		if ( ! is_taxonomy_hierarchical( reset( $taxonomies ) ) ) {
			return $selects;
		}

		if ( 'count' === $args['fields'] ) {
			$selects = array( '1' );
		}

		return $selects;
	}

}

new Preserve_Page_and_Taxonomy_Hierarchy;

Dieser Beitrag hat 0 Kommentare

Schreibe einen Kommentar

Deine E-Mail wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

An den Anfang scrollen