RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.38% (6.14%*) • Home Loan Fixed: 5.44% (6.26%*) • Fixed: 5.44% (6.26%*) • Variable: 5.38% (6.14%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Favicons with Outgoing Links in WordPress (or PHP) With Google’s Icon API

Favicons with Outgoing Links in WordPress (or PHP) With Google’s Icon API

Our online favicon generation tool (until a few days ago) included an API to return a favicon image that could be used with a PHP function to render the icon alongside any outgoing website link. The icon adds a stylistic element to links and provides a means of immediate brand recognition and assurance for popular websites.

While the service was free and provided a funky feature to users, it wasn't without benefit on our end. Every time a request was made for an icon we would record various details as a means of measuring traffic to certain sites, and it gave us a unique means of measuring web trends. However, like all API-based services, it attracted its fair share of spammers and would at times result in dodgy online practitioners batch-requesting multiple hundreds of thousands of icons for their own use. It's this ongoing abuse that has forced us to provide a time-expiry on the existing free icon API (the site will continue to manufacture site icons from uploaded images only). The API will be improved upon and ported over as a registered tool to this website in the next month. If you're existing legitimate user, get in touch and we'll provide you with a key.

Link-optimization is a hugely neglected part of SEO, and one of those areas that authors don't necessarily consider when producing content. Our client-only (at this stage) plugin turns links into magnets, and it feeds search engines with everything they need to rank your content with authority.

For those that couldn't be bothered with our new-and-improved service in the future, you can use a Google alternative to render a site icon alongside outgoing links. The function below makes use of Google's icon API to return links that aren't visibly different from our own current link functions.

Like all unregistered and 'unofficial' APIs provided by Google, there's no guarantee of continued availability. However, the advantage of shortcode is that if (and when) Google closes its doors you can simply migrate over to another service.

Before we port the API over to this site, we'll make details available here.

The Result

The shortcode of [link url="http://www.flight.org/" title="FLIGHT Link"]link in here[/link] returns the following: link in here FLIGHT Link (a link rendered with an icon image). For an external link External Link where you haven't defined a title, we'll return 'External Link'. The icon itself will have a title element matching the destination site's root domain.

If you're linking internally, a post ID is always preferred to a qualified URL (the ID won't be affected by permalink changes, domain changes, site reconfiguration etc). In such cases where you sensibly choose to use an ID rather than a URL, the shortcode of [link url="5156"]link in here[/link] will return link in here The Importance of a Website Favicon. Note that we've automatically retrieved and applied the title text. However, if you define your own title in your shortcode, we'll use your custom text.

If you link to an unpublished post, we'll return an empty link with the title of 'Not Yet Published'. If the ID referenced in your shortcode is an image, we'll return an image link with title and alt tags automatically populated (as long as you include the title tags when uploading images).

For those using our existing link shortcode function, the code below is a cut-and-paste replacement.

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

The shortcode requires that you include the function from "Get Root Domain from a URL with PHP" from this page.

1
<?php 
2
/*
3
 Favicons with Outgoing Links in WordPress (or PHP) With Google's Icon API
4
 http://www.beliefmedia.com/google-favicon-links
5
 Requires: http://www.beliefmedia.com/root-domain-url-php
6
*/
7
 
8
function beliefmedia_google_icon_links($atts, $content = null) {
9
 
10
  $atts = shortcode_atts(array(
11
    'url' => '',
12
    'title' => '',
13
    'size' => '14',
14
    'target' => '_blank',
15
    'follow' => 0,
16
    'internal' => 'beliefmedia.com',
17
    'align' => '-3'
18
  ), $atts);
19
 
20
  /* Post ID */
21
  global $post;
22
  $id = $post->ID;
23
 
24
  /* If the post is numeric (internal) */
25
  if (is_numeric($atts['url'])) {
26
 
27
    /* Is the ID an image link? */
28
    if (wp_attachment_is_image($atts['url']) !== false) return '<a href="' . wp_get_attachment_url($atts['url']) . '" alt="' . get_the_title($atts['url']) . '" title="Image: ' . get_the_title($atts['url']) . '">' . $content . '</a>';
29
 
30
    $title = ($atts['title'] == '') ? get_the_title($atts['url']) : $atts['title'];
31
    if (get_post_status($atts['url']) != 'publish') return '<a title="Not Yet Published">' . $content . '</a>';
32
    $atts['url'] = get_the_permalink($atts['url']);
33
 
34
  } else {
35
 
36
    /* If no link title defined, use title */
37
    $title = ($atts['title'] == '') ? $external = ($domain == $atts['internal']) ? get_the_title($id) : 'External Link' : $atts['title'];
38
 
39
  }
40
 
41
  /* Get root domain from URL */
42
  $domain = beliefmedia_get_domain($atts['url']);
43
 
44
 /* Return link */
45
 return ($domain == $atts['internal']) ? '<a href="' . $atts['url'] . '" target="' . $atts['target'] . '" title="' . $title . '" rel="noopener noreferrer">' . $content . '</a>' : '<a href="' . $atts['url'] . '" target="' . $atts['target'] . '" title="' . $title . '" rel="&#039; . $follow = ($atts[&#039;follow&#039;]) ? &#039;follow&#039; : &#039;nofollow&#039; noopener noreferrer">' . $content . '</a> <img src="https://s2.googleusercontent.com/s2/favicons?domain=' . $domain . '" style="width:' . $atts['size'] . 'px; height:' . $atts['size'] . 'px; vertical-align:' . $atts['align'] . 'px; display: inline; margin: 0;" alt="' . $title . '" title="' . $domain . '">';
46
}
47
add_shortcode('link', 'beliefmedia_google_icon_links');

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Shortcode Attributes

The following attributes are available.

url

The URL to link to; either a fully constructed URL or internal link/image ID.

title

The title tag. For internal links, it may be omitted and we'll render the relevant page title or image title text. If provided in your shortcode it will be used.

size

The size of the Google icon. The icon is returned as a 16x16 image.

target

The target for external links. By default it will use _blank (new window).

follow

You don't necessarily want to contribute towards the positive page-rank of linked websites. By default we'll add rel="nofollow" tags to all outgoing links . To disable the tag, use follow="1".

internal

The internal URL root domain. Images won't be rendered for internal links.

align

The vertical alignment of the Google icon image relative to text. We use -3 by default.

PHP Function

The purpose of the PHP function is to return just the Google icon.

1
<?php 
2
/*
3
 Favicons with Outgoing Links in WordPress (or PHP) With Google's Icon API
4
 http://www.beliefmedia.com/google-favicon-links
5
 Requires: http://www.beliefmedia.com/root-domain-url-php
6
*/
7
 
8
function beliefmedia_glink($url, $title = 'External Link', $size = '14', $align = '-3') {
9
  $pieces = parse_url($url);
10
  $domain = isset($pieces['host']) ? $pieces['host'] : '';
11
  $root = (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $m)) ? $m['domain'] : false;
12
 
13
 return ($root !== false) ? '<img src="https://s2.googleusercontent.com/s2/favicons?domain=' . $root . '" style="width:' . $size . 'px; height:' . $size . 'px; vertical-align:' . $align . 'px;" alt="' . $title . '" title="' . $domain . '">' : '';
14
}
15
 
16
/* Usage */
17
echo beliefmedia_glink($url = 'http://www.beliefmedia.com/');

Considerations

  • Our forthcoming code (dealing with our own API) includes additional link features such as automatically populating (and checking) and destination titles and automatic link truncation. It's more of a complete link-SEO solution than just a simple tool.

The Favicon Series

  1. The Importance of a Website FaviconJuly 23, 2017
  2. Reference (Additional) Device Favicons in WordPressJuly 26, 2017
  3. Favicons with Outgoing Links in WordPress (or PHP) With Google’s Icon APIJuly 27, 2017
  4. One-Click Website Favicons and Automated MaintenanceApril 13, 2021

Download


Title: Favicons with Outgoing Links in WP (or PHP) With Google's Icon API)
Description: Favicons with Outgoing Links in WordPress (or PHP) With Google's Icon API.
  Download • Version 0.1, 1.0K, zip, Category: WordPress Shortcodes

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment