Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing Menus #1

Closed
richtabor opened this issue Jul 28, 2017 · 8 comments
Closed

Importing Menus #1

richtabor opened this issue Jul 28, 2017 · 8 comments

Comments

@richtabor
Copy link
Owner

I'd like for menus to also import as part of the content importer. That'd be awesome. 🙌

@andrastudio
Copy link

+1 from me

@richtabor
Copy link
Owner Author

I have it importing menu locations based on get_theme_mod( 'nav_menu_locations' ) though that does not cover the actual pages/links within the menus. 🤔

@richtabor
Copy link
Owner Author

What do you think — should we specific menu navigation, or should that part specifically be left up to the user?

@JiveDig
Copy link

JiveDig commented Oct 1, 2018

What ended up with this? I had to write my own function to set the menus to their locations. It seems to work, but feels like it should be part of MerlinWP core. Most of the time when importing demo content the user is going to want to import the menus as well IMO.

@richtabor
Copy link
Owner Author

@JiveDig How're you importing menus? I think it'd be a nice addition. Maybe even have a checkbox on the content importing view.

@JiveDig
Copy link

JiveDig commented Oct 5, 2018

I went off your example file. It's based on slug so definitely not very useful in the core class. I just went off the main menu names we use in our demos and docs.

/**
 * Set the menus.
 *
 * @return  void
 */
add_action( 'merlin_after_all_import', function( $selected_import_index ) {

	$locations = $nav_menu_locations = array();

	// Get menus.
	$locations['header_left']  = maiconfigurations_get_menu_by_slug( 'header-left' );
	$locations['header_right'] = maiconfigurations_get_menu_by_slug( 'header-right' );
	$locations['primary']      = maiconfigurations_get_menu_by_slug( 'primary' );
	$locations['secondary']    = maiconfigurations_get_menu_by_slug( 'footer' );
	$locations['mobile']       = maiconfigurations_get_menu_by_slug( 'mobile' );

	// Loop through our nav menus.
	foreach ( $locations as $location => $menu ) {

		// Skip if no menu.
		if ( ! $menu ) {
			continue;
		}

		// Set as a valid menu.
		$nav_menu_locations[ $location ] = $menu->term_id;
	}

	// Bail if no menus.
	if ( ! $nav_menu_locations ) {
		return;
	}

	// Set the menus.
	set_theme_mod( 'nav_menu_locations', $nav_menu_locations );
});

function maiconfigurations_get_menu_by_slug( $slug ) {

	if ( $menu = get_term_by( 'slug', $slug, 'nav_menu' ) ) {
		return $menu;
	}

	if ( $menu = get_term_by( 'slug', $slug . '-nav', 'nav_menu' ) ) {
		return $menu;
	}

	if ( $menu = get_term_by( 'slug', $slug . '-menu', 'nav_menu' ) ) {
		return $menu;
	}

	return false;
}

There has to be a better way. Maybe storing/passing the menu object during import or something.

@JiveDig
Copy link

JiveDig commented Oct 5, 2018

Also, this would be way more useful to do demo-specific stuff if we could get #120 data passed. I tried to re-architect the class a little but that one seems like you may want to have the first pass at it, if you agree and are motivated ;)

@richtabor
Copy link
Owner Author

Also, this would be way more useful to do demo-specific stuff if we could get #120 data passed. I tried to re-architect the class a little but that one seems like you may want to have the first pass at it, if you agree and are motivated ;)

I honestly don't have a ton of resources currently for big parts here. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants