Skip to content

Instantly share code, notes, and snippets.

@gioiliop7
Created September 18, 2023 16:36
Show Gist options
  • Save gioiliop7/80b9e30ed95fb5bfc51258fcc03b5a06 to your computer and use it in GitHub Desktop.
Save gioiliop7/80b9e30ed95fb5bfc51258fcc03b5a06 to your computer and use it in GitHub Desktop.
[WORDPRESS][DOKAN] Create shortcode and display html with given id
<?php
// Define the custom shortcode function
function get_vendor_info_shortcode($atts)
{
// Extract the attributes from the shortcode
$atts = shortcode_atts(
array(
'id' => 0, // Default ID if not provided
),
$atts,
'vendors'
);
// Get the vendor ID from the shortcode attribute
$vendor_id = intval($atts['id']);
// Check if a valid vendor ID is provided
if ($vendor_id > 0) {
// Retrieve the user associated with the vendor ID
$user = get_userdata($vendor_id);
// Check if a user with the specified ID exists
if ($user) {
// Check if the user has the "seller" role
if (in_array('seller', $user->roles)) {
// Query the database or perform any other necessary actions to get vendor information
$vendor_info = get_user_meta($vendor_id);
// Check if vendor information is available
if ($vendor_info) {
// Display the vendor information
$output = '<div class="vendor-info">';
$output .= '<h3>Vendor Information for ID ' . $vendor_id . '</h3>';
$output .= '<p>Name: ' . esc_html($vendor_info->user_nicename) . '</p>';
// Add more vendor information fields as needed
$output .= '</div>';
return $output;
} else {
return 'Vendor not found.';
}
} else {
return 'The specified user is not a seller.';
}
} else {
return 'User not found.';
}
} else {
return 'Invalid vendor ID.';
}
}
// Register the shortcode
add_shortcode('vendors', 'get_vendor_info_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment