Add a submenu link in WordPress

Method #1: add_submenu_page()

/**
 * Adds "Drafts" submenu link to the WooCommerce "Products" menu.
 *
 * @see https://wpcodebook.com/add-a-submenu-link-in-wordpress/
 * @see https://developer.wordpress.org/reference/hooks/admin_menu/
 * @see https://developer.wordpress.org/reference/functions/add_submenu_page/
 */
add_action( 'admin_menu', function () {
	add_submenu_page(
		'edit.php?post_type=product',
		esc_html__( 'Drafts' ),
		esc_html__( 'Drafts' ),
		'manage_woocommerce',
		'wpcodebook-draft-products',
		function () {
			wp_safe_redirect( admin_url( 'edit.php?post_status=draft&post_type=product' ) );
			exit;
		}
	);
} );

Method #2: global $submenu

/**
 * Adds "Drafts" submenu link to the WooCommerce "Products" menu.
 *
 * @see https://wpcodebook.com/add-a-submenu-link-in-wordpress/
 * @see https://developer.wordpress.org/reference/hooks/admin_menu/
 * @see https://wordpress.stackexchange.com/questions/266318/how-to-add-custom-submenu-links-in-wp-admin-menus
 */
add_action( 'admin_menu', function () {
	global $submenu;
	$submenu['edit.php?post_type=product'][] = array(
		esc_html__( 'Drafts' ),
		'manage_woocommerce',
		admin_url( 'edit.php?post_status=draft&post_type=product' ),
	);
} );

Leave a Comment