Skip to content

Instantly share code, notes, and snippets.

@bporcelli
Created July 18, 2020 17:02
Show Gist options
  • Select an option

  • Save bporcelli/3a4b14c505d3c613f1e21f464f54c0a5 to your computer and use it in GitHub Desktop.

Select an option

Save bporcelli/3a4b14c505d3c613f1e21f464f54c0a5 to your computer and use it in GitHub Desktop.
Shows how to add a "My Store" menu item linking to the current user's Dokan store
<?php
/**
* Adds support for adding a "My Store" link to a WP nav menu.
*
* This works by finding all menu items with the URL "#dokan-my-store" and
* replacing "#dokan-my-store" with the current user's storefront URL. If the
* current user is not a vendor or Dokan is not active these menu items will
* be hidden instead.
*
* @param array $items Menu items.
*
* @return array
*/
function dokan_handle_my_store_menu_item( $items ) {
// We only want to change the menu item URL on the frontend
if ( is_admin() ) {
return $items;
}
foreach ( $items as $key => $item ) {
if ( '#dokan-my-store' === $item->url ) {
// Hide menu item if Dokan is not active
if ( ! function_exists( 'dokan_get_current_user_id' ) ) {
unset( $items[ $key ] );
continue;
}
// Set correct URL if user is seller or hide menu item otherwise
$user_id = dokan_get_current_user_id();
$is_seller = dokan_is_user_seller( $user_id );
if ( $is_seller ) {
$items[ $key ]->url = dokan_get_store_url( $user_id );
} else {
unset( $items[ $key ] );
}
}
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'dokan_handle_my_store_menu_item' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment