WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()publicWP 4.3.0

Get the value emulated into a WP_Post and set up as a nav_menu_item.

Method of the class: WP_Customize_Nav_Menu_Item_Setting{}

Return

WP_Post. With wp_setup_nav_menu_item() applied.

Usage

$WP_Customize_Nav_Menu_Item_Setting = new WP_Customize_Nav_Menu_Item_Setting();
$WP_Customize_Nav_Menu_Item_Setting->value_as_wp_post_nav_menu_item();

Changelog

Since 4.3.0 Introduced.

WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item() code WP 6.6.2

public function value_as_wp_post_nav_menu_item() {
	$item = (object) $this->value();
	unset( $item->nav_menu_term_id );

	$item->post_status = $item->status;
	unset( $item->status );

	$item->post_type  = 'nav_menu_item';
	$item->menu_order = $item->position;
	unset( $item->position );

	if ( empty( $item->original_title ) ) {
		$item->original_title = $this->get_original_title( $item );
	}
	if ( empty( $item->title ) && ! empty( $item->original_title ) ) {
		$item->title = $item->original_title;
	}
	if ( $item->title ) {
		$item->post_title = $item->title;
	}

	// 'classes' should be an array, as in wp_setup_nav_menu_item().
	if ( isset( $item->classes ) && is_scalar( $item->classes ) ) {
		$item->classes = explode( ' ', $item->classes );
	}

	$item->ID    = $this->post_id;
	$item->db_id = $this->post_id;
	$post        = new WP_Post( (object) $item );

	if ( empty( $post->post_author ) ) {
		$post->post_author = get_current_user_id();
	}

	if ( ! isset( $post->type_label ) ) {
		$post->type_label = $this->get_type_label( $post );
	}

	// Ensure nav menu item URL is set according to linked object.
	if ( 'post_type' === $post->type && ! empty( $post->object_id ) ) {
		$post->url = get_permalink( $post->object_id );
	} elseif ( 'taxonomy' === $post->type && ! empty( $post->object ) && ! empty( $post->object_id ) ) {
		$post->url = get_term_link( (int) $post->object_id, $post->object );
	} elseif ( 'post_type_archive' === $post->type && ! empty( $post->object ) ) {
		$post->url = get_post_type_archive_link( $post->object );
	}
	if ( is_wp_error( $post->url ) ) {
		$post->url = '';
	}

	/** This filter is documented in wp-includes/nav-menu.php */
	$post->attr_title = apply_filters( 'nav_menu_attr_title', $post->attr_title );

	/** This filter is documented in wp-includes/nav-menu.php */
	$post->description = apply_filters( 'nav_menu_description', wp_trim_words( $post->description, 200 ) );

	/** This filter is documented in wp-includes/nav-menu.php */
	$post = apply_filters( 'wp_setup_nav_menu_item', $post );

	return $post;
}