返回介绍

wp_update_nav_menu_object()

发布于 2017-09-11 13:04:58 字数 4624 浏览 1156 评论 0 收藏 0

wp_update_nav_menu_object( int $menu_id,  array $menu_data = array() )

Save the properties of a menu or create a new menu with those properties.


description

Note that $menu_data is expected to be pre-slashed.


参数

$menu_id

(int) (Required) The ID of the menu or "0" to create a new menu.

$menu_data

(array) (Optional) The array of menu data.

Default value: array()


返回值

(int|WP_Error) Menu ID on success, WP_Error object on failure.


源代码

File: wp-includes/nav-menu.php

function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
	// expected_slashed ($menu_data)
	$menu_id = (int) $menu_id;

	$_menu = wp_get_nav_menu_object( $menu_id );

	$args = array(
		'description' => ( isset( $menu_data['description'] ) ? $menu_data['description']  : '' ),
		'name'        => ( isset( $menu_data['menu-name']   ) ? $menu_data['menu-name']    : '' ),
		'parent'      => ( isset( $menu_data['parent']      ) ? (int) $menu_data['parent'] : 0  ),
		'slug'        => null,
	);

	// double-check that we're not going to have one menu take the name of another
	$_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
	if (
		$_possible_existing &&
		! is_wp_error( $_possible_existing ) &&
		isset( $_possible_existing->term_id ) &&
		$_possible_existing->term_id != $menu_id
	) {
		return new WP_Error( 'menu_exists',
			/* translators: %s: menu name */
			sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
				'<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
			)
		);
	}

	// menu doesn't already exist, so create a new menu
	if ( ! $_menu || is_wp_error( $_menu ) ) {
		$menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );

		if ( $menu_exists ) {
			return new WP_Error( 'menu_exists',
				/* translators: %s: menu name */
				sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
					'<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
				)
			);
		}

		$_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );

		if ( is_wp_error( $_menu ) )
			return $_menu;

		/**
		 * Fires after a navigation menu is successfully created.
		 *
		 * @since 3.0.0
		 *
		 * @param int   $term_id   ID of the new menu.
		 * @param array $menu_data An array of menu data.
		 */
		do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );

		return (int) $_menu['term_id'];
	}

	if ( ! $_menu || ! isset( $_menu->term_id ) )
		return 0;

	$menu_id = (int) $_menu->term_id;

	$update_response = wp_update_term( $menu_id, 'nav_menu', $args );

	if ( is_wp_error( $update_response ) )
		return $update_response;

	$menu_id = (int) $update_response['term_id'];

	/**
	 * Fires after a navigation menu has been successfully updated.
	 *
	 * @since 3.0.0
	 *
	 * @param int   $menu_id   ID of the updated menu.
	 * @param array $menu_data An array of menu data.
	 */
	do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
	return $menu_id;
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: esc_html()
  • wp-includes/taxonomy.php: wp_update_term()
  • wp-includes/taxonomy.php: wp_insert_term()
  • wp-includes/taxonomy.php: get_term_by()
  • wp-includes/plugin.php: do_action()
  • wp-includes/nav-menu.php: wp_create_nav_menu
  • wp-includes/nav-menu.php: wp_update_nav_menu
  • wp-includes/nav-menu.php: wp_get_nav_menu_object()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 6 more uses Hide more uses

Used By

  • wp-includes/customize/class-wp-customize-nav-menu-setting.php: WP_Customize_Nav_Menu_Setting::update()
  • wp-includes/nav-menu.php: wp_create_nav_menu()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文