Advertisement
vapvarun

Shortcode to display the current user's vendor store URL if they are a vendor

Apr 20th, 2025 (edited)
401
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | Software | 0 0
  1. /**
  2.  * WCFM Vendor Store URL Functions
  3.  *
  4.  * @package     Reign-child
  5.  * @subpackage  WCFM Vendor Extensions
  6.  * @since       1.0.0
  7.  * @author      WB COM
  8.  *
  9.  * This file contains functions for retrieving a WCFM vendor's store URL
  10.  * and providing it via shortcode for easy use in content.
  11.  */
  12.  
  13. /**
  14.  * Shortcode to display the current user's vendor store URL if they are a vendor
  15.  *
  16.  * @param array $atts Shortcode attributes
  17.  * @return string Formatted HTML link or # if conditions not met
  18.  *
  19.  * Usage:
  20.  * [vendor_store_url] - Default link text "My Store"
  21.  * [vendor_store_url text="Visit My Store"] - Custom link text
  22.  * [vendor_store_url text="My Chef Profile" class="custom-button"] - Custom CSS class
  23.  */
  24. function wbcom_get_vendor_store_url_shortcode($atts) {
  25.     // Default attributes
  26.     $atts = shortcode_atts(
  27.         array(
  28.             'text' => 'My Store', // Default link text
  29.             'class' => 'vendor-store-link', // Default CSS class
  30.         ),
  31.         $atts,
  32.         'vendor_store_url'
  33.     );
  34.    
  35.     // Check if user is logged in
  36.     if (!is_user_logged_in()) {
  37.         return '#'; // Return # if not logged in
  38.     }
  39.    
  40.     // Get current user
  41.     $current_user = wp_get_current_user();
  42.     $user_id = $current_user->ID;
  43.    
  44.     // Check if user is a vendor (has wcfm_vendor role)
  45.     if (!in_array('wcfm_vendor', (array) $current_user->roles)) {
  46.         return '#'; // Return # if not a vendor
  47.     }
  48.    
  49.     // Get the vendor's store URL
  50.     if (function_exists('wcfmmp_get_store_url')) {
  51.         $store_url = wcfmmp_get_store_url($user_id);
  52.        
  53.         // Return formatted link
  54.         return '<a href="' . esc_url($store_url) . '" class="' . esc_attr($atts['class']) . '">' . esc_html($atts['text']) . '</a>';
  55.     }
  56.    
  57.     return '#'; // Return # if function doesn't exist
  58. }
  59. add_shortcode('vendor_store_url', 'wbcom_get_vendor_store_url_shortcode');
  60.  
  61. /**
  62.  * Function to programmatically get vendor store URL
  63.  *
  64.  * @param int $user_id User ID (optional, defaults to current user)
  65.  * @return string Store URL or # if conditions not met
  66.  *
  67.  * Can be used in theme files with: <?php echo wbcom_get_wcfm_vendor_store_url(); ?>
  68.  * Or for specific user: <?php echo wbcom_get_wcfm_vendor_store_url($user_id); ?>
  69.  */
  70. function wbcom_get_wcfm_vendor_store_url($user_id = 0) {
  71.     // If no user ID is provided, use current user
  72.     if ($user_id === 0 && is_user_logged_in()) {
  73.         $user_id = get_current_user_id();
  74.     }
  75.    
  76.     // Check if user exists and is a vendor
  77.     if ($user_id > 0) {
  78.         $user = get_userdata($user_id);
  79.         if ($user && in_array('wcfm_vendor', (array) $user->roles)) {
  80.             // Get the vendor's store URL
  81.             if (function_exists('wcfmmp_get_store_url')) {
  82.                 return wcfmmp_get_store_url($user_id);
  83.             }
  84.         }
  85.     }
  86.    
  87.     return '#'; // Return # if not a vendor or if function doesn't exist
  88. }
Tags: WCFM
Advertisement
Comments
  • vapvarun
    79 days (edited)
    # Markdown 0.68 KB | 0 0

    How to Use the Shortcode:

    Basic usage in pages or posts:

    [vendor_store_url]

    With custom link text:

    [vendor_store_url text="Visit My Chef Store"]

    With custom CSS class:

    [vendor_store_url text="My Chef Profile" class="chef-button"]

    For Theme Developers:

    To get just the URL in PHP (returns # if not applicable):

    <?php echo wbcom_get_wcfm_vendor_store_url(); ?>

    For a specific user:

    <?php echo wbcom_get_wcfm_vendor_store_url($user_id); ?>

    This code aligns with your existing practices in the theme and maintains the naming convention you're using with other functions like wbcom_add_ld_instructor_role().

Add Comment
Please, Sign In to add comment
Advertisement