Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mralaminahamed/5b65ee92429137fcf9c0946a86dade86 to your computer and use it in GitHub Desktop.

Select an option

Save mralaminahamed/5b65ee92429137fcf9c0946a86dade86 to your computer and use it in GitHub Desktop.
Display Dokan Vendor meta in single product page and loop
/**
* Snippet unificato Dokan + Vendor
* Funziona sia con ID utente generico che con loop prodotto
* 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'; });
}
// --------------------------
// Shortcodes generici vendor
// --------------------------
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 ( !$vendor_id && get_query_var('author') ) {
$vendor_id = (int) get_query_var('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']) ) {
// Combina i campi principali
$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']);
// Se non c'è ID, ricava l'autore della pagina vendor
if ( !$vendor_id && get_query_var('author') ) {
$vendor_id = (int) get_query_var('author');
}
// Se siamo su prodotto, usa l'autore del prodotto
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 '';
});
}
// --------------------------
// Shortcodes prodotti (loop)
// --------------------------
if ( ! shortcode_exists('vendor_name_link') ) {
add_shortcode('vendor_name_link', function($atts){
// Verifica che le funzioni Dokan siano disponibili
if ( ! function_exists('dokan_get_vendor_by_product') ) return '';
// Prende il prodotto dal contesto (o dalla funzione custom)
$product = function_exists('sn_get_product_from_context') ? sn_get_product_from_context() : null;
if (!$product) return '';
// Prende il vendor
$vendor = dokan_get_vendor_by_product($product->get_id());
if (!$vendor) return '';
// Nome dello store
$shop_name = method_exists($vendor,'get_shop_name') ? $vendor->get_shop_name() : '';
$shop_name = trim( wp_strip_all_tags($shop_name) ); // Rimuove tag HTML e spazi
// URL del negozio
$shop_url = method_exists($vendor,'get_shop_url') ? $vendor->get_shop_url() : get_author_posts_url($vendor->get_id());
// Ritorna link sicuro
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment