Skip to content

Instantly share code, notes, and snippets.

@marrisonlab
Last active December 21, 2025 17:05
Show Gist options
  • Select an option

  • Save marrisonlab/2d2b1e2423312076256e9def70ff931a to your computer and use it in GitHub Desktop.

Select an option

Save marrisonlab/2d2b1e2423312076256e9def70ff931a to your computer and use it in GitHub Desktop.
Display Dokan Vendor meta in single product page and loop
/**
* Unified Snippet Dokan + Vendor
* Works with both generic User ID and product loop
* WPCode: PHP Snippet → Run Everywhere
*/
if ( ! function_exists( 'sn_get_product_from_context' ) ) {
function sn_get_product_from_context() {
global $product;
if ( isset( $product ) && is_object( $product ) ) return $product;
if ( function_exists( 'wc_get_product' ) && get_the_ID() ) {
$p = wc_get_product( get_the_ID() );
if ( $p ) return $p;
}
return null;
}
}
if ( ! function_exists( 'sn_robust_get_vendor_info' ) ) {
function sn_robust_get_vendor_info( $user_id ) {
$user_id = intval($user_id);
if ( ! $user_id ) return array();
if ( function_exists('dokan_get_store_info') ) {
$info = dokan_get_store_info($user_id);
if ( is_array($info) && !empty($info) ) return $info;
}
$settings = get_user_meta($user_id,'dokan_profile_settings',true);
if ( is_array($settings) && !empty($settings) ) return $settings;
$user = get_userdata($user_id);
return array(
'store_name' => $user ? $user->display_name : '',
'description'=> '',
'banner' => '',
'gravatar' => '',
);
}
}
add_action('after_setup_theme', function() {
// --------------------------
// Test shortcode
// --------------------------
if ( ! shortcode_exists('dokan_test') ) {
add_shortcode('dokan_test', function(){ return 'SHORTCODE OK'; });
}
// --------------------------
// Generic vendor shortcodes
// --------------------------
if ( ! shortcode_exists('dokan_store_name') ) {
add_shortcode('dokan_store_name', function($atts){
$atts = shortcode_atts(array('id'=>0),$atts);
$info = sn_robust_get_vendor_info($atts['id']);
return !empty($info['store_name']) ? esc_html($info['store_name']) : '';
});
}
if ( ! shortcode_exists('dokan_store_address') ) {
add_shortcode('dokan_store_address', function($atts){
$atts = shortcode_atts(array('id'=>0), $atts);
$vendor_id = intval($atts['id']);
// If no ID is provided and we are on an author/vendor page, get the author ID
if ( !$vendor_id && get_query_var('author') ) {
$vendor_id = (int) get_query_var('author');
}
// If no ID is provided and we are on a single product page, use the product author
if ( !$vendor_id && is_singular('product') ) {
$vendor_id = (int) get_post_field('post_author', get_the_ID());
}
if ( !$vendor_id ) return '';
$info = sn_robust_get_vendor_info($vendor_id);
$address = '';
if ( !empty($info['address']) && is_array($info['address']) ) {
// Combine the main fields
$address_parts = array_filter([
$info['address']['street_1'] ?? '',
$info['address']['street_2'] ?? '',
$info['address']['city'] ?? '',
$info['address']['state'] ?? '',
$info['address']['zip'] ?? '',
$info['address']['country'] ?? '',
]);
$address = implode(', ', $address_parts);
}
return esc_html($address);
});
}
if ( ! shortcode_exists('dokan_store_description') ) {
add_shortcode('dokan_store_description', function($atts){
$atts = shortcode_atts(array('id'=>0),$atts);
$vendor_id = intval($atts['id']);
// If no ID, get the author of the vendor page
if ( !$vendor_id && get_query_var('author') ) {
$vendor_id = (int) get_query_var('author');
}
// If on a product, use the product's author
if ( !$vendor_id && is_singular('product') ) {
$vendor_id = (int) get_post_field('post_author', get_the_ID());
}
if ( !$vendor_id ) return '';
$info = sn_robust_get_vendor_info($vendor_id);
$bio = '';
if ( !empty($info['vendor_biography']) ) {
$bio = $info['vendor_biography'];
} elseif ( !empty($info['description']) ) {
$bio = $info['description']; // fallback
}
return !empty($bio) ? wp_kses_post($bio) : '';
});
}
if ( ! shortcode_exists('dokan_store_logo') ) {
add_shortcode('dokan_store_logo', function($atts){
$atts = shortcode_atts(array('id'=>0,'class'=>'dokan-store-logo','alt'=>''),$atts);
$info = sn_robust_get_vendor_info($atts['id']);
$url = '';
if (!empty($info['gravatar'])) $url = is_numeric($info['gravatar']) ? wp_get_attachment_url(intval($info['gravatar'])) : esc_url_raw($info['gravatar']);
if ( !$url && function_exists('wc_placeholder_img_src') ) $url = wc_placeholder_img_src();
if (!$url) $url='https://via.placeholder.com/80';
return '<img src="'.esc_url($url).'" alt="'.esc_attr($atts['alt']).'" class="'.esc_attr($atts['class']).'">';
});
}
if ( ! shortcode_exists('dokan_store_banner') ) {
add_shortcode('dokan_store_banner', function($atts){
$atts = shortcode_atts(array('id'=>0,'class'=>'dokan-store-banner','alt'=>''),$atts);
$info = sn_robust_get_vendor_info($atts['id']);
$url = !empty($info['banner']) ? wp_get_attachment_url(intval($info['banner'])) : '';
if (!$url && function_exists('wc_placeholder_img_src')) $url = wc_placeholder_img_src();
if (!$url) $url='https://via.placeholder.com/200x100';
return '<img src="'.esc_url($url).'" alt="'.esc_attr($atts['alt']).'" class="'.esc_attr($atts['class']).'">';
});
}
if ( ! shortcode_exists('dokan_store_link') ) {
add_shortcode('dokan_store_link', function($atts){
$atts = shortcode_atts(array('id'=>0,'label'=>'Visita il negozio','class'=>'dokan-store-link'),$atts);
$id = intval($atts['id']);
$url = $id && function_exists('dokan_get_store_url') ? dokan_get_store_url($id) : ($id ? get_author_posts_url($id):'');
if ($url) return '<a href="'.esc_url($url).'" class="'.esc_attr($atts['class']).'">'.esc_html($atts['label']).'</a>';
return '';
});
}
// --------------------------
// Product shortcodes (loop)
// --------------------------
if ( ! shortcode_exists('vendor_name_link') ) {
add_shortcode('vendor_name_link', function($atts){
// Check if Dokan functions are available
if ( ! function_exists('dokan_get_vendor_by_product') ) return '';
// Get the product from context (or custom function)
$product = function_exists('sn_get_product_from_context') ? sn_get_product_from_context() : null;
if (!$product) return '';
// Get the vendor
$vendor = dokan_get_vendor_by_product($product->get_id());
if (!$vendor) return '';
// Store name
$shop_name = method_exists($vendor,'get_shop_name') ? $vendor->get_shop_name() : '';
$shop_name = trim( wp_strip_all_tags($shop_name) ); // Remove HTML tags and spaces
// Store URL
$shop_url = method_exists($vendor,'get_shop_url') ? $vendor->get_shop_url() : get_author_posts_url($vendor->get_id());
// Return secure link
return '<a class="vendor-link" href="'.esc_url($shop_url).'">'.esc_html($shop_name).'</a>';
});
}
if ( ! shortcode_exists('vendor_badge') ) {
add_shortcode('vendor_badge', function($atts){
if ( ! function_exists('dokan_get_vendor_by_product') ) return '';
$product = sn_get_product_from_context();
if (!$product) return '';
$vendor = dokan_get_vendor_by_product($product->get_id());
if (!$vendor) return '';
$shop_name = method_exists($vendor,'get_shop_name') ? $vendor->get_shop_name() : '';
$shop_url = method_exists($vendor,'get_shop_url') ? $vendor->get_shop_url() : get_author_posts_url($vendor->get_id());
$logo_url = '';
if ( method_exists($vendor,'get_shop_info') ) {
$shop_info = $vendor->get_shop_info();
if (is_array($shop_info) && !empty($shop_info['gravatar'])) {
$gr = $shop_info['gravatar'];
$logo_url = is_numeric($gr) ? wp_get_attachment_url(intval($gr)) : esc_url_raw($gr);
}
}
if (!$logo_url && method_exists($vendor,'get_avatar_url')) $logo_url = $vendor->get_avatar_url();
if (!$logo_url && function_exists('wc_placeholder_img_src')) $logo_url = wc_placeholder_img_src();
if (!$logo_url) $logo_url = 'https://via.placeholder.com/80';
$html = '<div class="vendor-badge"><a href="'.esc_url($shop_url).'">';
$html .= '<img class="vendor-logo" src="'.esc_url($logo_url).'" alt="'.esc_attr($shop_name).' logo" />';
$html .= '<span class="vendor-name">'.esc_html($shop_name).'</span></a></div>';
return $html;
});
}
}, 0);
@marrisonlab
Copy link
Author

[dokan_test] : A simple test shortcode that returns the string "SHORTCODE OK" to confirm that the snippet is running correctly.

[dokan_store_name] : Displays the name of a vendor's store. It can be used with an optional id attribute to specify a user ID.

[dokan_store_address] : Displays the address of a vendor. It automatically finds the vendor from the context of a product page or a vendor page, or you can provide a user ID with the id attribute.

[dokan_store_description] : Displays the vendor's biography or store description. Like the address shortcode, it can automatically detect the vendor from the page context or use a provided id.

[dokan_store_logo] : Displays the vendor's store logo. You can specify a user ID with the id attribute, and also provide custom class and alt attributes for the image.

[dokan_store_banner] : Displays the vendor's store banner image. It works similarly to the logo shortcode, accepting an optional id and custom class and alt attributes.

[dokan_store_link] : Generates a link to the vendor's store page. It accepts an optional id to specify the vendor and a label to set the link's text.

[vendor_name_link] : Designed for use within a product loop, this shortcode displays the vendor's name as a hyperlink to their store page. It automatically gets the vendor information from the current product's context.

[vendor_badge] : Also for use in a product loop, this shortcode creates an HTML element that displays both the vendor's logo and their name, with a link to their store page.

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