-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavicon-links.php
55 lines (47 loc) · 1.54 KB
/
favicon-links.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* Plugin Name: Favicon Links
* Plugin URI: http://whyte624.ru/prj/wordpress-favicon-links
* Description: Adds favicons to all links in any post
* Version: 1.1
* Author: Andrei Sozonov
* Author URI: http://whyte624.ru/
* License: GPL2
*/
/**
* @param $content
* @return string
*/
function favicon_links_the_content($content)
{
try {
$html = new DOMDocument(null, 'UTF-8');
@$html->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">' . $content);
foreach ($html->getElementsByTagName('a') as $a) {
/* @var DOMElement $a */
$imgs = $a->getElementsByTagName('img');
if ($imgs->length > 0) {
continue;
}
$url = $a->attributes->getNamedItem('href');
if (!$url) {
continue;
}
$urlParts = parse_url($url->textContent);
if (!$urlParts || empty($urlParts['host'])) {
continue;
}
if ($a->attributes->getNamedItem('data-no-favicon')) {
continue;
}
$img = $html->createElement('img');
$img->setAttribute('src', 'http://www.google.com/s2/favicons?domain=' . $urlParts['host']);
$img->setAttribute('style', 'display: inline-block; padding-right: 4px;');
$a->insertBefore($img, $a->firstChild);
}
return $html->saveHTML();
} catch (Exception $e) {
return $content;
}
}
add_filter('the_content', 'favicon_links_the_content');