Home / Attachments / FontAwesome
Duplicate Snippet

Embed Snippet on Your Site

FontAwesome

Piers Berry PRO
<10
Code Preview
php
<?php
<?php
/**
 * [fa] shortcode
 * Usage: [fa icon="envelope" style="solid" size="48" class="my-extra-class" label="Email"]
 * - icon: Font Awesome icon name without the "fa-" prefix (e.g., "envelope", "facebook")
 * - style: solid | regular | brands (default: solid)  [light is Pro-only; we fall back]
 * - size: number in px (e.g., 20, 48) OR FA keyword (sm, lg, xl, 2x, 3x...) (default: inherit)
 * - class: optional extra classes
 * - label: accessible label; when set, we render role="img" + aria-label
 */
add_shortcode('fa', function ($atts) {
    $atts = shortcode_atts([
        'icon'  => 'circle',
        'style' => 'solid',
        'size'  => '',
        'class' => '',
        'label' => '',
        'title' => '',
    ], $atts, 'fa');
    // Map requested style to FA 6 Free class
    $style = strtolower(trim($atts['style']));
    switch ($style) {
        case 'brands':
        case 'brand':
            $style_class = 'fa-brands';
            break;
        case 'regular': // Regular is partially free
            $style_class = 'fa-regular';
            break;
        case 'light':   // Light is Pro; fall back to solid
            $style_class = 'fa-solid';
            break;
        case 'solid':
        default:
            $style_class = 'fa-solid';
            break;
    }
    // Build size (px or FA keyword)
    $size = trim($atts['size']);
    $style_attr = '';
    $size_class = '';
    if ($size !== '') {
        if (is_numeric($size)) {
            // Numeric = pixels
            $style_attr = ' style="font-size:' . intval($size) . 'px"';
        } else {
            // Assume FA keyword like 'xl', '2x', etc.
            $size_class = ' fa-' . preg_replace('/[^a-z0-9x\-]/i', '', $size);
        }
    }
    $icon = preg_replace('/[^a-z0-9\-]/i', '', $atts['icon']); // simple sanitize
    $extra = trim($atts['class']);
    $title = esc_attr($atts['title']);
    // Accessibility
    $label = trim($atts['label']);
    $role  = $label !== '' ? ' role="img" aria-label="' . esc_attr($label) . '"' : ' aria-hidden="true"';
    return '<i class="' . esc_attr($style_class . ' fa-' . $icon . $size_class . ($extra ? ' ' . $extra : '')) . '"' . $role . ($title ? ' title="'.$title.'"' : '') . $style_attr . '></i>';
});

Comments

Add a Comment