Skip to content

Instantly share code, notes, and snippets.

@artlung
Last active October 26, 2023 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artlung/71a6af202e5ff8287331735bd4a36ad4 to your computer and use it in GitHub Desktop.
Save artlung/71a6af202e5ff8287331735bd4a36ad4 to your computer and use it in GitHub Desktop.
I wanted to replace the text of my menu with font awesome icons so I created a new walker class to do replacements for links with specific outbound links. Not at all generic but I wanted to stash it. I'm sure there are better ways to do this.
a.social-link {
* {
&.color-orange {
color: orange;
&:hover {
color: darken(orange, 10%);
}
&:visited {
color: orange;
}
}
&.color-mastodon {
color: #2b90d9;
&:hover {
color: darken(#2b90d9, 10%);
}
&:visited {
color: #2b90d9;
}
}
&.color-twitter {
color: #1da1f2;
&:hover {
color: darken(#1da1f2, 10%);
}
&:visited {
color: #1da1f2;
}
}
&.color-mail {
color: #000;
&:hover {
color: darken(#000, 10%);
}
&:visited {
color: #000;
}
}
&.color-facebook {
color: #3b5998;
&:hover {
color: darken(#3b5998, 10%);
}
&:visited {
color: #3b5998;
}
}
&.color-linkedin {
color: #0077b5;
&:hover {
color: darken(#0077b5, 10%);
}
&:visited {
color: #0077b5;
}
}
&.color-instagram {
color: #e1306c;
&:hover {
color: darken(#e1306c, 10%);
}
&:visited {
color: #e1306c;
}
}
&.color-flickr {
color: #ff0084;
&:hover {
color: darken(#ff0084, 10%);
}
&:visited {
color: #ff0084;
}
}
}
}
<?php
// https://developer.wordpress.org/reference/functions/wp_nav_menu/
$args = [
'menu' => 'roanoke-menu-bar',
'container' => 'nav',
'container_class' => 'roanoke-menu-bar',
'container_aria_label' => 'Menu Bar',
'walker' => new RoanokeMenuWalker(),
];
wp_nav_menu($args);
<?php
// Requires wp_enqueue_script('font-awesome', '//use.fontawesome.com/releases/v5.8.0/js/all.js');
// to be part of your scripts
class RoanokeMenuWalker extends Walker_Nav_Menu {
public static $replacements = [
'https://instagram.com/artlung' => 'fab fa-instagram color-instagram',
'https://xoxo.zone/@artlung' => 'fab fa-mastodon color-mastodon',
'https://twitter.com/artlung' => 'fab fa-twitter color-twitter',
'https://www.facebook.com/artlung' => 'fab fa-facebook color-facebook',
'https://www.linkedin.com/in/artlung/' => 'fab fa-linkedin color-linkedin',
'https://www.flickr.com/photos/artlung/' => 'fab fa-flickr',
'mailto:joe@artlung.com' => 'fas fa-envelope color-mail',
'https://artlung.com/feed/' => 'fas fa-rss color-orange',
];
public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) {
parent::start_el( $output, $data_object, $depth, $args, $current_object_id ); // TODO: Change the autogenerated stub
$href = $data_object->url;
$inner_text = $data_object->title;
foreach(self::$replacements as $needle => $replacementClass) {
if (strpos($href, $needle) !== false) {
$outputDom = new DOMDocument();
$outputDom->loadHTML($output);
$anchors = $outputDom->getElementsByTagName('a');
foreach($anchors as $anchor) {
if ($anchor->getAttribute('href') === $href) {
$anchor->nodeValue = '';
$fontAwesomeIcon = $outputDom->createElement('i');
$fontAwesomeIcon->setAttribute('class', $replacementClass);
$anchor->appendChild($fontAwesomeIcon);
// set attribute on the anchor
$anchor->setAttribute('class', 'social-link');
$anchor->setAttribute('target', '_blank');
}
}
$output = $outputDom->saveHTML();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment