Home / Admin / Auto-link Site Name – Home Page
Duplicate Snippet

Embed Snippet on Your Site

Auto-link Site Name – Home Page

Pulls company name from Settings > General > Site Title
Pulls link title from Settings > General > Tagline

<10
Code Preview
php
<?php
/**
 * Auto-link Site Name - Global Version
 * Pulls company name from Settings > General > Site Title
 * Pulls link title from Settings > General > Tagline
 */
function site_name_auto_links($content) {
    // Fire on all singular post types (posts, pages, and all custom post types)
    if (!is_singular()) {
        return $content;
    }
    
    if (empty($content) || strlen($content) < 50) {
        return $content;
    }
    
    // Get site name and tagline from WordPress settings
    $site_name = get_bloginfo('name');
    $site_tagline = get_bloginfo('description');
    $site_url = home_url('/');
    
    // Skip if no site name is set
    if (empty($site_name)) {
        return $content;
    }
    
    $links = array(
        $site_name => array(
            'url'   => $site_url,
            'title' => !empty($site_tagline) ? $site_tagline : $site_name
        ),
    );
    
    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML('<?xml encoding="UTF-8"><div id="site-name-wrap">' . $content . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    libxml_clear_errors();
    
    foreach ($dom->childNodes as $item) {
        if ($item->nodeType == XML_PI_NODE) {
            $dom->removeChild($item);
        }
    }
    $dom->encoding = 'UTF-8';
    
    $xpath = new DOMXPath($dom);
    
    $text_nodes = $xpath->query('
        //text()[
            (ancestor::p or ancestor::li or ancestor::td)
            and not(ancestor::a)
            and not(ancestor::h1)
            and not(ancestor::h2)
            and not(ancestor::h3)
            and not(ancestor::h4)
            and not(ancestor::h5)
            and not(ancestor::h6)
            and not(ancestor::button)
            and not(ancestor::script)
            and not(ancestor::style)
            and not(ancestor::figcaption)
            and not(ancestor::figure)
            and not(ancestor::img)
            and not(ancestor::input)
            and not(ancestor::textarea)
            and not(ancestor::header)
            and not(ancestor::footer)
            and not(ancestor::nav)
            and not(ancestor::blockquote)
        ]
    ');
    
    foreach ($text_nodes as $text_node) {
        $text = $text_node->nodeValue;
        $replaced = false;
        
        foreach ($links as $keyword => $data) {
            if (stripos($text, $keyword) !== false) {
                $pattern = '/\b(' . preg_quote($keyword, '/') . ')\b/i';
                $replacement = '<a href="' . esc_url($data['url']) . '" title="' . esc_attr($data['title']) . '">$1</a>';
                $text = preg_replace($pattern, $replacement, $text, -1);
                $replaced = true;
            }
        }
        
        if ($replaced) {
            $fragment = $dom->createDocumentFragment();
            @$fragment->appendXML($text);
            if ($fragment->hasChildNodes()) {
                $text_node->parentNode->replaceChild($fragment, $text_node);
            }
        }
    }
    
    $wrapper = $dom->getElementById('site-name-wrap');
    if ($wrapper) {
        $result = '';
        foreach ($wrapper->childNodes as $child) {
            $result .= $dom->saveHTML($child);
        }
        return $result;
    }
    
    return $content;
}
add_filter('the_content', 'site_name_auto_links', 999);

Comments

Add a Comment