Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * WCFM Vendor Store URL Functions
- *
- * @package Reign-child
- * @subpackage WCFM Vendor Extensions
- * @since 1.0.0
- * @author WB COM
- *
- * This file contains functions for retrieving a WCFM vendor's store URL
- * and providing it via shortcode for easy use in content.
- */
- /**
- * Shortcode to display the current user's vendor store URL if they are a vendor
- *
- * @param array $atts Shortcode attributes
- * @return string Formatted HTML link or # if conditions not met
- *
- * Usage:
- * [vendor_store_url] - Default link text "My Store"
- * [vendor_store_url text="Visit My Store"] - Custom link text
- * [vendor_store_url text="My Chef Profile" class="custom-button"] - Custom CSS class
- */
- function wbcom_get_vendor_store_url_shortcode($atts) {
- // Default attributes
- $atts = shortcode_atts(
- array(
- 'text' => 'My Store', // Default link text
- 'class' => 'vendor-store-link', // Default CSS class
- ),
- $atts,
- 'vendor_store_url'
- );
- // Check if user is logged in
- if (!is_user_logged_in()) {
- return '#'; // Return # if not logged in
- }
- // Get current user
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- // Check if user is a vendor (has wcfm_vendor role)
- if (!in_array('wcfm_vendor', (array) $current_user->roles)) {
- return '#'; // Return # if not a vendor
- }
- // Get the vendor's store URL
- if (function_exists('wcfmmp_get_store_url')) {
- $store_url = wcfmmp_get_store_url($user_id);
- // Return formatted link
- return '<a href="' . esc_url($store_url) . '" class="' . esc_attr($atts['class']) . '">' . esc_html($atts['text']) . '</a>';
- }
- return '#'; // Return # if function doesn't exist
- }
- add_shortcode('vendor_store_url', 'wbcom_get_vendor_store_url_shortcode');
- /**
- * Function to programmatically get vendor store URL
- *
- * @param int $user_id User ID (optional, defaults to current user)
- * @return string Store URL or # if conditions not met
- *
- * Can be used in theme files with: <?php echo wbcom_get_wcfm_vendor_store_url(); ?>
- * Or for specific user: <?php echo wbcom_get_wcfm_vendor_store_url($user_id); ?>
- */
- function wbcom_get_wcfm_vendor_store_url($user_id = 0) {
- // If no user ID is provided, use current user
- if ($user_id === 0 && is_user_logged_in()) {
- $user_id = get_current_user_id();
- }
- // Check if user exists and is a vendor
- if ($user_id > 0) {
- $user = get_userdata($user_id);
- if ($user && in_array('wcfm_vendor', (array) $user->roles)) {
- // Get the vendor's store URL
- if (function_exists('wcfmmp_get_store_url')) {
- return wcfmmp_get_store_url($user_id);
- }
- }
- }
- return '#'; // Return # if not a vendor or if function doesn't exist
- }
Advertisement
Comments
-
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