0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
function wpturbo_map_subtenants_shortcode($atts) {
// Get the 'subtenants' attribute
$subtenants = isset($atts['subtenants']) ? $atts['subtenants'] : '';
// Split the subtenants string into an array of lines
$lines = explode(PHP_EOL, $subtenants);
// Initialize the output variable
$output = '';
// Loop through each line
foreach ($lines as $line) {
// Split the line into label and URL using the '|' character
$parts = explode('|', $line);
$label = isset($parts[0]) ? trim($parts[0]) : '';
$url = isset($parts[1]) ? trim($parts[1]) : '';
// Create the list item
$listItem = '<li>';
// Check if a URL is present
if (!empty($url)) {
// Wrap the label in a link with target="_blank" and rel="nofollow noopener"
$listItem .= '<a href="' . esc_url($url) . '" target="_blank" rel="nofollow noopener">' . esc_html($label) . '</a>';
} else {
// If no URL is present, just display the label
$listItem .= esc_html($label);
}
$listItem .= '</li>';
// Add the list item to the output
$output .= $listItem;
}
// Wrap the list items in a <ul> element with class "subtenant"
$output = '<ul class="subtenant">' . $output . '</ul>';
return $output;
}
add_shortcode('map_subtenants', 'wpturbo_map_subtenants_shortcode');
Explanation:
To use the shortcode, you can add [map_subtenants subtenants="Label 1|URL 1nLabel 2|URL 2nLabel 3"] to your content, replacing "Label" and "URL" with your actual values. Each line represents a separate list item.
Please note that this code includes proper escaping of HTML and URL attributes using esc_html and esc_url functions respectively, which helps prevent security vulnerabilities such as XSS attacks.